MATLAB: Taking mean value of a gray scaled image and equating it to across images

Image Processing Toolboxmean gray value

I take mean value of an image:
I = imread('1.png'); val = mean2(I)
Because I want to set all images to an equal mean gray value, I need to equate the obtained mean to a specific value (e.g., 128).
Can you help me with this?

Best Answer

Try this:
grayImage = imread('1.png');
meanValue = mean2(grayImage);
% Now make a new image with a desired mean by scaling our current image.
desiredMeanValue = 128; % or whatever.
scaledGrayImage = double(grayImage) * (desiredMeanValue / meanValue);
% scaledGrayImage will be of type double now.
% Either cast it back to uint8, which may change the mean slightly,
% or use [] when you display it
imshow(scaledGrayImage, []);
Related Question