Page 1 of 1

Extract mask from specific color from some point

Posted: 2007-12-27T13:52:35-07:00
by Slig
First of all, thank all of you, in special Anthony, for the documentation. It's great and easy to follow.

I'm trying to extract a mask from picture, based on some specific color from some point.

source pic:
Image

what I need to get when I use the command with x=200 & y=700 (yellow pixel at his right feet):
Image

I think that the line I'm using can explain a lot better what I'm trying to achieve:

Code: Select all

convert $pic \( +clone -fill white -fuzz $fuzz% -draw 'color $x,$y replace' \) +swap -compose ChangeMask -composite $mask"
With the above code I can get an get a transparent canvas with a white "mask". But what I really need is a mask, ie, the image must have a black background instead of a transparent one.

So I appended a extra +matte to the end of the command, but it didn't work. Then I tried this:

Code: Select all

convert $mask +matte test.gif
And it worked as expected! So I tried to use the pipe thing, so I don't need to create an extra temp file:

Code: Select all

convert $pic \( +clone -fill white -fuzz $fuzz% -draw 'color $x,$y replace' \) +swap -compose ChangeMask -composite miff:- | convert - +matte $mask
And this didn't work =/

What I'm missing? Or is there a more straightforward way to do the whole thing?

Thanks in advance,
Tiago

Re: Extract mask from specific color from some point

Posted: 2007-12-27T17:00:08-07:00
by Slig
Hi all again!

I managed to find a better solution, or at least less complicated(no clone, ChangeMask, etc), IMHO, after looking the source of MagicWand.

Here's how I'm doing now:

first we create a transparent mask for all the region that have the same color of the x,y coord:

Code: Select all

convert $pic -fuzz $fuzz% -fill none -draw 'matte $x,$y replace' -fill black -colorize 100% $mask
next we need a canvas with the same size of the mask with a white background. The MagicHand script uses identify to get the width and height and then creates a new canvas based on that. But I thought it was better to use some of the techniques found on Create canvas from the same size to create the canvas(and then saving us from figuring out the dimensions using identify) and then apply the transparent mask:

Code: Select all

convert $mask -background white $mask -compose over -flatten $mask
If you still have a better way to do this, please post here, as I'm very interested in learning it.

That's it! Thanks again IM team for the great product and the very useful docs.

Re: Extract mask from specific color from some point

Posted: 2007-12-28T17:16:27-07:00
by fmw42

Re: Extract mask from specific color from some point

Posted: 2007-12-28T17:21:02-07:00
by Slig
fmw42 wrote:try my script magicwand at

http://www.fmwconcepts.com/imagemagick/index.html
Hi!
Like I said in the second reply, I've seen your source and I find it very valuable. Thanks for sharing this.

Re: Extract mask from specific color from some point

Posted: 2008-01-02T19:23:51-07:00
by anthony
So I appended a extra +matte to the end of the command, but it didn't work.
That would only work if the transparent areas were modified to fully-transparent black, which may not be the case..

I talk about color replacement, as well as 'not-this-color' replacement in the forum topic... "Negating color range from command line"
viewtopic.php?f=1&t=10354

Basically this and the other topic is a two color segmentation of the color space, a similar topic was a three color segmentation that was recentally answered by me on the IM Email List...
http://www.nabble.com/Quantise-%2B-post ... 64186.html

This generalizes the problem more though the example being used is simpler.