Thilina's Blog

I might be wrong, but…

Contrast Stretching for Gray Scale images with MATLAB

When studying “Introduction to Image Processing and Machine vision” and “Bio Medical Engineering and Instrumentation” for the end semester exam, I read a small section related to those two modules which can be easily implemented using MATLAB. “Contrast Stretching” is a simple piecewise linear transformation function which expands the range of intensity of the image to user defined span.

Simple mathematical illustration of the process is as below.


Considering Line L1;                             clip_image004

Considering Line L2;                             clip_image006

Considering Line L3;                             clip_image008

Therefore the piecewise transform function can me defined as follows;


When jumping to the implementation on MATLAB I used 4 sliders to define the x1, x2, y1 and y2 values and displayed the resulting line with the aid of plot function. And they are converted to plot and finally for modified the image output as follows;


% Thilina S. Ambagahawaththa
% 2011-03-23
% breaking points from gui
x1 = floor(get (x_1,'Value'));
x2 = floor(get (x_2,'Value'));
y1 = floor(get (y_1,'Value'));
y2 = floor(get (y_2,'Value'));

% range definitions
x_r1 = 0:x1;
x_r2 = x1:x2;
x_r3 = x2:255;

% line gradients
a1 = y1/x1;
a2 = (y2-y1)/(x2-x1);
a3 = (255-y2)/(255-x2);

% line functions
yo_1 = floor(a1*x_r1);
yo_2 = floor(y1 + (a2*(x_r2-x1)));
yo_3 = floor(y2 + (a3*(x_r3-x2)));

% line concatance
y = [yo_1 yo_2 yo_3];

% plot line
xlim([0 255]);
ylim([0 255]);

try % if image is loaded

    % mask images for colour intensity regions
    mask_1 = double(imageIn<=x1);
    mask_2 = double((imageIn>x1)&(imageIn<x2));
    mask_3 = double(imageIn>=x2);

    % contrast stretching in regions
    im1 = mask_1.*floor(a1*imageIn);
    im2 = mask_2.*floor(y1 + (a2*(imageIn-x1)));
    im3 = mask_3.*floor(y2 + (a3*(imageIn-x2)));

    % concatance of output image
    imageOut = cast(im1+im2+im3,'uint8');

    % show output image

    % image histogram generation
    histo1 = log10(imhist(imageOut));
    histo2 = log10(imhist(cast(imageIn,'uint8')));
    hgrm = [histo1,histo2];
    xlim([0 255]);

catch e %image not loaded


Following Image demonstrates a small application written using above technique, the test image is downloaded from this link via google search and you can directly download image from here.


Thank you for reading ..!

2011 March 23 - Posted by | Image Processing, MATLAB


  1. it realy nice…….
    thanks for help………

    Comment by saroj hatheele | 2011 August 9 | Reply

    • Thank you very much for the comment .!

      Comment by Thilina S. | 2011 August 9 | Reply

  2. I can’t show the image to the axes which you showed in Filtered Image like you. Can you help me?

    Comment by Hoang | 2011 December 16 | Reply

  3. That’s great, Yours blog relating matlab image processing are really amazing.

    Comment by bakhtazam | 2012 October 13 | Reply

  4. what is fg of the line subplot(1,1,1,’Parent’,fg);

    Comment by Sumii | 2012 November 10 | Reply

    • Parent zurgiin format ni yum bishuu suumii

      Comment by Tulgaa | 2012 November 12 | Reply

  5. may i have ur email plz, i need ur help with my program

    Comment by Iora Omair | 2013 March 5 | Reply

  6. thank’s

    Comment by sangwidyimoezt | 2013 June 26 | Reply

  7. Hi, how to get value x1, x2 y1, y2. Is it we just put any value as long as x1 > x2 and y1 . y2?

    Comment by zaidi | 2014 October 27 | Reply

  8. I guess the straight line equations are not proper. We should follow (y-y1)=slope(x-x1) concept. In line2 equation y should be equal to y2 when x is x2 that means y = ((y2-y1)/(x2-x1))*(x-x1) + y1. and line3 equation should be y = ((255-y2)/(255-x2))*(x-x2) + y2.

    Comment by raja | 2015 April 23 | Reply

  9. subplot(1,1,1,’Parent’,fg); …… I don’t understand this line.. what is meant by fg and parent… can any one help me in this regard…. can any one send me the exact commond.?? Thanks.

    Comment by decci | 2015 May 12 | Reply

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: