Page 1 of 1

pixel count

Posted: 2011-08-16T09:25:55-07:00
by boarders' paradise
hi. sorry, newbie question: How can I tell ImageMagick to output the amount of pixels in an image (e.g. not 640*480, but 307200), and only this information (i.e. not identify -verbose rose.jpg)?

Thank you so much.

Re: pixel count

Posted: 2011-08-16T09:56:19-07:00
by fmw42

Re: pixel count

Posted: 2011-08-19T03:23:57-07:00
by webshaker
And is it possible to count pixel that are not transparent ?

Re: pixel count

Posted: 2011-08-19T09:48:19-07:00
by fmw42
webshaker wrote:And is it possible to count pixel that are not transparent ?

all pixels:

convert logo: -format "%[fx:w*h]" info:
307200

create image with transparency:

convert logo: -transparent white logot.png

opaque pixels -- get alpha channel which is binary, then get the mean in the range 0 to 1 and multiply by the w*h:

convert logot.png -alpha extract -format "%[fx:round(mean*w*h)]" info:
50956

Re: pixel count

Posted: 2011-08-19T18:45:13-07:00
by anthony
WARNING using mean like this may not get an exact count as images get larger. Though I am not certain at what size it will start to become inaccurite. Better idea, extract the boolean alpha channel, and get the histogram. That will get exact counts.

Re: pixel count

Posted: 2011-08-19T20:00:51-07:00
by fmw42
anthony wrote:WARNING using mean like this may not get an exact count as images get larger. Though I am not certain at what size it will start to become inaccurite. Better idea, extract the boolean alpha channel, and get the histogram. That will get exact counts.

Why should the mean be affected by the image size? I don't understand that? I am making a binary image from the alpha channel. The mean will always be between 0 and 1 depending upon the amount of white vs. black. So multiplying the mean*w*h should always give the pixel count within 1 due to fractional part of the mean and how you rounded or truncated or whatever to the nearest whole number the product.

However, getting the histogram counts of white vs black in the binary mask is perhaps a better and more reliable way, but it requires some unix to separate the counts from the histogram, though not very complex.


convert logot.png -alpha extract -format %c histogram:info: | grep white | sed -n 's/^ *\(.*\):.*$/\1/p'
50956

Same number as my technique above.

Re: pixel count

Posted: 2011-08-22T00:53:59-07:00
by anthony
Because mean is just a floating point number and when you get a image with a large number of pixels it starts to get iffy about getting back an exact number. For example 3 pixels out of 9 or 1/3 does not store perfectly in a floating point number, and eventually with very large numbers that inaccuracy takes it toll, and you don't get the exact count. It happened to a couple of users and as such it not a accurate method.

Re: pixel count

Posted: 2011-08-22T10:21:25-07:00
by fmw42
anthony wrote:Because mean is just a floating point number and when you get a image with a large number of pixels it starts to get iffy about getting back an exact number. For example 3 pixels out of 9 or 1/3 does not store perfectly in a floating point number, and eventually with very large numbers that inaccuracy takes it toll, and you don't get the exact count. It happened to a couple of users and as such it not a accurate method.

OK, thanks for the explanation. As you saw above, even the fraction from the produce means there is at least the possibility of a one pixel error depending upon round() or trunc(), etc.

I agree your method is more reliable, but then requires some post processing in unix (or the equivalent in Windows)