%cameraman image contrast_stretching_grayimage_
clc;
clear all;
close all;
%defining points
r1=120; s1=60;
r2=150; s2=130;
%slope
m1 =(s1/r1);
m2 =((s2-s1)/(r2-r1));
m3 =((255-s2)/(255-r2));
%equations of line
k=0:r1;
y1=m1*k;
plot(k,y1);
hold on;
k = r1: r2;
y2 = m2*(k - r1) + m1*r1;
plot (k,y2);
k = r2:255;
y3 = m3*(k-r2) + m2*(r2-r1)+ m1*r1;
plot (k,y3);
xlim([0 255]);
ylim([0 255]);
xlabel('input gray level, r');
ylabel('output gray level, s');
title('Form of transformation');
hold on;
f=imread('cameraman.tif');
[m, n] = size (f);
%contrast stretching
for i = 1:m
for j = 1:n
if((f(i,j)>=0) & (f(i,j)<=r1))
g(i,j) = m1*f(i,j);
else
if((f(i,j)>r1) & (f(i,j)<=r2))
g(i,j) = ((m2*(f(i,j)-r1)+(m1*r1)));
else
if((f(i,j)>r2) & (f(i,j)<=255))
g(i,j) = ((m3*(f(i,j)-r2)+(m2*(r2-r1)+(m1*r1))));
end
end
end
end
end
figure
subplot(221)
imshow(f)
title('actual image')
subplot(222)
imshow(g)
title('contrast-stretched image')
for i = 1:m
for j = 1:n
if((f(i,j)>=0) & (f(i,j)<130))
g(i,j) = 0;
else
g(i,j) = 255;
end
end
end
subplot(223)
imshow(f)
title('actual image')
subplot(224)
imshow(g)
title('contrast-stretched image')