Extract mask from specific color from some point

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
Slig

Extract mask from specific color from some point

Post 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
Slig

Re: Extract mask from specific color from some point

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Extract mask from specific color from some point

Post by fmw42 »

Slig

Re: Extract mask from specific color from some point

Post 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.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Extract mask from specific color from some point

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply