MATLAB: Finding the contrast ratio of a grayscale image

contrastimageImage Processing Toolbox

So as the title says, I want to find the contrast ratio of a grayscale image by Michelson contrast definition. I'm thankful for any help! I'm not sure how to do this. Am I supposed to find the max and min value in each columns or row or the whole matrix?
% Michelson contrast definition
% Imax : the largest value in the image's matrix
% Imin : the smallest value in the image's matrix
contrast = (Imax-Imin)(Imax+Imin)

Best Answer

Do you need loops, like this is a homework question that requires them? If not, just simply do
Imax = max(I(:))
Imin = min(I(:))
contrast = (Imax - Imin) / (Imax + Imin)
Notice that I'm using the correct formula. You are not. They subtract in the numerator, not divide like you have it.