Is it possible to detect the amount of a specific color (in this case, yellow) as a percentage or number of pixels in an image?
To be precise, I want to tell how much of my image contains #ECAE01 for a script I'm writing.
Any and all help much appreciated!
Detect amount of color in image
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Detect amount of color in image
alexp2_ad wrote:Is it possible to detect the amount of a specific color (in this case, yellow) as a percentage or number of pixels in an image?
To be precise, I want to tell how much of my image contains #ECAE01 for a script I'm writing.
Any and all help much appreciated!
What if your image has no pixels with that exact color value? You can look at the histogram and see the pixel counts for any given color value.
convert yourimage -format %c histogram:info:-
But some images have too many colors for a histogram.
Here is how you would do it if your image has no white in it. You can adjust the fuzz value to make it as close to zero as you want to get that exact color value. Change your color to white, then change every non-white value to black. Then get the mean value of the image (in the range 0 to 1) and multiply by 100
convert yourimage -fuzz 5% -fill white -opaque "#ECAE01" -fill black +opaque white yourimage_tmp.gif
convert yourimage_tmp.gif -format "%[fx:100*mean]" info:
or in one step
convert yourimage -fuzz 5% -fill white -opaque "#ECAE01" -fill black +opaque white -format "%[fx:100*mean]" info:
If your image has white in it, then it is a bit harder but you can use transparent as a temporary color.
First, change your color to transparent. Then change all non-transparent to black. Then change all transparent to white. Then get 100xmean.
convert yourimage -fuzz 5% -transparent "#ECAE01" +fuzz \
-fill black +opaque none -channel rgba -fill white -opaque none yourimage_tmp.gif
convert yourimage_tmp.gif -format "%[fx:100*mean]" info:
or in one step
convert yourimage -fuzz 5% -transparent "#ECAE01" +fuzz \
-fill black +opaque none -channel rgba -fill white -opaque none \
-format "%[fx:100*mean]" info:
Re: Detect amount of color in image
Thanks for your help, I'm just having a play around with your commands, I think I see how they all work, but I am having one problem: they don't seem to be running properly on the command line - specifically whatever is after +opaque generates a message along the lines of "convert: unable to open image `none': No such file or directory." - this is true if I try precise rgba colors or just any color names (like none) - I can't see why it thinks this should be a file and not a color. Am I doing something wrong?
Thanks again!
EDIT: If it's relevant info I'm using the bash shell with Mac OS X 10.5 and ImageMagick 6.4.1
Thanks again!
EDIT: If it's relevant info I'm using the bash shell with Mac OS X 10.5 and ImageMagick 6.4.1
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Detect amount of color in image
alexp2_ad wrote:Thanks for your help, I'm just having a play around with your commands, I think I see how they all work, but I am having one problem: they don't seem to be running properly on the command line - specifically whatever is after +opaque generates a message along the lines of "convert: unable to open image `none': No such file or directory." - this is true if I try precise rgba colors or just any color names (like none) - I can't see why it thinks this should be a file and not a color. Am I doing something wrong?
Thanks again!
EDIT: If it's relevant info I'm using the bash shell with Mac OS X 10.5 and ImageMagick 6.4.1
I made those examples above on Mac OSX Tiger with IM 6.4.6-8. Perhaps you need to upgrade your IM, possibly a bug in earlier versions. Also be sure to include the \ as line continuation or remove them and make one long command line.
Try a simpler test with just +opaque, such as
convert yourimage -fill red +opaque someothercolorinyourimage tmp.png
That should make everything but someothercolorinyourimage red.
Or try replacing +fuzz with -fuzz 0 or remove it all together. I am not sure, now, that there is a +fuzz. I was just trying to turn off the -fuzz 5%.
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: Detect amount of color in image
Use +opaque and -opaque to make a mask of that color and not color and output the statistics.
It is even easier if the image has no transparency!
Using colorwheel.png
get number of pixels within 25% of yellow.
for exact just set -fuzz to 0 or leave it out completely.
It is even easier if the image has no transparency!
Using colorwheel.png
get number of pixels within 25% of yellow.
Code: Select all
convert colorwheel.png -alpha set \
-fuzz 25% -transparent yellow \
-verbose info:
or 5.1% of the image has colors simular to yellow!...
alpha:
min: 0 (0)
max: 65535 (1)
mean: 3379.95 (0.0515747)
standard deviation: 14494.2 (0.221167)
...
for exact just set -fuzz to 0 or leave it out completely.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
Re: Detect amount of color in image
I updated my version of ImageMagick and +opaque seems to work now - must have not been in / working in my version.
Thanks for both your help, I'll play around and see what solution works best for me.
Thanks again!
Thanks for both your help, I'll play around and see what solution works best for me.
Thanks again!