Get amount of white(-ish) pixels in image(PNG)

Magick++ is an object-oriented C++ interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick++.
Post Reply
mham
Posts: 10
Joined: 2012-09-01T11:06:39-07:00
Authentication code: 67789

Get amount of white(-ish) pixels in image(PNG)

Post by mham »

Hi,
what is the best way to get the amount of pixels that fall in a certain RGB range. I'm looking for a way to determine how much whitespace my image has. For starters it would be enough to measure the really white pixels (255,255,255) - but then I need to measure a range, let's say from (200,200,200)to(255,255,255).
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Get amount of white(-ish) pixels in image(PNG)

Post by snibgo »

To count the number of pixels that are exactly (or approximately) any given colour, I would:

1. Turn that colour white. Eg "-fuzz 5% -fill White -opaque {colour}"

2. Turn all other pixels black. "-fill Black +opaque White"

3. Find the mean. This is the proportion of pixels that are now white. "-format %[fx:mean] info:" Multiply by the number of pixels. "-format %[fx:int(mean*w*h+0.5)] info:"

Finding colour in a range is trickier. Do you mean:

200<=red<=255 AND 200<=green<=255 AND 200<=blue<=255 ?

"-fx" is probably the neatest answer. But it is slow.
snibgo's IM pages: im.snibgo.com
mham
Posts: 10
Joined: 2012-09-01T11:06:39-07:00
Authentication code: 67789

Re: Get amount of white(-ish) pixels in image(PNG)

Post by mham »

Thank you for the quick answer, snibgo.

So I did this

Code: Select all

convert 50889165.png -fuzz 4% -fill White -opaque White out.png
convert out.png -fuzz 4% -fill Black +opaque White out.png
which gives me a black and white image -great!

Code: Select all

convert out.png  -format %[fx:int(mean*w*h+0.5)] info:
Gives me an output of 4.13491e+006 . How do I interprete this number?

Yeah I mean I need to find pixels that are not 100% pure white but kind of. This could happen if a white sheet of paper got scanned in for example
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Get amount of white(-ish) pixels in image(PNG)

Post by snibgo »

The mean is a number from 0.0 to 1.0. This is the proportion that is white. For example, a result of 0.25 would mean that 25% of pixels were white.

When you multiply this by the number of pixels in the image, which is width*height, the result is the number of pixels that were white.

4.13491e+006 is about 4.1 million. That's the number of pixels that were white. Use "-precision 15" to write it as an ordinary integer.


I would do the whole thing in a single convert:

Code: Select all

convert 50889165.png -fuzz 4% -fill White -opaque White -fuzz 0 -fill Black +opaque White -precision 15 -format %[fx:int(mean*w*h+0.5)] info:
snibgo's IM pages: im.snibgo.com
mham
Posts: 10
Joined: 2012-09-01T11:06:39-07:00
Authentication code: 67789

Re: Get amount of white(-ish) pixels in image(PNG)

Post by mham »

That's brilliant and it is a big help to my solution - thank you very much snibgo!
Post Reply