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

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 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 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 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]); catch e %image not loaded end

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 ..!

it realy nice…….

thanks for help………

Comment by saroj hatheele | 2011 August 9 |

Thank you very much for the comment .!

Comment by Thilina S. | 2011 August 9 |

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 |

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

Comment by bakhtazam | 2012 October 13 |

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

Comment by Sumii | 2012 November 10 |

Parent zurgiin format ni yum bishuu suumii

Comment by Tulgaa | 2012 November 12 |

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

Comment by Iora Omair | 2013 March 5 |

thank’s

Comment by sangwidyimoezt | 2013 June 26 |

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 |

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 |

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 |