select region that has similar color

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?".
galv
Posts: 62
Joined: 2010-05-23T17:35:59-07:00
Authentication code: 8675308

select region that has similar color

Post by galv »

I'm seeking a function that acts like the magic wand tool in Photoshop/Gimp. I want to be able to click on a specific point on an image and have the nearby pixels selected (growing region) as long as their color is similar (does not exceed a certain threshold).

I'm talking about largest continuous area. So if the image is showing different fruit, clicking on a yellow pixel on a banana would expand and select all yellow-ish pixels of that banana.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: select region that has similar color

Post by fmw42 »

what do you want to do with the region once selected? If you want to recolor it, then see
http://www.imagemagick.org/Usage/draw/#color
http://www.imagemagick.org/Usage/draw/#matte

If you want to isolate it, such as make the region white or transparent and the background black, then you need to add some further code (provided that white or transparent does not exist in your image elsewhere). Just globally recolor everything that is not your recolored color using -fill ... +opaque ...

or see my unix bash script, magickwand, at the link below.


Perhaps you can post a link to an example image and describe what you want for your result. Also identify your IM version and platform.
galv
Posts: 62
Joined: 2010-05-23T17:35:59-07:00
Authentication code: 8675308

Re: select region that has similar color

Post by galv »

Thank you very much Fred!!
I used your magickwand script and it worked fine!

My IM version is
Version: ImageMagick 6.6.0-4 2010-11-16 Q16 http://www.imagemagick.org
and platform is Debian Linux.

Some questions:
1. Why did you list your magickwand script as the last option? Would the other methods you listed before have any difference?
2. Is there a way to make the processing faster? In large images there's a slight delay.

3. This is a new question but it's relevant. Is there a way to figure out the 'center' of the pixels that have the similar color?
Example: if i click once on a pixel with a specific color, it should return the coordinates of the most central pixel of similar colors. If the whole image is a square and after the click the magickwand script selects an area that is a circle, the central-most pixel would be the center of the circle.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: select region that has similar color

Post by fmw42 »

Some questions:
1. Why did you list your magickwand script as the last option? Would the other methods you listed before have any difference?
2. Is there a way to make the processing faster? In large images there's a slight delay.

3. This is a new question but it's relevant. Is there a way to figure out the 'center' of the pixels that have the similar color?
Example: if i click once on a pixel with a specific color, it should return the coordinates of the most central pixel of similar colors. If the whole image is a square and after the click the magickwand script selects an area that is a circle, the central-most pixel would be the center of the circle.
1&2) I listed my script last as it will be slower due to argument processing and all the other options it has. To process faster, you should use the direct IM commands that I listed from Anthony's Examples.

3) One would have to binarize the result and compute the centroid from the first few moments.

Centroid: {x, y } = {M10/M00, M01/M00 }

see
http://en.wikipedia.org/wiki/Image_moment
http://homepages.inf.ed.ac.uk/rbf/CVonl ... node4.html

Unfortunately, IM does not do such computations currently to my knowledge. However, it could be scripted and should not be hard if the image is binary as all one is doing is finding the average x and average y coordinates of the white pixels by converting the image to txt format.

Here is an example:

Image


xlist=`convert shape_ellipse_mask.gif txt:- | grep "white" | sed -n 's/^\(.*\):.*$/\1/p' | cut -d, -f1`
sum=0
count=0
for x in $xlist; do
sum=$(($sum+$x))
count=$(($count+1))
done
xave=`convert xc: -format "%[fx:$sum/$count]" info:`
echo $xave
50

ylist=`convert shape_ellipse_mask.gif txt:- | grep "white" | sed -n 's/^\(.*\):.*$/\1/p' | cut -d, -f2`
sum=0
count=0
for y in $ylist; do
sum=$(($sum+$y))
count=$(($count+1))
done
yave=`convert xc: -format "%[fx:$sum/$count]" info:`
echo $yave
50

so the centroid is at x,y = 50,50 which agrees with my measurements
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: select region that has similar color

Post by anthony »

Note that the centriod of an object may not be the object itself! Consider a arc, the centroid of the arc in in the middle space and not on the arc itself.

If what you are really wanting is the point that is furthest from any pixel not that color, That is the center of a large solid area of the specific color, take a look at the Morphological Distance function.
http://www.imagemagick.org/Usage/morphology/#distance

The Chebyshev distance kernel will for example find he point that is at the center of the largest odd-sized square that fits the mask image. Other distance kernels find progressively more circular center points.

If you want a rectangle with a specific aspect ratio you can modify the chebyshev kernel with different X and Y distance values.


Fred your true centroid point calculations would probably be much faster if done using awk (which I know you know how to do) or perl. However it is a good algorithm, and I probably should add that to IM Examples.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
galv
Posts: 62
Joined: 2010-05-23T17:35:59-07:00
Authentication code: 8675308

Re: select region that has similar color

Post by galv »

Thank you for your answers.

Something else:
a. Is there a way to instruct Fred's magickwand script not to include closed circles (closed paths)?
b. I want to enhance the image to make the magickwand script perform better. Meaning, not selecting areas that are not meant to be selected (caused by the -fuzz operator). I was thinking to do preprocessing on the image, like increasing the contrast so that the image segmentation will be more accurate. Maybe edge detection or sharpening? What do you suggest?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: select region that has similar color

Post by fmw42 »

galv wrote:Thank you for your answers.

Something else:
a. Is there a way to instruct Fred's magickwand script not to include closed circles (closed paths)?
b. I want to enhance the image to make the magickwand script perform better. Meaning, not selecting areas that are not meant to be selected (caused by the -fuzz operator). I was thinking to do preprocessing on the image, like increasing the contrast so that the image segmentation will be more accurate. Maybe edge detection or sharpening? What do you suggest?

A. No. The script has no knowledge about the shape.

B. I have no idea. Changing fuzz values is not much different from making the image more contrasty. But if you have some non-linear or edge operation or even morphologic operator, then perhaps that might help.

But I suggest you look at the script or Anthony's page and extract the basic algorithm as mentioned above and modify that. My script has too many choices of things to do. So it is slow and cumbersome.
galv
Posts: 62
Joined: 2010-05-23T17:35:59-07:00
Authentication code: 8675308

Re: select region that has similar color

Post by galv »

Sorry, maybe you misunderstood. I no longer want to find the centroid, I am clicking on the area to get the seed value/color. Anthony was proposing a solution for the centroid question if I understood correctly.
Also, maybe fuzz can become less faulty if I stretch the connected areas to extremes. Sometimes I get closed circles (areas within the area segmented that are not selected) which is what I want to avoid. High fuzz will select bad areas, low fuzz will result in holes in the final segmented image (closed circles).
I'm trying to improve this through image preprocessing.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: select region that has similar color

Post by fmw42 »

galv wrote:Sorry, maybe you misunderstood. I no longer want to find the centroid, I am clicking on the area to get the seed value/color. Anthony was proposing a solution for the centroid question if I understood correctly.
Also, maybe fuzz can become less faulty if I stretch the connected areas to extremes. Sometimes I get closed circles (areas within the area segmented that are not selected) which is what I want to avoid. High fuzz will select bad areas, low fuzz will result in holes in the final segmented image (closed circles).
I'm trying to improve this through image preprocessing.

Sorry, I really am not following your new objective. What code are you using to find the seed value? Then what do you do with it after that? What results are you trying to get? Perhaps you can post some more information or some links to your current input and results with your code.
galv
Posts: 62
Joined: 2010-05-23T17:35:59-07:00
Authentication code: 8675308

Re: select region that has similar color

Post by galv »

Sorry, you're right.

Objective: I'm trying to select the hair out of photos of persons.

The way I'm doing it right now requires to find the 'sweet spot' between low and high fuzz, so that it only selects the hair and not eyebrows, skin, shadows in the background and generally unwanted areas. The problem is better illustrated below.

I'm using a wrapper script in order to get the point coordinates (seed value) with a click of the mouse, and feed those (x,y) to your magickwand script.

The following image illustrates this( $ gmic image.png ) :
Image

I've prepared a few images to help you understand the problem. Each set includes an image that shows the area selected (the non interesting area turns transparent) and an image that colorizes the same selected area with red color.

Original Image

Image


a. threshold value: 15

ImageImage


b. threshold value: 20

ImageImage


c. threshold value: 27

ImageImage

As you can see, in "case a" it captures the outline of the hair relatively well.
Problems:
i. It produces "holes" (the closed circles/paths that I was talking about in earlier posts) in the selected area.
ii. It selects part of the left eye too.
iii. It fails to include the leftmost braid.

In "case b", the selection has fewer "holes" but part of the eyebrow and parts of the background are selected too. Also fails to include the leftmost braid.

In "case c", the selection has very few holes and succeeds in including the leftmost braid. Downsides: fairly large amount of the background and the eye/eyebrow get selected.

I want to improve the selection to be as accurate as possible and get rid of the "holes".
Note that this is only an example image, other image might be a blonde wearing different clothes.

Edit: Found this link ( http://www.cambridgeincolour.com/forums/thread2575.htm ) while searching for solutions, maybe it helps?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: select region that has similar color

Post by fmw42 »

for small holes, you can use morphology open to fill them in.

try this, though does not close large holes fully.

convert 1thres_15.png -morphology open diamond:3 1thres_15_open_d3.png

But you can play around with the threshold and the morphology size and iteration.

see http://www.imagemagick.org/Usage/morphology/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: select region that has similar color

Post by anthony »

Hair is a real problem in any image processing. Basically hair will generate large numbers of blended pixels, that is pixels that are part hair, part background, making it very hard to deal with.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
galv
Posts: 62
Joined: 2010-05-23T17:35:59-07:00
Authentication code: 8675308

Re: select region that has similar color

Post by galv »

I realize this is a hard problem, I just want to improve the detection as much as I can.

Morphology open is a good postprocessing suggestion, thanks Fred.
Is there a way to make it operate only on the area where the holes are? Because the way it works now, it's losing detail from outline/edges of the hair. Is there a way to identify where the holes are and apply morphology open on those spots only?

But I was thinking that the most important part would be preprocessing. Find edges so that the hair color will be further differentiated from the background and the skin color. I don't know if Fred's scripts histogram equilization or gamma correction or autolevels would help. Or nearest neighbour selection to avoid having the eyebrow selected? I don't know if such a thing exists in IM.
Or image quintization?

Anthony, I can make things easier by having photos on a white background only (yet it does not work that well for light blondes). Sometimes the hair have not so uniform distribution in colors and that is solved by increasing fuzz, which on the other hand falsely selects part of the skin/eyebrow.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: select region that has similar color

Post by fmw42 »

I believe you can minimize (but not remove completely) the effects on the edges by iterating rather than using a larger shape, but it will be slower.

Right now there is no way for morphology to find just the holes, that I know about. Perhaps Anthony will have other ideas. This is really a segmentation issue and as such IM is currently very limited.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: select region that has similar color

Post by anthony »

You could use a -region to limit a process to a specific area

http://www.imagemagick.org/Usage/masking/#region
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply