Page 1 of 1
How to count the amount of white in an image
Posted: 2011-07-26T05:55:10-07:00
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 ?
Re: How to count the amount of white in an image
Posted: 2011-07-26T09:26:50-07:00
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/
Re: How to count the amount of white in an image
Posted: 2011-07-26T19:22:09-07:00
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
Re: How to count the amount of white in an image
Posted: 2011-07-26T19:33:48-07:00
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.
Re: How to count the amount of white in an image
Posted: 2011-07-28T03:58:25-07:00
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 ...
Re: How to count the amount of white in an image
Posted: 2011-08-16T12:18:57-07:00
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
Re: How to count the amount of white in an image
Posted: 2011-08-16T13:29:25-07:00
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%
Re: How to count the amount of white in an image
Posted: 2011-08-16T13:34:56-07:00
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):
As the reference, here is
case 2 (55.3376):
which calculates *less* white space than case 1!
Re: How to count the amount of white in an image
Posted: 2011-08-16T13:37:56-07:00
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.
Re: How to count the amount of white in an image
Posted: 2011-08-16T13:40:04-07:00
by xpt
More examples to test:
Re: How to count the amount of white in an image
Posted: 2011-08-16T13:41:49-07:00
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?
Re: How to count the amount of white in an image
Posted: 2011-08-16T13:43:36-07:00
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
Re: How to count the amount of white in an image
Posted: 2011-08-16T18:31:52-07:00
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.
Re: How to count the amount of white in an image
Posted: 2011-08-16T18:38:43-07:00
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".