Hi,
how can I detect how much white (or any other color) color a picture has?
Preferably percentage of pixels, but also amount of white pixels is enough.
I have fairly small pictures which are generated automatically, and for some
reason some of the pictures fail, they contain just an error message, rest of
the picture (more than 90%, almost 99% I think) is white.
being albe to find these picturec which contain a lot of white would help me
isolating those easily, now I have viewed those manually and I have has enough...
-Paavo
Detect how much white color a picture has?
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Detect how much white color a picture has?
Threshold the image to black and white with the threshold to catch only the white value you have.
If you are on a current release 6.4.0-11 or higher you can do:
convert logo: -threshold 99% -format "%[fx:100*image.mean]" info:
gives the percentage of white
you can change the threshold value to whatever absolute color value of white you have (rather than a percent) so if pure white and Q16, this could be
convert logo: -threshold 65534 -format "%[fx:100*image.mean]" info:
If you are before IM 6.4.0-11 but after IM 6.3.9-1, you have to use two steps and store the mean as temp variable
mean=`convert logo: -threshold 99% -format "%[mean]" info:`
convert xc: -format "%[fx:100*$mean/quantumrange]" info:
This can be combined awkwardly into one step as:
convert xc: -format "%[fx:100*$(echo `convert logo: -threshold 99% -format "%[mean]" info:`)/quantumrange]" info:
If you are before IM 6.3.9-1, you can still do it but need to use unix sed, for example, to extract the mean from the verbose info:
data=`convert logo: -threshold 99% -verbose info:`
mean=`echo "$data" | sed -n '/^.*[Mm]ean:.*[(]\([0-9.]*\).*$/{ s//\1/; p; q; }'`
convert xc: -format "%[fx:100*$mean]" info:
____________________________
If you have another color, see http://www.imagemagick.org/Usage/color/#opaque for replacing colors. For example the following should work to make the blue color into white and everything else black:
convert logo: -fill black +opaque "rgb(2,90,164)" -fill white -opaque "rgb(2,90,164)" tmp.png
This does not seem to work in IM 6.4.1-3 and has been reported as a bug and should be fixed in IM 6.4.1-4
If you are on a current release 6.4.0-11 or higher you can do:
convert logo: -threshold 99% -format "%[fx:100*image.mean]" info:
gives the percentage of white
you can change the threshold value to whatever absolute color value of white you have (rather than a percent) so if pure white and Q16, this could be
convert logo: -threshold 65534 -format "%[fx:100*image.mean]" info:
If you are before IM 6.4.0-11 but after IM 6.3.9-1, you have to use two steps and store the mean as temp variable
mean=`convert logo: -threshold 99% -format "%[mean]" info:`
convert xc: -format "%[fx:100*$mean/quantumrange]" info:
This can be combined awkwardly into one step as:
convert xc: -format "%[fx:100*$(echo `convert logo: -threshold 99% -format "%[mean]" info:`)/quantumrange]" info:
If you are before IM 6.3.9-1, you can still do it but need to use unix sed, for example, to extract the mean from the verbose info:
data=`convert logo: -threshold 99% -verbose info:`
mean=`echo "$data" | sed -n '/^.*[Mm]ean:.*[(]\([0-9.]*\).*$/{ s//\1/; p; q; }'`
convert xc: -format "%[fx:100*$mean]" info:
____________________________
If you have another color, see http://www.imagemagick.org/Usage/color/#opaque for replacing colors. For example the following should work to make the blue color into white and everything else black:
convert logo: -fill black +opaque "rgb(2,90,164)" -fill white -opaque "rgb(2,90,164)" tmp.png
This does not seem to work in IM 6.4.1-3 and has been reported as a bug and should be fixed in IM 6.4.1-4
Re: Detect how much white color a picture has?
Hi,
fmw42> convert logo: -threshold 99% -format "%[fx:100*image.mean]" info:
THANKS!!!
-Paavo
fmw42> convert logo: -threshold 99% -format "%[fx:100*image.mean]" info:
THANKS!!!
-Paavo
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: Detect how much white color a picture has?
-threshold 99% will not catch just white, but anything which is within 1% of white.
You are also not taking into accound that threshold thresholds each channel separateally. which is okay if your image is grayscale to start with
Instead use -colorspace Gray -negate -threshold 0 -negate.
If you images are not grayscale, then even this will not catch a pixel that is one
single bit distance from pure white.
To do it properly for ANY image, you need to neaget and add the channels together
before thresholding.
This is talked about in thresholding 'difference' images. in the new examples on image comparison methods. though in that case you are trying to threshold pure black, rather than pure white (and thus the added negates above).
You are also not taking into accound that threshold thresholds each channel separateally. which is okay if your image is grayscale to start with
Instead use -colorspace Gray -negate -threshold 0 -negate.
If you images are not grayscale, then even this will not catch a pixel that is one
single bit distance from pure white.
To do it properly for ANY image, you need to neaget and add the channels together
before thresholding.
Code: Select all
convert image.png -negate -separate +channel \
-compose plus -background black -flatten \
-threshold 0 -negate white_mask.png
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/