Page 1 of 1
Determine number of pixels within certain RGB value range
Posted: 2010-07-16T04:20:45-07:00
by cv_scientist
Dear ImageMagick Users,
The RGB values of a pixel can be determined by the command:
Code: Select all
convert example.png -format "%[pixel:s.p{10,10}]" info:
This works fine, but how can I determine how many pixels have RGB values within a certain range (for example [0,0,0] to [0,64,255])?
Thanks in advance for your help.
Re: Determine number of pixels within certain RGB value rang
Posted: 2010-07-16T09:50:26-07:00
by fmw42
cv_scientist wrote:Dear ImageMagick Users,
The RGB values of a pixel can be determined by the command:
Code: Select all
convert example.png -format "%[pixel:s.p{10,10}]" info:
This works fine, but how can I determine how many pixels have RGB values within a certain range (for example [0,0,0] to [0,64,255])?
Thanks in advance for your help.
Color ranges can be a bit ambiguous. The best interpretation that I can give is to find all pixels that are red=0, green between 0 and 64, and blue between 0 and 255 (all blue values). So one way to do that is to create a mask for each channel that is white for each range you want and black elsewhere. For a range of gray values in a channel, you can use a combination of -black-threshold and -white-threshold to get your the range. Then -compose multiply -composite the masks to get one mask. Then get the mean of the mask (in range 0-1, via -format "%[fx:mean]" info:) and multiply that by the total pixels in the image.
convert image -separate image_%d
convert image_0 -fill black +opaque white -fill white -opaque black image_red_mask
convert image_2 -threshold -1 image_blue_mask
convert image-1 -black-threshold 0% -white-threshold 25.1% -fill black -opaque white -fill white +opaque black image_green_mask
(note 64/255=25.1%)
convert image_red_mask image_green_mask -compose multiply -composite image_blue_mask -compose multiply -composite image_mask
convert image_mask -format "%[fx:mean*w*h]" info:
should produce the number of pixels with those overlapping color ranges
Re: Determine number of pixels within certain RGB value rang
Posted: 2010-07-16T09:59:15-07:00
by cv_scientist
That was exactly what I was looking for.
Re: Determine number of pixels within certain RGB value rang
Posted: 2010-07-22T15:59:48-07:00
by fmw42
I have now created a script, locatecolors, to do this. It will identify those colors in an image and make the rest transparent and show the number and percent of colors found. see link below