Clear Filters
Clear Filters

how to find mean of a histogram?

4 views (last 30 days)
nurul hafizah
nurul hafizah on 8 May 2015
Commented: Walter Roberson on 8 May 2015
first I want to ask whether it is same between the mean of histogram and mean of matrix.
i want to find mean of histogram which of this coding is correct?
coding 1
R=rgb1(:,:,1);
I2=histeq(R);
pixel_values_R=single(I2);
mean_r=mean(pixel_values_R(:))
coding 2
[counts_R,binsLocation_R]= imhist(R);
image_mean_R=sum(counts_R.*binsLocation_R)/sum(counts_R)
thank you in advance
  1 Comment
Walter Roberson
Walter Roberson on 8 May 2015
What is the data type of the image? Is it uint8() or is it a different integer type or is it double precision?

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 8 May 2015
The mean of the matrix will of course be the most accurate. If you take the mean of the histogram, it will assume that all elements in that bin have the value of the bin center, which is, of course, not true in general.
  3 Comments
Image Analyst
Image Analyst on 8 May 2015
True. Of course with his code, he's not even taking the histogram of the same thing! In "coding 1" he's taking the histogram of the histogram equalized image I2 whereas in "coding 2" he's taking the histogram of the original image. So, while the bins will have the same height (counts), they are shifted to a new gray level location and so the mean will change.
As a side note, there is a function mean2() that the poster may want to know about so you don't have to do (:), though that probably doesn't add much time.
Side note 2: histogram equalization is rarely needed and often produces unnatural-looking images. They can be a lot worse than just doing a simple linear stretch with imadjust() or mat2gray(). I've never needed to use a global histogram equalization in almost 4 decades of developing image analysis algorithms.
Walter Roberson
Walter Roberson on 8 May 2015
I wasn't going to try to explain the evils of taking the mean of the histogram equalized image until the poster put a bit more effort into the question.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!