-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRotate.m
46 lines (41 loc) · 855 Bytes
/
Rotate.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
function [image] = Rotate(image)
[x,y] = size(image);
for j = 1:1:y
upper = 0;
lower = 0;
i = 1;
while( upper==0 && i<=x )
if(image(i,j)==0)
upper = i;
break;
end
i = i + 1;
end
i=x;
while( lower==0 && i>=1 )
if(image(i,j)==0)
lower = i;
break;
end
i = i - 1;
end
if (upper==0 && lower==0)
result = x/2;
else
result = (upper+lower)/2;
end
middlepoint(j) = result;
end
%for i=1:1:y %outputs the middle point
% middlepoint(i)
%end
C(1:y)=(1:y);
p = polyfit( C, middlepoint,1);
f = polyval(p,C);
hold on
%plot(C, f,'--r'); plots the line
%plot(C, middlepoint,'--r'); plots the middle point
angle = atan((f(y)-f(1))./(y-1));
angle2 = radtodeg(angle);
image = imrotate(image,angle2 );
end