Contrast stretching transformation in Image Processing

I tried to solve for question but i didn't do.
I tried this code for solving.
p1=[0,0];
p2=[75,5];
p3=[140,250];
p4=[255,255];
m1=(p1(1,2)-p2(1,2))/(p1(1,1)-p2(1,1));
m2=(p2(1,2)-p3(1,2))/(p2(1,1)-p3(1,1));
m3=(p3(1,2)-p4(1,2))/(p3(1,1)-p4(1,1));
c1=p1(1,2)-m1*p1(1,1);
c2=p2(1,2)-m2*p2(1,1);
c3=p3(1,2)-m3*p3(1,1);
%% Transformation function
t=[];
for x=0:255
if(x<=p2(1,1))
t=[t (m1*x+c1)];
end
if(x>p2(1,1) && x<=p3(1,1))
t=[t (m2*x+c2)];
end
if(x>p3(1,1) && x<=p4(1,1))
t=[t (m3*x+c3)];
end
end
for n=1:s(1,1)
for m=1:s(1,2)
ot(n,m)=t(a(n,m)+1);
end
end
I don't know how to find answer of this question. I will be glad if you help.

1 Comment

I found answer is 130 but i'm not sure.
I tried this:
r = input("Please input the intensity value, r: ");
fprintf("\n");
r1 = 70;
r2 = 150;
s1 = 30;
s2 = 230;
Point_1 = [0 0];
Point_2 = [r1 s1];
Point_3 = [r2 s2];
Point_4 = [255 255];
x = 1;
y = 2;
%Slope of region 1%
Rise = Point_2(y) - Point_1(y);
Run = Point_2(x) - Point_1(x);
Slope_1 = Rise/Run;
%Slope of region 2%
Rise = Point_3(y) - Point_2(y);
Run = Point_3(x) - Point_2(x);
Slope_2 = Rise/Run;
%Slope of region 3%
Rise = Point_4(y) - Point_3(y);
Run = Point_4(x) - Point_3(x);
Slope_3 = Rise/Run;
if r <= r1
fprintf("Region 1\n");
s = Slope_1*r;
fprintf("The output intensity, s = T(r) is: %.2f\n",s);
elseif (r1 < r) && (r < r2)
fprintf("Region 2\n");
Constant_Offset = s1;
Relative_Run = (r - r1);
s = Slope_2*Relative_Run + Constant_Offset;
fprintf("The output intensity, s = T(r) is: %.2f\n",s);
elseif r >= r2
fprintf("Region 3\n");
Constant_Offset = s2;
Relative_Run = (r - r2);
s = Slope_3*Relative_Run + Constant_Offset;
fprintf("The output intensity, s = T(r) is: %.2f\n",s);
end

Sign in to comment.

 Accepted Answer

Wow. Try this much easier way:
r = [70, 150]
s = [30, 230];
output = interp1(r, s, 110) % output is 130.

4 Comments

Sir, you are professional in matlab .Thanks you again.
If this solved the question, can you "Accept this answer" unless you want to wait for possible other, better answers.
Excuse me,
Sir, i accepted your answer.
I sometimes forget it.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!