select region that has similar color
Re: select region that has similar color
1. I have an alternative idea regarding the hole filling.
In Photoshop it would be: control and mouse click on the layer (that selects the layer), edit->fill with white.
Getting the outline would theoretically result in preserving the detail on the edges and the fill would guarantee the hole fillling.
Can that be done in IM?
2. Is there a way to detect if there are holes? I was thinking of turning the transparent areas (only those that are "inside" the mask) to black and counting the black blobs somehow.
3. Also, I was thinking on doing image quantization on the original image to reduce color variation (e.g. some blondes have different shades of color) in the hair. What do you think? Perhaps other ideas?
In Photoshop it would be: control and mouse click on the layer (that selects the layer), edit->fill with white.
Getting the outline would theoretically result in preserving the detail on the edges and the fill would guarantee the hole fillling.
Can that be done in IM?
2. Is there a way to detect if there are holes? I was thinking of turning the transparent areas (only those that are "inside" the mask) to black and counting the black blobs somehow.
3. Also, I was thinking on doing image quantization on the original image to reduce color variation (e.g. some blondes have different shades of color) in the hair. What do you think? Perhaps other ideas?
- 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
IM does not know where or if there are holes per se. The morphology method is the closest, but it does affect other borders. The idea is to fill the holes by the amount of the shape, then remove the same amount. If the hole gets filled it does not get removed. But if the hole still has a small hole in it, it will get opened up again. The edges will be added and removed, but the shape will change a little due to the shape of the kernel used.
IM does not have much interactive processing. So you would need to find some way to extract the location of each hole and floodfill it. You can convert the image to txt format and filter the black. Then you would need to find a way to clump regions and then distinguish between the black holes and the other regions outside your figure (presumably by size). IM can only give you the coordinates of each black pixels. see http://www.imagemagick.org/Usage/files/#txt. This is really a segmentation problem and IM does not have much yet in that way.
You can try color quantization and see what happens. That is easy to do. I have no idea about the results.
IM does not have much interactive processing. So you would need to find some way to extract the location of each hole and floodfill it. You can convert the image to txt format and filter the black. Then you would need to find a way to clump regions and then distinguish between the black holes and the other regions outside your figure (presumably by size). IM can only give you the coordinates of each black pixels. see http://www.imagemagick.org/Usage/files/#txt. This is really a segmentation problem and IM does not have much yet in that way.
You can try color quantization and see what happens. That is easy to do. I have no idea about the results.
Re: select region that has similar color
I realized that I'm probably mistaken about the Photoshop reference. That will return the holes as well and not just the outline of the hair (not tested).
I'm surprised that IM can't do simple outlining. :/
As for the alternative method for counting holes and filling them afterwards, I was thinking this: viewtopic.php?f=1&t=10889
But I don't know how to instruct it to count inside the outline.
If you know of a better tool than IM that can help, go ahead.
I'm surprised that IM can't do simple outlining. :/
As for the alternative method for counting holes and filling them afterwards, I was thinking this: viewtopic.php?f=1&t=10889
But I don't know how to instruct it to count inside the outline.
If you know of a better tool than IM that can help, go ahead.

- 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
see my script, magicwand,at the link below. But you have to tell it where the holes are one-at-a-time. The basic part is just a floodfill at a given coordinate. So you could write a simple script that would read a file of coordinates and floodfill each of them.
see -draw with floodfill or -floodfill
http://www.imagemagick.org/Usage/draw/#color
http://www.imagemagick.org/script/comma ... #floodfill
You can even specify -fuzz XX% with these, if needed to get close colors filled as well in the neighborhood of the point.
see -draw with floodfill or -floodfill
http://www.imagemagick.org/Usage/draw/#color
http://www.imagemagick.org/script/comma ... #floodfill
You can even specify -fuzz XX% with these, if needed to get close colors filled as well in the neighborhood of the point.
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: select region that has similar color
To close of all internal holes. add a border and flood fill from all edges. No invert that result and remove the border, and all internal holes will be closed.fmw42 wrote:see my script, magicwand,at the link below. But you have to tell it where the holes are one-at-a-time. The basic part is just a floodfill at a given coordinate. So you could write a simple script that would read a file of coordinates and floodfill each of them.
Only one flood fill is thus needed. However it will fail for any 'hole' that is in contact with the edge of the image!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
Re: select region that has similar color
I'm having trouble realizing the above into IM commands, can you help?To close of all internal holes. add a border and flood fill from all edges. No invert that result and remove the border, and all internal holes will be closed.
- 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
I am not sure this is exactly what Anthony suggest as I don't understand his comment fully, but try this as it may be close to what he is suggesting.

convert 1thres_15.png -threshold 50% -bordercolor white -border 1 \
-fill red -floodfill +0+0 white \
-fill red -floodfill +386+0 white \
-fill red -floodfill +386+596 white \
-fill red -floodfill +0+596 white \
-fill black +opaque red \
-fill white -opaque red \
-shave 1x1 1thres_15_filled.png
or
convert 1thres_15.png -threshold 50% -bordercolor white -border 1 \
-fill red -draw "color 0,0 floodfill" \
-fill red -draw "color 386,0 floodfill" \
-fill red -draw "color 386,596 floodfill" \
-fill red -draw "color 0,596 floodfill" \
-fill black +opaque red \
-fill white -opaque red \
-shave 1x1 1thres_15_filled.png

see
http://www.imagemagick.org/script/comma ... #floodfill
http://www.imagemagick.org/Usage/draw/#color

convert 1thres_15.png -threshold 50% -bordercolor white -border 1 \
-fill red -floodfill +0+0 white \
-fill red -floodfill +386+0 white \
-fill red -floodfill +386+596 white \
-fill red -floodfill +0+596 white \
-fill black +opaque red \
-fill white -opaque red \
-shave 1x1 1thres_15_filled.png
or
convert 1thres_15.png -threshold 50% -bordercolor white -border 1 \
-fill red -draw "color 0,0 floodfill" \
-fill red -draw "color 386,0 floodfill" \
-fill red -draw "color 386,596 floodfill" \
-fill red -draw "color 0,596 floodfill" \
-fill black +opaque red \
-fill white -opaque red \
-shave 1x1 1thres_15_filled.png

see
http://www.imagemagick.org/script/comma ... #floodfill
http://www.imagemagick.org/Usage/draw/#color
- 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
this also works, by extracting only the black holes, making the rest transparent and compositing over the original.
convert 1thres_15.png -threshold 50% \
\( +clone -bordercolor white -border 1 \
-fill black -draw "color 0,0 floodfill" \
-fill black -draw "color 386,0 floodfill" \
-fill black -draw "color 386,596 floodfill" \
-fill black -draw "color 0,596 floodfill" \
-negate -shave 1x1 -transparent white \) \
-compose over -composite \
1thres_15_filled.png
convert 1thres_15.png -threshold 50% \
\( +clone -bordercolor white -border 1 \
-fill black -draw "color 0,0 floodfill" \
-fill black -draw "color 386,0 floodfill" \
-fill black -draw "color 386,596 floodfill" \
-fill black -draw "color 0,596 floodfill" \
-negate -shave 1x1 -transparent white \) \
-compose over -composite \
1thres_15_filled.png
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: select region that has similar color
With the border, only one flood fill should be needed, as the border will ensure the fill 'attacks' the shape from all sides.
However I would probably fill with transparency, to make the mask, as it will be guranteed not to exist in the original image.
EG: fill using a separate channel.
However I would probably fill with transparency, to make the mask, as it will be guranteed not to exist in the original image.
EG: fill using a separate channel.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
- 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
anthony wrote:With the border, only one flood fill should be needed, as the border will ensure the fill 'attacks' the shape from all sides.
Thanks for the update. I was just following your instructions earlier. (however, in addition all that does is extract the holes, not fill them, so I modified it)
I did notice when I made my examples that no change was apparent from each floodfill after the first. So that did puzzled me.To close of all internal holes. add a border and flood fill from all edges. No invert that result and remove the border, and all internal holes will be closed.
Fred
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: select region that has similar color
Yes what I meant to say was that by adding a border, a single floodfill will flood from all edges.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
Re: select region that has similar color
Hello Fred!
I have been looking at this post of yours and the code you have written here below (for finding the central point of a circle) is what i was trying to do, and have been unsuccessful, for quite some time now. I am working usually on Windows DOS (shell), but I could not find any way of making this script work on DOS, neither on Cygwin, which i recently downloaded for Unix codes.
Would you be able to help me?
I would need to use this code integrated in a simpler code I have, which is:
convert test1.png -crop 45x45+256+240 -level 20%%,50%% -colorspace gray -depth 8 -compress none -threshold 19.6%% cropoutput.png
I use this line to take a specific region of a picture and with threshold and level correction I get a fully-black hole over a white background (the cropoutput.png picture output).
After this I would like to obtain, as you did, the position of the central pixel, outputted to a text file like "position.txt".
But I have not been able to include your script either in my DOS Shell nor in Cygwin shell...
any help would be appreciated! thanks so much!!
I have been looking at this post of yours and the code you have written here below (for finding the central point of a circle) is what i was trying to do, and have been unsuccessful, for quite some time now. I am working usually on Windows DOS (shell), but I could not find any way of making this script work on DOS, neither on Cygwin, which i recently downloaded for Unix codes.
Would you be able to help me?
I would need to use this code integrated in a simpler code I have, which is:
convert test1.png -crop 45x45+256+240 -level 20%%,50%% -colorspace gray -depth 8 -compress none -threshold 19.6%% cropoutput.png
I use this line to take a specific region of a picture and with threshold and level correction I get a fully-black hole over a white background (the cropoutput.png picture output).
After this I would like to obtain, as you did, the position of the central pixel, outputted to a text file like "position.txt".
But I have not been able to include your script either in my DOS Shell nor in Cygwin shell...
any help would be appreciated! thanks so much!!
fmw42 wrote:
[...]
Here is an example:
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
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: select region that has similar color
That script finds the average X-coordinate of the white pixels, and the average Y-coord. A Windows BAT script for this:
Code: Select all
set /A sigX=0
set /A sigY=0
set /A nCount=0
for /F "usebackq tokens=1,2 delims=,:" %%X ^
in (`%IM%convert shape_ellipse_mask.gif txt:^|find "white"`) ^
do (
set /A sigX+=%%X
set /A sigY+=%%Y
set /A nCount+=1
)
echo %sigX% %sigY% %nCount%
for /F "usebackq tokens=1,2 delims=,:" %%L ^
in (`%IM%identify -format "avgX=%%[fx:%sigX%/%nCount%]\navgY=%%[fx:%sigY%/%nCount%]" xc:`) ^
do set %%L
echo %avgX% %avgY%
snibgo's IM pages: im.snibgo.com