-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
357 lines (292 loc) · 11.7 KB
/
main.cpp
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <string>
using namespace cv;
using namespace std;
vector<Vec2f> lines;
void mergeRelatedLines(vector<Vec2f> *lines, Mat &img)
{
vector<Vec2f>::iterator current;
for(current=lines->begin(); current!=lines->end(); current++)
{
if((*current)[0]==0 && (*current)[1]==-100) continue;
float p1 = (*current)[0];
float theta1 = (*current)[1];
Point pt1current, pt2current;
if(theta1>CV_PI*45/180 && theta1<CV_PI*135/180)
{
pt1current.x=0;
pt1current.y = p1/sin(theta1);
pt2current.x=img.size().width;
pt2current.y=-pt2current.x/tan(theta1) + p1/sin(theta1);
}
else
{
pt1current.y=0;
pt1current.x=p1/cos(theta1);
pt2current.y=img.size().height;
pt2current.x=-pt2current.y/tan(theta1) + p1/cos(theta1);
}
vector<Vec2f>::iterator pos;
for(pos=lines->begin(); pos!=lines->end(); pos++)
{
if(*current==*pos) continue;
if(fabs((*pos)[0]-(*current)[0])<20 && fabs((*pos)[1]-(*current)[1])<CV_PI*10/180)
{
float p = (*pos)[0];
float theta = (*pos)[1];
Point pt1, pt2;
if((*pos)[1]>CV_PI*45/180 && (*pos)[1]<CV_PI*135/180)
{
pt1.x=0;
pt1.y = p/sin(theta);
pt2.x=img.size().width;
pt2.y=-pt2.x/tan(theta) + p/sin(theta);
}
else
{
pt1.y=0;
pt1.x=p/cos(theta);
pt2.y=img.size().height;
pt2.x=-pt2.y/tan(theta) + p/cos(theta);
}
if(((double)(pt1.x-pt1current.x)*(pt1.x-pt1current.x) + (pt1.y-pt1current.y)*(pt1.y-pt1current.y)<64*64) &&
((double)(pt2.x-pt2current.x)*(pt2.x-pt2current.x) + (pt2.y-pt2current.y)*(pt2.y-pt2current.y)<64*64))
{
// Merge the two
(*current)[0] = ((*current)[0]+(*pos)[0])/2;
(*current)[1] = ((*current)[1]+(*pos)[1])/2;
(*pos)[0]=0;
(*pos)[1]=-100;
}
}
}
}
}
void drawLine(Vec2f line, Mat &img, Scalar rgb = CV_RGB(0,0,255))
{
if(line[1]!=0)
{
float m = -1/tan(line[1]);
float c = line[0]/sin(line[1]);
cv::line(img, Point(0, c), Point(img.size().width, m*img.size().width+c), rgb);
}
else
{
cv::line(img, Point(line[0], 0), Point(line[0], img.size().height), rgb);
}
}
int main()
{
VideoCapture stream(0); //0 is the id of video device.0 if you have only one camera.
if (!stream.isOpened()) //check if video device has been initialised
{
cout << "cannot open camera";
}
//unconditional loop
while (true)
{
Mat screen;
stream.read(screen);
//flip(src,src,1);
Mat outerBox = Mat(screen.size(), CV_8UC1);
cvtColor(screen,screen, CV_BGR2GRAY);
GaussianBlur(screen,screen,Size(11,11),0);
adaptiveThreshold(screen, outerBox, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY_INV, 13, 4);
Mat kernel = (Mat_<uchar>(3,3) << 0,1,0,1,1,1,0,1,0);
erode(outerBox,outerBox,kernel);
dilate(outerBox, outerBox, kernel);
Mat element = getStructuringElement(MORPH_RECT, Size(1,1));
dilate(outerBox,outerBox,element);
erode(outerBox,outerBox,element);
int count=0;
int max=-1;
Point maxPt;
for(int y=0; y<outerBox.size().height; y++)
{
uchar *row = outerBox.ptr(y);
for(int x=0; x<outerBox.size().width; x++)
{
if(row[x]>=128)
{
int area = floodFill(outerBox, Point(x,y), CV_RGB(0,0,64));
if(area>max)
{
maxPt = Point(x,y);
max = area;
}
}
}
}
floodFill(outerBox, maxPt, CV_RGB(255,255,255));
for(int y=0; y<outerBox.size().height; y++)
{
uchar *row = outerBox.ptr(y);
for(int x=0; x<outerBox.size().width; x++)
{
if(row[x]==64 && x!=maxPt.x && y!=maxPt.y)
{
int area = floodFill(outerBox, Point(x,y), CV_RGB(0,0,0));
}
}
}
HoughLines(outerBox, lines,1, CV_PI/180, 200);
for (int i=0; i<=lines.size(); i++)
{
drawLine(lines[i],outerBox, CV_RGB(0,0,128));
}
vector<Vec2f> lines;
HoughLines(outerBox, lines, 1, CV_PI/180, 200);
mergeRelatedLines(&lines, screen);
Vec2f topEdge = Vec2f(1000,1000);
double topYIntercept=100000, topXIntercept=0;
Vec2f bottomEdge = Vec2f(-1000,-1000);
double bottomYIntercept=0, bottomXIntercept=0;
Vec2f leftEdge = Vec2f(1000,1000);
double leftXIntercept=100000, leftYIntercept=0;
Vec2f rightEdge = Vec2f(-1000,-1000);
double rightXIntercept=0, rightYIntercept=0;
for(int i=0; i<lines.size(); i++)
{
Vec2f current = lines[i];
float p=current[0];
float theta=current[1];
if(p==0 && theta==-100)
continue;
double xIntercept, yIntercept;
xIntercept = p/cos(theta);
yIntercept = p/(cos(theta)*sin(theta));
if(theta>CV_PI*80/180 && theta<CV_PI*100/180)
{
if(p<topEdge[0])
topEdge = current;
if(p>bottomEdge[0])
bottomEdge = current;
}
else if(theta<CV_PI*10/180 || theta>CV_PI*170/180)
{
if(xIntercept>rightXIntercept)
{
rightEdge = current;
rightXIntercept = xIntercept;
}
else if(xIntercept<=leftXIntercept)
{
leftEdge = current;
leftXIntercept = xIntercept;
}
}
}
drawLine(topEdge, screen, CV_RGB(0,0,0));
drawLine(bottomEdge, screen, CV_RGB(0,0,0));
drawLine(leftEdge, screen, CV_RGB(0,0,0));
drawLine(rightEdge, screen, CV_RGB(0,0,0));
Point left1, left2, right1, right2, bottom1, bottom2, top1, top2;
int height=outerBox.size().height;
int width=outerBox.size().width;
if(leftEdge[1]!=0)
{
left1.x=0;
left1.y=leftEdge[0]/sin(leftEdge[1]);
left2.x=width;
left2.y=-left2.x/tan(leftEdge[1]) + left1.y;
}
else
{
left1.y=0;
left1.x=leftEdge[0]/cos(leftEdge[1]);
left2.y=height;
left2.x=left1.x - height*tan(leftEdge[1]);
}
if(rightEdge[1]!=0)
{
right1.x=0;
right1.y=rightEdge[0]/sin(rightEdge[1]);
right2.x=width;
right2.y=-right2.x/tan(rightEdge[1]) + right1.y;
}
else
{
right1.y=0;
right1.x=rightEdge[0]/cos(rightEdge[1]);
right2.y=height;
right2.x=right1.x - height*tan(rightEdge[1]);
}
bottom1.x=0;
bottom1.y=bottomEdge[0]/sin(bottomEdge[1]);
bottom2.x=width;
bottom2.y=-bottom2.x/tan(bottomEdge[1]) + bottom1.y;
top1.x=0;
top1.y=topEdge[0]/sin(topEdge[1]);
top2.x=width;
top2.y=-top2.x/tan(topEdge[1]) + top1.y;
double leftA = left2.y-left1.y;
double leftB = left1.x-left2.x;
double leftC = leftA*left1.x + leftB*left1.y;
double rightA = right2.y-right1.y;
double rightB = right1.x-right2.x;
double rightC = rightA*right1.x + rightB*right1.y;
double topA = top2.y-top1.y;
double topB = top1.x-top2.x;
double topC = topA*top1.x + topB*top1.y;
double bottomA = bottom2.y-bottom1.y;
double bottomB = bottom1.x-bottom2.x;
double bottomC = bottomA*bottom1.x + bottomB*bottom1.y;
// Intersection of left and top
double detTopLeft = leftA*topB - leftB*topA;
CvPoint ptTopLeft = cvPoint((topB*leftC - leftB*topC)/detTopLeft, (leftA*topC - topA*leftC)/detTopLeft);
// Intersection of top and right
double detTopRight = rightA*topB - rightB*topA;
CvPoint ptTopRight = cvPoint((topB*rightC-rightB*topC)/detTopRight, (rightA*topC-topA*rightC)/detTopRight);
// Intersection of right and bottom
double detBottomRight = rightA*bottomB - rightB*bottomA;
CvPoint ptBottomRight = cvPoint((bottomB*rightC-rightB*bottomC)/detBottomRight, (rightA*bottomC-bottomA*rightC)/detBottomRight);// Intersection of bottom and left
double detBottomLeft = leftA*bottomB-leftB*bottomA;
CvPoint ptBottomLeft = cvPoint((bottomB*leftC-leftB*bottomC)/detBottomLeft, (leftA*bottomC-bottomA*leftC)/detBottomLeft);
int maxLength = (ptBottomLeft.x-ptBottomRight.x)*(ptBottomLeft.x-ptBottomRight.x) + (ptBottomLeft.y-ptBottomRight.y)*(ptBottomLeft.y-ptBottomRight.y);
int temp = (ptTopRight.x-ptBottomRight.x)*(ptTopRight.x-ptBottomRight.x) + (ptTopRight.y-ptBottomRight.y)*(ptTopRight.y-ptBottomRight.y);
if(temp>maxLength) maxLength = temp;
temp = (ptTopRight.x-ptTopLeft.x)*(ptTopRight.x-ptTopLeft.x) + (ptTopRight.y-ptTopLeft.y)*(ptTopRight.y-ptTopLeft.y);
if(temp>maxLength) maxLength = temp;
temp = (ptBottomLeft.x-ptTopLeft.x)*(ptBottomLeft.x-ptTopLeft.x) + (ptBottomLeft.y-ptTopLeft.y)*(ptBottomLeft.y-ptTopLeft.y);
if(temp>maxLength) maxLength = temp;
maxLength = sqrt((double)maxLength);
Point2f src[4], dst[4];
src[0] = ptTopLeft;
dst[0] = Point2f(0,0);
src[1] = ptTopRight;
dst[1] = Point2f(maxLength-1, 0);
src[2] = ptBottomRight;
dst[2] = Point2f(maxLength-1, maxLength-1);
src[3] = ptBottomLeft;
dst[3] = Point2f(0, maxLength-1);
Mat undistorted = Mat(Size(maxLength, maxLength), CV_8UC1);
cv::warpPerspective(screen, undistorted, cv::getPerspectiveTransform(src, dst), Size(maxLength, maxLength));
Mat undistortedThreshed = undistorted.clone();
adaptiveThreshold(undistorted, undistortedThreshed, 255, CV_ADAPTIVE_THRESH_GAUSSIAN_C, CV_THRESH_BINARY_INV, 33, 2);
imshow("threshold",outerBox);
//imshow("undisolved", undistortedThreshed);
int col = 0, row = 8 ;
Rect rec = Rect(col * (undistortedThreshed.cols/9), row * (undistortedThreshed.rows/9), undistortedThreshed.cols/9, undistortedThreshed.rows/9);
Mat roi = undistortedThreshed(rec);
imshow("ROI", roi);
imshow("src", undistorted);
int solvemap[9][9];
CvPoint cell;
for (int i=0; i<9; i++)
for (int j=0; j<9; j++)
{
cell = cvPoint(ptTopLeft.x+undistortedThreshed.cols/18+col * (undistortedThreshed.cols/9),ptTopLeft.y+undistortedThreshed.rows/9+row * (undistortedThreshed.rows/9));
string result;
ostringstream convert;
convert << solvemap[i][j];
result = convert.str();
putText(screen,result,cell,FONT_ITALIC,1,Scalar(0,200,200),4);
}
imshow("screen",screen);
if (waitKey(30) >= 0)
break;
}
return 0;
}