-
Notifications
You must be signed in to change notification settings - Fork 2
/
find_corners.m
165 lines (148 loc) · 4.08 KB
/
find_corners.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
%%find_corners function from Spring 2012~2013 project Pool Table Edge,
%%Pocket and Ball Position Estimation.
function corners = find_corners(picture_edges)
index = 1;
picture_label=bwlabel(picture_edges,4);
STATS=regionprops(picture_label,'Area','Centroid','BoundingBox');
area=cat(1,STATS.Area);
centroid=cat(1,STATS.Centroid);
boundingbox=cat(1,STATS.BoundingBox);
cent=centroid(1,:);
bound=boundingbox(1,:);
vertex(1,1)=cent(1)-bound(3)/2;
vertex(1,2)=cent(2)-bound(4)/2;
vertex(2,1)=cent(1)+bound(3)/2;
vertex(2,2)=cent(2)-bound(4)/2;
vertex(3,1)=cent(1)-bound(3)/2;
vertex(3,2)=cent(2)+bound(4)/2;
vertex(4,1)=cent(1)+bound(3)/2;
vertex(4,2)=cent(2)+bound(4)/2;
start=round((vertex(3,2)-vertex(1,2))./4+vertex(1,2));
stop=round((vertex(3,2)-vertex(1,2)).*3/4+vertex(1,2));
picture_zero=picture_edges*0;
for y=start:stop
x=1;
% looks for binarized white edges of table
while (picture_edges(y,x)==0)
x=x+1;
end
% saves edges in arrays
picture_zero(y,x)=1;
end
% Hough transform
[H,theta,rho]=hough(picture_zero,'Theta',-90:0.1:89.999);
peaks=houghpeaks(H,1);
t=theta(peaks(1,2))*pi/180;
r=rho(peaks(1,1));
if tan(t)==0
slope=99999;
else
slope=-1./tan(t);
end
if sin(t)==0
intercept=-r(index)*slope;
else
intercept=r./sin(t);
end
slope_left=slope;
intercept_left=intercept;
start=round((vertex(4,2)-vertex(2,2))./4+vertex(2,2));
stop=round((vertex(4,2)-vertex(2,2)).*3/4+vertex(2,2));
picture_zero=picture_edges*0;
for y=start:stop
x=size(picture_edges,2);
% looks for binarized white edges of table
while (picture_edges(y,x)==0)
x=x-1;
end
% saves edges in arrays
picture_zero(y,x)=1;
end
% Hough transform
[H,theta,rho]=hough(picture_zero,'Theta',-90:0.1:89.999);
peaks=houghpeaks(H,1);
t=theta(peaks(1,2))*pi/180;
r=rho(peaks(1,1));
if tan(t)==0
slope=99999;
else
slope=-1./tan(t);
end
if sin(t)==0
intercept=-r(index)*slope;
else
intercept=r./sin(t);
end
slope_right=slope;
intercept_right=intercept;
start=round((vertex(2,1)-vertex(1,1))./4+vertex(1,1));
stop=round((vertex(2,1)-vertex(1,1)).*3/4+vertex(1,1));
picture_zero=picture_edges*0;
for x=start:stop
y=1;
% looks for binarized white edges of table
while (picture_edges(y,x)==0)
y=y+1;
end
% saves edges in arrays
picture_zero(y,x)=1;
end
% Hough transform
[H,theta,rho]=hough(picture_zero,'Theta',-90:0.1:89.999);
peaks=houghpeaks(H,1);
t=theta(peaks(1,2))*pi/180;
r=rho(peaks(1,1));
if tan(t)==0
slope=99999;
else
slope=-1./tan(t);
end
if sin(t)==0
intercept=-r(index)*slope;
else
intercept=r./sin(t);
end
slope_top=slope;
intercept_top=intercept;
start=round((vertex(4,1)-vertex(3,1))./4+vertex(3,1));
stop=round((vertex(4,1)-vertex(3,1)).*3/4+vertex(3,1));
picture_zero=picture_edges*0;
for x=start:stop
y=size(picture_edges,1);
% looks for binarized white edges of table
while (picture_edges(y,x)==0)
y=y-1;
end
% saves edges in arrays
picture_zero(y,x)=1;
end
% Hough transform
[H,theta,rho]=hough(picture_zero,'Theta',-90:0.1:89.999);
peaks=houghpeaks(H,1);
t=theta(peaks(1,2))*pi/180;
r=rho(peaks(1,1));
if tan(t)==0
slope=99999;
else
slope=-1./tan(t);
end
if sin(t)==0
intercept=-r(index)*slope;
else
intercept=r./sin(t);
end
slope_bottom=slope;
intercept_bottom=intercept;
corner_upper_left(1)=(intercept_left-intercept_top)./(slope_top-slope_left);
corner_upper_left(2)=slope_top.*corner_upper_left(1)+intercept_top;
corner_upper_right(1)=(intercept_right-intercept_top)./(slope_top-slope_right);
corner_upper_right(2)=slope_top.*corner_upper_right(1)+intercept_top;
corner_lower_left(1)=(intercept_left-intercept_bottom)./(slope_bottom-slope_left);
corner_lower_left(2)=slope_bottom.*corner_lower_left(1)+intercept_bottom;
corner_lower_right(1)=(intercept_right-intercept_bottom)./(slope_bottom-slope_right);
corner_lower_right(2)=slope_bottom.*corner_lower_right(1)+intercept_bottom;
corners = [corner_upper_left(1),corner_upper_left(2);...
corner_upper_right(1),corner_upper_right(2);...
corner_lower_left(1),corner_lower_left(2);
corner_lower_right(1),corner_lower_right(2)];
end