# Thilina's Blog

## 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;

Considering Line L2;

Considering Line L3;

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;

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
subplot(1,1,1,'Parent',fg);
plot(y);
xlim([0 255]);
ylim([0 255]);

try % if image is loaded

% mask images for colour intensity regions

% contrast stretching in regions

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

% show output image
subplot(1,1,1,'Parent',ck);
imshow(imageOut);

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

end

```

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

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