Determine number of pixels within certain RGB value range

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?".
Post Reply
cv_scientist

Determine number of pixels within certain RGB value range

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA
Contact:

Re: Determine number of pixels within certain RGB value rang

Post 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
Last edited by fmw42 on 2010-07-16T10:00:55-07:00, edited 1 time in total.
cv_scientist

Re: Determine number of pixels within certain RGB value rang

Post by cv_scientist »

That was exactly what I was looking for.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA
Contact:

Re: Determine number of pixels within certain RGB value rang

Post 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
Post Reply

Who is online

Users browsing this forum: Amazon [Bot], Google [Bot] and 2 guests