How to get pixelcount in image.

I tried with [pixel-count x]=imhist(i), i is gray scale image but I got pixel-count=256xdouble like dis not value. Please help me with this, matlab code.

 Accepted Answer

i is the imaginary number and would be an especially horrible name for an image. Change it.
Your subject line says "How to get pixelcount in image." and the answer to that, for a gray scale image, is
numberOfPixels = numel(grayImage);
Then you say "[pixel-count x]=imhist(i)" Well pixel-count is not even a valid variable name. You can't have a minus sign in the middle of a variable name. Perhaps it was a type, like using "dis" for "this". Maybe you meant
[pixelCounts, grayLevels] = imhist(grayImage);
In that case pixelsCounts is the number of pixels in each gray level bin of the histogram, NOT the total number of pixels in the image, which would be the sum of the pixel counts in each bin:
numberOfPixels = sum(pixelCounts)

4 Comments

Meangl=sum(graylevels.*pixelcounts) /Numofpixels,here pixelcount value how it is taken there is separate pixelcount for each bin, please explain me its operation.
Yes, that's the formula for mean gray level based on the quantized gray levels in the bins. let's say there were 3 bins with gray levels 10, 20, and 50, with counts 10, 5, and 2. So the total integrated gray level is 10*10 + 20*5 + 50*2 = 300. Then divide by (10+5+2) = 17. So the mean gray level is 300/17 = 17.647. It's just the standard weighted mean formula mean = sum(n*g)/sum(n). Note that the n sort of cancels out and you have units of g (gray level).
Thank you sir
Hello,
I want to find the number of pixels on the right handside or the left hand side of an image how do i do that
Thank you,

Sign in to comment.

More Answers (1)

What pixel count?
imhist returns the number of pixels at each of 256 different gray levels. If you want more or less intensity levels, simply specify a different number of bins rather than using the default.
If you want to know the total number of pixels in the image, it's simply height*width, i.e.
numpixels = size(i, 1) * size(i, 2); %and you shouldn't call your image i.

2 Comments

What is meant by pixelcount?can you explain with one example.
This is the exact question I asked, what do you mean by pixel count? Pixel count of what?

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!