Automatic Image Level Adjustment

6 views (last 30 days)
Matej Znidaric
Matej Znidaric on 20 May 2020
Answered: DGM on 6 Jul 2022
Hi everyone!
I would be really thankful if you could solve the following problem. I've transformed image from RGB to cieLAB and then extracted a channel. How can I automatically adjust the tonal range of the a channel like in Adobe Photoshop?
A = imread('image.jpg');
CT = makecform('srgb2lab');
lab = applycform(A,CT);
a = lab(:,:,2);
%????? --> how to automatically adjust the tonal range of a channel?

Answers (1)

DGM
DGM on 6 Jul 2022
A simple linear "Levels" type adjustment tool is exactly what imadjust() provides. In the Photoshop UI, you have five parameters controlled by five sliders:
  • input levels (low and high)
  • gamma (the center handle)
  • output levels (low and high)
IPT imadjust() allows you to explicitly specify input and output levels along with gamma.
% a test image
A = imread('pout.tif');
% show the image and its histogram
subplot(1,2,1); imshow(A);
subplot(1,2,2); imhist(A);
The levels can be explicitly set:
% manually adjust the image (input levels)
B = imadjust(A,[0.2 0.8]);
% show the image and its histogram
figure
subplot(1,2,1); imshow(B);
subplot(1,2,2); imhist(B);
... or the levels can be automatically determined by using stretchlim() and feeding the result to imadjust(). The tolerance used by stretchlim() can be optionally specified if desired.
% automatically adjust the image (input levels)
inrange = stretchlim(A,0.005) % saturate 1% of pixels (default is 2%)
inrange = 2×1
0.3020 0.6863
C = imadjust(A,inrange);
% show the image and its histogram
figure
subplot(1,2,1); imshow(C);
subplot(1,2,2); imhist(C);
For grayscale images like the above, it's not strictly necessary to call stretchlim() directly, unless it's desired to use a non-default tolerance. The short syntax for imadjust() will internally call stretchlim() (with the default 2% tolerance).
% automatically adjust the image (input levels)
D = imadjust(A); % this syntax only works on single-channel images
% show the image and its histogram
figure
subplot(1,2,1); imshow(D);
subplot(1,2,2); imhist(D);

Community Treasure Hunt

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

Start Hunting!

Translated by