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

조회 수: 5 (최근 30일)
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 (^_^)

채택된 답변

Image Analyst
Image Analyst 2012년 6월 23일
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.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2012년 6월 23일
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)

카테고리

Help CenterFile Exchange에서 Modify Image Colors에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by