# GUIDE brightness/contrast function explained

7 views (last 30 days)
Ivan Tanczos on 13 May 2021
Edited: DGM on 9 May 2022
Hi,
I caame across a function for my MATLAB GUIDE program in which i adjust the contrast and brightness of my image using sliders. The function works perfectly however i dont seem to understand the function at all, could anyone explain the lines of code below? Thanks a lot.
function I = filterImage(I, brightness, contrast)
I = double(I);
% Normalize
Imin = min(I(:));
Imax = max(I(:));
I = (I-Imin)/(Imax-Imin);
% Apply contrast filter
I = I.^(contrast+1.02);
% Undo normalization
I = I*(Imax-Imin) + Imin;
% Normalize again
Imin = min(I(:));
Imax = max(I(:));
I = (I-Imin)/(Imax-Imin);
% Apply brightness filter
I = I + brightness;
% Undo normalization
I = I*(Imax-Imin) + Imin;
end

DGM on 9 May 2022
Edited: DGM on 9 May 2022
I wouldn't say the code "works perfectly", since it doesn't do what is implied that it does -- though in its defense, its lack of documentation means that it makes no actual explicit claims of purpose or behavior.
First off, this isn't a brightness and contrast tool. It's a brightness and gamma tool. Could you say that gamma adjustment changes contrast? Sure, but you can also say that brightness adjustment changes contrast or that drawing a smiley face on the image changes contrast. You wouldn't call either of those "contrast adjustment".
Second, the parameterization of the gamma adjustment has some nonsense arbitrary offset. I have a feeling that this was copied out of some other code wherein gamma was controlled by a linearized parameter that has been omitted from this implementation.
Third, there's a whole bunch of pointless obfuscation going on with normalizing and denormalizing for no reason. The image is nomalized with respect to its extrema instead of its nominal data range, so the transformation (the gamma adjustment) is going to be with respect to those extrema instead of with respect to the class-specific black and white values as they normally would be.
To further the ridiculousness of it all, the fact that gamma adjustment is done with respect to extrema instead of with respect to nominal data range means that the extreme range of data in the image cannot change unless the brightness adjustment causes truncation. For certain interpretations of the word "contrast", that particular restriction means that this "contrast filter" literally cannot change the image contrast.
% this whole part can be replaced by im2double(I)
I = double(I);
% Normalize
Imin = min(I(:));
Imax = max(I(:));
I = (I-Imin)/(Imax-Imin);
% this isn't a "contrast filter"
% Apply contrast filter
I = I.^(contrast+1.02);
% this whole mess does literally nothing
% Undo normalization
I = I*(Imax-Imin) + Imin;
% Normalize again
Imin = min(I(:));
Imax = max(I(:));
I = (I-Imin)/(Imax-Imin);
% this is fine
% Apply brightness filter
I = I + brightness;
% this denormalization will rescale the image to the correct range
% for the original class, but it won't actually cast the output
% back to the same class, so the output will likely be incorrectly
% scaled for its class.
% Undo normalization
I = I*(Imax-Imin) + Imin;
Okay, so enough digging on that. We've all made mistakes, and we've all written things that were built on misunderstandings. So if you did want to adjust brightness and contrast in a simple fashion, how would you do it?
Here's a handful of ideas:
The brightness/contrast routine roughly described in that answer is what is used (in part) in MIMT imbcg(). If all you need is a simple brightness/contrast tool as you'd find in an image editor, that should suffice. If you want to know how to write something that does the same thing, just open it up and read it.
B = imbcg(A,'b',0.1,'c',0.3);
imshow([A B]) 