Contrast stretching??? this pic

Answers (1)

Let's start with the basic approach using stretchlim() and imadjust(). In this case, stretchlim() returns the limits that will remap the extrema of the source to black and white -- or more accurately, it will remap the intensity values such that the bottom and top 1% of pixels are clipped. This is done on all three channels independently; consequently, there is a noticeable color shift, and the brightest and darkest regions shift toward pure white and black.
inpict = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/388723/image.jpeg');
limits = stretchlim(inpict,0.01);
outpict = imadjust(inpict,limits);
imshow([inpict; outpict])
Alternatively, one can try to reduce the independent limits returned by stretchlim() to a single set of limits that can be applied uniformly to all three channels. In this case, I just chose to use the mean, but one could also use the inner or outer extrema. Note that there isn't as much of a hue shift, though there might be a slight shift in saturation.
inpict = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/388723/image.jpeg');
limits = mean(stretchlim(inpict,0.01),2);
outpict = imadjust(inpict,limits);
imshow([inpict; outpict])
Of course, one doesn't have to do this in RGB. In this example, L is stretched in LAB.
inpict = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/388723/image.jpeg');
outpict = rgb2lab(inpict);
limits = stretchlim(outpict(:,:,1)/100,0.01);
outpict(:,:,1) = imadjust(outpict(:,:,1)/100,limits)*100;
outpict = im2uint8(lab2rgb(outpict));
imshow([inpict; outpict])
There's nothing saying that you have to adhere to those limits either. You can adjust the input endpoints to whatever you want:
inpict = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/388723/image.jpeg');
limits = [0.2; 0.8];
outpict = imadjust(inpict,limits);
imshow([inpict; outpict])

Asked:

on 22 Oct 2020

Answered:

DGM
on 23 Mar 2022

Community Treasure Hunt

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

Start Hunting!