How to get an image contrast value?

조회 수: 87 (최근 30일)
Denis van Egeren
Denis van Egeren 2020년 9월 17일
댓글: DGM 2023년 11월 29일
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

채택된 답변

Madhav Thakker
Madhav Thakker 2020년 9월 22일
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
mary john
mary john 2023년 11월 29일
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 2023년 11월 29일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by