Link to home
Create AccountLog in
Avatar of xenonn
xenonn

asked on

Doing contrast stretch on an image in Matlab

I'm trying to do a contrast stretch on an image in Matlab. I know how contrast stretching work theoretically, but I'm not sure how I can do it in Matlab since I haven't used it before.

I'm told that I need to use a subtraction operation followed by multiplication operation, and need only 2 lines of code. But I don't understand how I should start the code to do a contrast stretch after reading in the image with imread() function.
Avatar of yuk99
yuk99
Flag of United States of America image

Contrast stretching (or normalization) is very simple in MATLAB if you have IMAGE Processing Toolbox. IMADJUST will do everything automatically.

Im = imread(filename);
Im2 = imadjust(Im); %automatic limits
% or
Im2 = imadjust(Im,[low high]) % manual limits


If you want to do it manually, here is the formula on Wikipedia, for example:
User generated image
Let's say you're importing grayscale image with one channel. IMREAD will create uint8 HxW matrix with numbers from 0 to 255. If you want to stratch 70 to 0 and 200 to 255:

Im3 = (Im-70)*(255/(240-70)) + 0;

You can avoid the last summation, of course.

For RGB images, the procedure is similar.

If you still have questions, let me know first the nature of your image. May be you can attach an example here or give us a link.
Avatar of xenonn
xenonn

ASKER

Thanks! Since imread() will create a uint8 matrix, do I have to use imadd() and imsubtract() instead of the usual operators?
ASKER CERTIFIED SOLUTION
Avatar of yuk99
yuk99
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer