How to find "number of pixels" for a particular "gray level" in using HIST function ?

9 views (last 30 days)
Hi,
I am working on image processing.
In using the hist function, I want to know, how to get the "number of pixels" for a particular "grey level" and also for all the gray level value ?
Please help, Thank you in advance (^_^)

Accepted Answer

Image Analyst
Image Analyst on 23 Jun 2012
For a normal 8 bit (uint8) grayscale image I do this:
[pixelCounts grayLevels] = imhist(grayImage);
It's a lot trickier for double images. Let me know if you have double images - I think I have a demo on that.

More Answers (1)

Walter Roberson
Walter Roberson on 23 Jun 2012
Usually if you want to be testing for exact values you would use histc() instead of hist(), in order to have greater certainty about exactly where the boundaries will go.
In order to get the count for a specific grey level using either hist() or histc() you would have to have adjacent bins that together gather everything that is not that grey level. This is easier to specify in histc() but it can be done in hist() as well, if you are careful.
With either function, assign the output to a variable. The (first) output will be a vector of values, one for each bin. By knowing which bin you put that specific gray level in, index the output vector to get the counts.
Mind you if I was doing this for only one gray level, I would just count them directly:
sum(YourGrayImage(:) == TheGrayLevel)
Sample code using histc:
counts = histc( YourGrayImage(:), 0:255 );
Then the count for gray value G is counts(G+1)

Categories

Find more on Modify Image Colors in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!