How to get an image contrast value?

56 views (last 30 days)
Dear Matlab community,
I am working right now on my masters thesis and for this I have to analyze pictures taken with a special microscope. These pictures are mainly in a red/green color plane. Therefore I have segmented the images in RGB-Colormaps and converted them to a gray scale in different subplots.
But here is my problem: I would like to automatically choose the subplot with the highest contrast for further analysis. Is there any way to maybe compare the contrast values of the images and then the code choses the image with the highest contrast?
Greetings from Germany and thank you in advance!
A desperate student

Accepted Answer

Madhav Thakker
Madhav Thakker on 22 Sep 2020
Hi Denis,
I understand that you want to calculate the contrast of your gray scale images. You can use a very simple max - min definition as taken from https://www.mathworks.com/matlabcentral/answers/231214-how-to-find-the-contrast-of-a-image-in-matlab#answer_187225.
image_contrast = max(grayImage(:)) - min(grayImage(:));
You can compute the contrast for all different subplot images and choose image with the highest contrast by using a switch loop.
Hope this helps.
  3 Comments
mary john
mary john on 29 Nov 2023
Hi.
What should be the data type of the grayImage?
Also since the double format may have negative values, the answer of image_contrast will be more than 1. Please advice.
Kind regards
Mary
DGM
DGM on 29 Nov 2023
If you're expecting the result to be nominally unit-scale, then it's probably expected that the image data is also unit-scale (i.e. floating point class). Otherwise, you'd need to normalize the result with respect to the nominal range implied by the class.
Au8 = imread('cameraman.tif'); % uint8
Ai16 = im2int16(Au8); % int16
Ad = im2double(Au8); % double
% extreme spread normalized WRT class
% range() is the same as max()-min()
% do normalization first
fk = @(x) range(im2double(x),'all');
k = fk(Au8)
k = 0.9647
k = fk(Ai16)
k = 0.9647
k = fk(Ad)
k = 0.9647
% ... or do normalization after
fk = @(x) range(double(x),'all')/diff(getrangefromclass(x));
k = fk(Au8)
k = 0.9647
k = fk(Ai16)
k = 0.9647
k = fk(Ad)
k = 0.9647
If you get contrast figures larger than 1 because you have data that's out of gamut, well then either that's what the contrast is, or if OOG data is irrelevant, then truncate it before calculating the contrast.
Aoog = Ad*1.2-0.1;
imrange(Aoog) % some data is OOG
ans = 1×2
-0.0671 1.0906
Aclamped = imclamp(Aoog); % truncate
imrange(Aclamped)
ans = 1×2
0 1
... or you could solve that by normalizing with respect to the data extrema. That all depends on what your data is, why it's negative, how much of it's negative, and whether the negative content is meaningful, or whether overall continuity is more important than scale.

Sign in to comment.

More Answers (0)

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!