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: