How to count the amount of white in an image
-
- Posts: 88
- Joined: 2010-06-29T14:36:09-07:00
- Authentication code: 8675308
How to count the amount of white in an image
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 ?
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 ?
- 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
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/
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/
- 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
Another way to get the actual count is to get a color histogram.
See Extracting Image Colors
http://www.imagemagick.org/Usage/quantize/#extract
See Extracting Image Colors
http://www.imagemagick.org/Usage/quantize/#extract
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
- 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
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.
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.
-
- Posts: 88
- Joined: 2010-06-29T14:36:09-07:00
- Authentication code: 8675308
Re: How to count the amount of white in an image
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 ...
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
Q1: Why my calculation is different than all of yours?
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
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
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
- 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
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
That is the point of the -fuzz value to get values near your color with some color distance expressed as XX%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
Re: How to count the amount of white in an image
Will such algorithm confuse pure black as white?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.
case 1 (55.3489):
As the reference, here is
case 2 (55.3376):
which calculates *less* white space than case 1!
- 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
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.
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.
Re: How to count the amount of white in an image
More examples to test:
Re: How to count the amount of white in an image
Exactly usingwhat command did you run on these two images?
Code: Select all
convert file -fuzz 0% -negate -threshold 0 -negate -format "%[fx:100*mean]" info:
- 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
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
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
Thanks Fred,
Your
algorithm works better for me, according to my visual inspection, between this one and the previous one.
Thank you.
Your
Code: Select all
convert file.jpg -fuzz 10% -fill white -opaque white -fill black +opaque white -format "%[fx:100*mean]" info:
Thank you.
- 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
xpt wrote:Thanks Fred,
Your
algorithm works better for me, according to my visual inspection, between this one and the previous one.Code: Select all
convert file.jpg -fuzz 10% -fill white -opaque white -fill black +opaque white -format "%[fx:100*mean]" info:
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".