Page 1 of 1

Complex masking situation

Posted: 2011-06-08T13:32:08-07:00
by BatsShadow
I'm working on a weird problem for a project, and I need to do some complex masking. I have an image that looks like this:
Image

The real images are larger and the shapes are irregular, but there is always a black outline on a white background and then more black area inside the outline. The inside shape may not be the same as the outside outline. What I need to do is cut out only the outside black outline, masking out the rest of the image.

The end result should look like this:
Image

I would like to do this without specifying any coordinates for the different areas. The only guarantee is that the black area never touches the edge of the image. Is this possible?

Re: Complex masking situation

Posted: 2011-06-08T14:42:41-07:00
by fmw42
try this

infile="boxes.gif"
coords=`convert $infile -format "%[fx:w/2],%[fx:h/2]" info:`
echo $coords
convert $infile -fuzz 1% -fill white -draw "color $coords floodfill" -transparent white boxes_border.gif

vary the fuzz XX% as much as needed if not pure black and white, ie. if antialiased so the edges are gray. If pure black and white, then -fuzz is not needed.

Note this does not seem to work right in IM 6.7.0.5 Q16 with png output. I will report the bug.


if in Windows, see differences at http://www.imagemagick.org/Usage/windows/

Re: Complex masking situation

Posted: 2011-06-09T09:15:05-07:00
by BatsShadow
Yep, this works, assuming that the center area always covers the center pixel. In the end, that may not be the case, but we can cross that bridge if we run into it.

Re: Complex masking situation

Posted: 2011-06-09T20:22:54-07:00
by anthony
This is where the color of the -floodfill operator comes in useful. If the pixel is NOT the expected matching color, floodfill does nothing. It aborts gracefully in unexpected situations.
http://www.imagemagick.org/Usage/color_ ... #floodfill

Draw flood fill (immediately above in the given link) will always floodfill the specified area, regardless of its color.