Count fraction of pixels above or below certain value

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
hwttdz
Posts: 22
Joined: 2010-08-29T05:29:08-07:00
Authentication code: 8675308

Count fraction of pixels above or below certain value

Post by hwttdz »

Assuming I have a grayscale image is there a good way of counting the fraction of pixels with a value above or below a cutoff value? I'm planning on determining if I am clipping on either end of the histogram, but I'm not quite sure how to proceed. My thought it maybe to apply a curve operator which sends values below the threshold to zero and above to 1 (or 255 if you'd rather think of it that way) and then getting the average color of the image.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Count fraction of pixels above or below certain value

Post by fmw42 »

threshold the image so that the pixels you want to exclude are white and pixels you want to include are black. then get the mean value in the range of 0 to 1 and then multiply by 100.

once you threshold the image (see -threshold, -black-threshold, -white-threshold), you can compute the fraction or percent via

convert thresholdedimage -format "%[fx:mean]" info:

that will give you the mean in the range of 0 to 1 i.e. a fraction

or

convert thresholdedimage -format "%[fx:100*mean]" info:

this will give it to you as a percent.


see
http://www.imagemagick.org/script/fx.php
http://www.imagemagick.org/Usage/transform/#fx_escapes

see
http://www.imagemagick.org/script/comma ... #threshold
http://www.imagemagick.org/script/comma ... -threshold
http://www.imagemagick.org/script/comma ... -threshold

you can threshold by value (in your IM compile Q range -- Q8 values are 0 to 255, Q16 values are 0 to 65535) or by percent of Q value (100% is equivalent to 255 for Q8 or 65535 for Q16)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Count fraction of pixels above or below certain value

Post by fmw42 »

here is a trivial example where I take the lower 20% and the highest 10% from a grayscale gradient and threshold those regions to white and make the rest black. then I get the mean as a faction which should result in 0.3


convert -size 100x100 gradient: grad100.png
convert grad100.png -black-threshold 20% \
-fill white -opaque black \
-white-threshold 90% \
-fill black +opaque white \
grad100_thresh.png
convert grad100_thresh.png -format "%[fx:mean]" info:
0.3


P.S. There is a function called -contrast-stretch that will actually clip the histogram on either or both ends by count of pixels or percent count of pixels. You might want to look at that. see http://www.imagemagick.org/script/comma ... st-stretch
Post Reply