pixel count

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
boarders' paradise
Posts: 14
Joined: 2008-12-01T17:42:18-07:00

pixel count

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: pixel count

Post by fmw42 »

webshaker
Posts: 16
Joined: 2007-07-25T15:42:25-07:00

Re: pixel count

Post by webshaker »

And is it possible to count pixel that are not transparent ?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: pixel count

Post 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
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: pixel count

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: pixel count

Post 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.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: pixel count

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: pixel count

Post 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)
Post Reply