2

How to take average gray value of an image?

2 Answers 2

7

Your question is very vague, but if you mean that you want the arithmetic mean of a greyscale image:

img = imread('my_greyscale_image.bmp');
img_mean = mean(mean(img));

Note that the reason you need to apply mean twice is because the first time mean is applied to a matrix and it calculates the means of each column and returns these values as a vector. Applying mean again to this vector then gives you a single mean value for the whole matrix.

4
  • 5
    When doing statistics on the entire image, I find that it is better to stretch the image into a vector and then do the operation, i.e. img_mean = mean(img(:)); For the mean it does not matter, but for non-linear operations like the std and median it is important.
    – Ghaul
    Apr 1, 2011 at 9:34
  • @Ghaul: good point - it would be more succinct (and probably more efficient) for linear operations such as mean too.
    – Paul R
    Apr 1, 2011 at 9:49
  • ok now what if i want to apply this average value on another image .mean if i want to multiply it with the image than....how to do it?????????
    – chee
    Apr 5, 2011 at 20:51
  • @chee: if you have a new question then you should probably post it as a separate question on SO. It sounds though like you just want to know how to multiply a matrix by a scalar value in Matlab ?
    – Paul R
    Apr 5, 2011 at 20:53
1

Straight from the manual (http://www.mathworks.com/help/toolbox/images/ref/rgb2gray.html):

I = imread('color_image.tif');
J = rgb2gray(I);
figure, imshow(I), figure, imshow(J);
2
  • The question is vague, but I think he wants to calculate the mean value of a greyscale image, not convert an RGB image to a greyscale image.
    – Paul R
    Apr 1, 2011 at 8:28
  • Gamma correction may also be needed to get a realistic average: 4p8.com/eric.brasseur/gamma.html
    – user57368
    Apr 1, 2011 at 9:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.