Page 1 of 1

Shaded Colour Replacement

Posted: 2008-10-02T11:02:44-07:00
by DeadlyDad
While I'm sure you've had this come up many times and I might have missed a post that duplicates what I suggest, here goes:

For many who want to remove a background from an image, they are using some form of 'green screen' behind an object. Comparing a control pic which contains no objects with one with an object in it work fairly well, but things like shadows throw the comparison off because they are different enough from the base colour that a larger 'fuzzy' value is required. The trouble with that is that a large enough value to include shadows tends to also include parts of the object. The shadow problem gave me an idea, though. Shadows on a single-color plane, while being different from the base colour in absolute value, are (pretty much) identical in their RGB ratio. For example, light blue-grey and dark blue-grey both have the same 86%/86%/100% RGB ratio. While using ratios won't work as well as the gradient color replacement, it should be easier to implement.

Parameters would be
  • Colour: The base colour.
    Range Min, Max: Sets the minimum and maximum values (absolute or percent of base colour values) for the search range, based on the largest of the RGB values.
    {Ratio}: (Optional) Three numbers that set the RGB ratio. Used if the base colour values aren't optimal. If percent signs are used, the two smaller values are expressed as percentages of the largest.
    {X, Y}: (Optional) If used, search is local to location and stopped by any borders. (i.e. non-matching pixels.) If not used, search is global, and any matching pixels in the whole image are selected.

Re: Shaded Colour Replacement

Posted: 2008-10-14T23:33:11-07:00
by anthony
See IM Examples... Masking with Anti-Aliased Edges
http://www.imagemagick.org/Usage/channe ... antialised

It may not seem like it but it is basically doing exactly what you describe.

Code: Select all

Extracting an image and its shadow from a known background (not necessarily a green screen) and using two percentages for the limits of the transparent-opaque range for the shadow.

The technique is fully described, all that is nessary is to convert it into either a script or program.

Also look at the new -sparse-color  option
   http://www.imagemagick.org/Usage/canvas/#sparse-color
which was created not to generate gradients, but as a means to generate background images for background removal.  It has more features than you think as its options are pre-parsed for 'percent escapes'
For example...
try this...
[code]convert -size 100x100 xc:blue -sparse-color voronoi '
         %[fx:w*rand()],%[fx:h*rand()] Black
         %[fx:w*rand()],%[fx:h*rand()] White
         %[fx:w*rand()],%[fx:h*rand()] Red
         %[fx:w*rand()],%[fx:h*rand()] Blue
         %[fx:w*rand()],%[fx:h*rand()] Lime
         %[fx:w*rand()],%[fx:h*rand()] Yellow
         %[fx:w*rand()],%[fx:h*rand()] Cyan
         %[fx:w*rand()],%[fx:h*rand()] Magenta
   ' x:
Now think of an existing image (random here) as a color source.
convert -size 100x100 xc:blue +noise random \
-sparse-color voronoi '
%[fx:w*rand()],%[fx:h*rand()] %[pixel:p{0,1}]
%[fx:w*rand()],%[fx:h*rand()] %[pixel:p{0,2}]
%[fx:w*rand()],%[fx:h*rand()] %[pixel:p{0,3}]
%[fx:w*rand()],%[fx:h*rand()] %[pixel:p{0,4}]
%[fx:w*rand()],%[fx:h*rand()] %[pixel:p{0,5}]
%[fx:w*rand()],%[fx:h*rand()] %[pixel:p{0,6}]
%[fx:w*rand()],%[fx:h*rand()] %[pixel:p{0,7}]
%[fx:w*rand()],%[fx:h*rand()] %[pixel:p{0,8}]
' x:[/code]
The colors come from the source image!
Of course the colors and locations also match up, which is planed to be implemented as a +sparse-color variation! I just have not gotten round to it yet!

Now imaging if you used on of the other shading methods like 'shepards' on a green screen image! Also imagine the green screen in an image having various shading effects causing by the lights used in the photo.

basically you can use it to re-create a clean empty background for background removal!

I just have not quite gone that far in the tutorials. I also want to add more sparse color methods that provide more localized shading rather than 'shepards' more universal shading effects.