How to count the amount of white in an image

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
markmarques
Posts: 88
Joined: 2010-06-29T14:36:09-07:00
Authentication code: 8675308

How to count the amount of white in an image

Post by markmarques »

Hi...
After some tryout from the "usage" docs I still can not figure it out how to count the amount of white existing in a grayscale image....
I have tryed several variations ( including -fuzz, -unique-colors, -opaque , etc) with no sucess :(

my original idea is to send a percentage value to a variable inside a script ( Windows ) something like :
set a = convert yyyy.jpg -colorspace gray -unique-colors -print "%fx[:white/(w*h)]" null:

No matter what "convert" arguments I try I simply get the value "1" ...

According to the docs I should be careful with the black color ...

Any ideas ?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to count the amount of white in an image

Post by fmw42 »

use -fuzz if desired if you want near-white as well as pure white, negate the image, threshold at 0, negate again, then compute the mean and multiply by 100.


percentwhite=`convert logo: -fuzz 0% -negate -threshold 0 -negate -format "%[fx:100*mean]" info:`
echo "percent white = $percentwhite"
percent white = 83.4128


If on windows, then the syntax for variables and other things is different. So see http://www.imagemagick.org/Usage/windows/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How to count the amount of white in an image

Post by anthony »

Another way to get the actual count is to get a color histogram.

See Extracting Image Colors
http://www.imagemagick.org/Usage/quantize/#extract
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to count the amount of white in an image

Post by fmw42 »

Per Anthony's suggestions, this gives the total count of white pixels


convert logo: -format %c histogram:info: | grep "white" | sed -n 's/^ *\(.*\):.*$/\1/p'
256244



So if you want the percent

count=`convert logo: -format %c histogram:info: | grep "white" | sed -n 's/^ *\(.*\):.*$/\1/p'`
percent=`convert logo: -format "%[fx:100*$count/(w*h)]" info:`
echo $percent
83.4128

But my earlier method only requires one line of processing rather than the two here.
markmarques
Posts: 88
Joined: 2010-06-29T14:36:09-07:00
Authentication code: 8675308

Re: How to count the amount of white in an image

Post by markmarques »

Just a quick reply:
After trying out the previous ideas I got what I needed by using something like :

"convert logo: -colorspace gray -fuzz 5% +opaque white -format "%[fx:mean*100]" info: "
83.996

No need to double negate anything ... :)

Thanks ...
xpt
Posts: 59
Joined: 2010-10-06T20:18:24-07:00
Authentication code: 8675308

Re: How to count the amount of white in an image

Post by xpt »

Q1: Why my calculation is different than all of yours?

Code: Select all

$ convert logo: -fuzz 0% -negate -threshold 0 -negate -format "%[fx:100*mean]" info:
82.4707

$ convert logo: -colorspace gray -fuzz 5% +opaque white -format "%[fx:mean*100]" info: 
99.9733
I.e., instead of advertised 83.4128 or 83.996, I get 82.4707 and 99.9733. My imagemagick version is 6.6.2.6-1, BTW.

Q2: to get white count from color histogram, if I want near-white as well as pure white to be counted, it is possible?

Thanks
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to count the amount of white in an image

Post by fmw42 »

xpt wrote:Q1: Why my calculation is different than all of yours?

First, your two calculations are quite different.

Second, at one point, I am not sure when, the meaning of [fx:mean] was changed from red channel only to global (all channel) mean. Look over changelog or upgrade your IM
xpt wrote:Q2: to get white count from color histogram, if I want near-white as well as pure white to be counted, it is possible?

Thanks
That is the point of the -fuzz value to get values near your color with some color distance expressed as XX%
xpt
Posts: 59
Joined: 2010-10-06T20:18:24-07:00
Authentication code: 8675308

Re: How to count the amount of white in an image

Post by xpt »

fmw42 wrote:use -fuzz if desired if you want near-white as well as pure white, negate the image, threshold at 0, negate again, then compute the mean and multiply by 100.
Will such algorithm confuse pure black as white?

case 1 (55.3489):
Image

As the reference, here is
case 2 (55.3376):
Image

which calculates *less* white space than case 1!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to count the amount of white in an image

Post by fmw42 »

what command did you run on these two images?

if -fuzz XX% is small, then it won't confuse white and black.

The reason is that when you threshold the image a lot of near white is becoming black.
Last edited by fmw42 on 2011-08-16T13:43:12-07:00, edited 1 time in total.
xpt
Posts: 59
Joined: 2010-10-06T20:18:24-07:00
Authentication code: 8675308

Re: How to count the amount of white in an image

Post by xpt »

More examples to test:

Image

Image
xpt
Posts: 59
Joined: 2010-10-06T20:18:24-07:00
Authentication code: 8675308

Re: How to count the amount of white in an image

Post by xpt »

what command did you run on these two images?
Exactly using

Code: Select all

convert file -fuzz 0% -negate -threshold 0 -negate -format "%[fx:100*mean]" info:
what do you get from this?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to count the amount of white in an image

Post by fmw42 »

The reason is that when you threshold the image a lot of near white is becoming black.

As you have some gray that is very close to white from antialiasing, you can see this by changing the fuzz value. The larger the fuzz value the more gray that is considered to be white.


convert 69bugcoloringpage03.jpg -fuzz 0% -fill white -opaque white -fill black +opaque white -format "%[fx:100*mean]" info:
55.3376

convert 69bugcoloringpage03.jpg -fuzz 10% -fill white -opaque white -fill black +opaque white -format "%[fx:100*mean]" info:
77.8961

You can see the effects by doing

convert 69bugcoloringpage03.jpg -fuzz 0% -fill white -opaque white -fill black +opaque white tmp1.png

convert 69bugcoloringpage03.jpg -fuzz 10% -fill white -opaque white -fill black +opaque white tmp2.png
xpt
Posts: 59
Joined: 2010-10-06T20:18:24-07:00
Authentication code: 8675308

Re: How to count the amount of white in an image

Post by xpt »

Thanks Fred,

Your

Code: Select all

convert file.jpg -fuzz 10% -fill white -opaque white -fill black +opaque white -format "%[fx:100*mean]" info:
algorithm works better for me, according to my visual inspection, between this one and the previous one.

Thank you.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to count the amount of white in an image

Post by fmw42 »

xpt wrote:Thanks Fred,

Your

Code: Select all

convert file.jpg -fuzz 10% -fill white -opaque white -fill black +opaque white -format "%[fx:100*mean]" info:
algorithm works better for me, according to my visual inspection, between this one and the previous one.

Thank you.

You should adjust the fuzz XX% to suit your situation as to how close/far from pure white you want to consider as still "white".
Post Reply