Page 1 of 1

Count fraction of pixels above or below certain value

Posted: 2011-06-22T17:40:14-07:00
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.

Re: Count fraction of pixels above or below certain value

Posted: 2011-06-22T17:50:13-07:00
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)

Re: Count fraction of pixels above or below certain value

Posted: 2011-06-22T18:07:02-07:00
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