-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjudge.cpp
403 lines (390 loc) · 11.2 KB
/
judge.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#include "canny.h"
Point2i getcrosspoint(Point point1, Point point2, Point point3, Point point4, int height, int width)
{
double ka, kb;
Point2i crossPoint;
ka = (double)(point2.y - point1.y) / (double)(point2.x - point1.x); //求出LineA斜率
kb = (double)(point4.y - point3.y) / (double)(point4.x - point3.x); //求出LineB斜率
crossPoint.x = (ka*point1.x - point1.y - kb * point3.x + point3.y) / (ka - kb);
crossPoint.y = (ka*kb*(point1.x - point3.x) + ka * point3.y - kb * point1.y) / (ka - kb);
//crosspoint.x = (ka*LineA[0] - LineA[1] - kb * LineB[0] + LineB[1]) / (ka - kb);
//crossPoint.y = (ka*kb*(LineA[0] - LineB[0]) + ka * LineB[1] - kb * LineA[1]) / (ka - kb);
if (-500 > crossPoint.x || crossPoint.x > width+500 || -500 > crossPoint.y || crossPoint.y > height+500)
{
crossPoint.x = 0;
crossPoint.y = 0;
return crossPoint;
}
else
{
return crossPoint;
}
}
bool sortcrosspoint(Point point1, Point point2, Point point3, Point point4, int height, int width)
{
double ka, kb;
Point2i crossPoint;
ka = (double)(point2.y - point1.y) / (double)(point2.x - point1.x); //求出LineA斜率
kb = (double)(point4.y - point3.y) / (double)(point4.x - point3.x); //求出LineB斜率
crossPoint.x = (ka*point1.x - point1.y - kb * point3.x + point3.y) / (ka - kb);
crossPoint.y = (ka*kb*(point1.x - point3.x) + ka * point3.y - kb * point1.y) / (ka - kb);
if (0 > crossPoint.x || crossPoint.x > width || 0 > crossPoint.y || crossPoint.y > height )
{
return false;
}
else
{
return true;
}
}
int judgepoint(uchar** ptr,Mat &img1,Point point, int height, int width)
{
//Mat img1 = imread("D:/hough.png", 0);
//int height = img1.rows;
//int width = img1.cols;
int i, j;
int result;
/*
int i, j;
int result;
uchar **ptr = (uchar **)malloc(height * sizeof(uchar *)); //二维数组ptr[][]
for (i = 0; i < height; i++)
ptr[i] = (uchar *)malloc(width * sizeof(uchar *));
for (i = 0; i < height; i++)
{
for (j = 0; j < width; j++)
{
ptr[i][j] = img1.at<uchar>(i, j); //img的矩阵数据传给二维数组ptr[][]
}
}
*/
if (point.x == 0) ////即prt中的y为0 point.x=0 与左边界的交点
{
int mins = minn(point.y, height - point.y, 20); //正常情况下左右各20的像素范围内 但是要是靠近图像顶点就会越界,所以增加min()函数
for (i = -mins; i < mins; i++)
{
for (j = 0; j < 5; j++)
{
if ((ptr[point.y +i][point.x + j]) == 255)
{
result = 1;
return result;
}
if (i == 2 && j == 5)
{
result = 0;
return result;
}
}
}
}
else if (point.x == width - 1) //即prt中的y为width - 1 与右边界的交点
{
int mins = minn(point.y , height - point.y , 20);
for (i = -mins; i < mins; i++)
{
for (j = 0; j < 5; j++)
{
if ((ptr[point.y + i][point.x - j]) == 255)
{
result = 1;
return result;
}
if (i == 2 && j == 4)
{
result = 0;
return result;
}
}
}
}
else if (point.y == 0) //即prt中的x为0 与上边界的交点
{
int mins = minn(point.x, width - point.x, 20);
for (i = 0; i < 5; i++)
{
for (j = -mins; j < mins; j++)
{
if ((ptr[point.y + i][point.x + j]) == 255)
{
result = 1;
return result;
}
if (i == 4 && j == 2)
{
result = 0;
return result;
}
}
}
}
else if (point.y == height - 1) //即prt中的x为height-1 与下边界的交点
{
int mins = minn(point.x, width - point.x, 20);
for (i = 0; i < 5; i++)
{
for (j = -mins; j < mins; j++)
{
if ((ptr[point.y - i][point.x + j]) == 255)
{
result = 1;
return result;
}
if (i == 4 && j == 2)
{
result = 0;
return result;
}
}
}
}
else
{
result = 0;
return result;
}
}
vector<Point> sort(vector<Point> &result_points,int height,int width) //xy之和最小为左上 最大不一定为右下!!!
{ //确定左上后根据交点来确定右下
int i, j,e;
double ka, kb;
Point2i crossPoint;
Point temp;
i = 0;
for (i = 0; i < result_points.size() - 1; i++)
{
for (j = 0; j < result_points.size() - 1 - i; j++)
{
if ((abs(result_points[j].x) + abs(result_points[j].y)) > (abs(result_points[j + 1].x) + abs(result_points[j + 1].y)))
{
temp = result_points[j];
result_points[j] = result_points[j + 1];
result_points[j + 1] = temp;
}
}
}
if (sortcrosspoint(result_points[0], result_points[1], result_points[2], result_points[3], height, width)) //第二个点在右下
{
temp = result_points[1];
result_points[1] = result_points[3];
result_points[3] = temp;
}
if (sortcrosspoint(result_points[0], result_points[2], result_points[1], result_points[3], height, width)) //第三个点在右下
{
temp = result_points[2];
result_points[2] = result_points[3];
result_points[3] = temp;
}
//否则 第四个点在右下 即正确的位置
//int ans = (x2 - x1)*(y3 - y1) - (y2 - y1)*(x3 - x1);//
int ans = (result_points[1].y - result_points[0].y)*(result_points[2].x - result_points[0].x) -
(result_points[1].x - result_points[0].x)*(result_points[2].y - result_points[0].y);
if (ans < 0)
{
return result_points;
}
else //根据顺时针来确定左下点和右上点
{
temp = result_points[1];
result_points[1] = result_points[2];
result_points[2] = temp;
}
return result_points;
}
//传入参数:四个点坐标 返回参数:图像 Mat
//将不在这四个点坐标围成的四边形的像素值全部变为0(消除边界的联通问题)
Mat frame(int count,uchar** ptr,Mat &img,vector<Point> &sorted_points)
{
//Mat img = imread("D:/hough.png", 0);
int height = img.rows;
int width = img.cols;
Mat image = Mat(height, width, CV_8UC1);
int i, j;
uchar *ptmp = NULL;//这是关键的指针!!
//uchar **ptr = (uchar **)malloc(height * sizeof(uchar *)); //二维数组ptr[][]
//for (i = 0; i < height; i++)
//ptr[i] = (uchar *)malloc(width * sizeof(uchar *));
for (i = 0; i < height; i++)
{
ptmp = image.ptr<uchar>(i);
for (j = 0; j < width; j++)
{
//ptr[i][j] = img.at<uchar>(i, j); //img的矩阵数据传给二维数组ptr[][]
if (ptr[i][j] == 255&&judgeinout(count,i, j, sorted_points) == 1)
{
ptmp[j] = ptr[i][j];
}
else
{
ptmp[j] = 0;
}
}
}
return image;
/*
float k1, k2, k3, b1, b2, b3; //三条直线的斜率和截距【xy互换】
k1 = float(sorted_points[1].x - sorted_points[0].x) / (sorted_points[1].y - sorted_points[0].y);
k2 = float(sorted_points[2].x - sorted_points[0].x) / (sorted_points[3].y - sorted_points[0].y);
k3 = float(sorted_points[3].x - sorted_points[2].x) / (sorted_points[3].y - sorted_points[2].y);
b1 = float(sorted_points[1].x - k1 * sorted_points[1].y);
b2 = float(sorted_points[2].x - k2 * sorted_points[2].y);
b3 = float(sorted_points[3].x - k3 * sorted_points[3].y);
*/
}
int judgeinout(int count,int x, int y, vector<Point> &sorted_points) //判断一个点是否在该四边形内部 点向量法 四个点顺时针或逆时针!!!不是左上右上左下右下
{
if (count == 4)
{
int a, b, c, d; //0123 顺时针为 0231 0->1 2->2 3->3 1->4
a = (sorted_points[2].y - sorted_points[0].y)*(y - sorted_points[0].x) - (sorted_points[2].x - sorted_points[0].x)*(x - sorted_points[0].y);
b = (sorted_points[3].y - sorted_points[2].y)*(y - sorted_points[2].x) - (sorted_points[3].x - sorted_points[2].x)*(x - sorted_points[2].y);
c = (sorted_points[1].y - sorted_points[3].y)*(y - sorted_points[3].x) - (sorted_points[1].x - sorted_points[3].x)*(x - sorted_points[3].y);
d = (sorted_points[0].y - sorted_points[1].y)*(y - sorted_points[1].x) - (sorted_points[0].x - sorted_points[1].x)*(x - sorted_points[1].y);
if ((a > 0 && b > 0 && c > 0 && d > 0) || (a < 0 && b < 0 && c < 0 && d < 0))
{
return 1;
}
else
{
return 0;
}
}
if (count == 3)
{
int a, b, c; //012 顺时针为 0231 0->1 2->2 3->3 1->4
a = (sorted_points[1].y - sorted_points[0].y)*(y - sorted_points[0].x) - (sorted_points[1].x - sorted_points[0].x)*(x - sorted_points[0].y);
b = (sorted_points[2].y - sorted_points[1].y)*(y - sorted_points[1].x) - (sorted_points[2].x - sorted_points[1].x)*(x - sorted_points[1].y);
c = (sorted_points[0].y - sorted_points[2].y)*(y - sorted_points[2].x) - (sorted_points[0].x - sorted_points[2].x)*(x - sorted_points[2].y);
if ((a > 0 && b > 0 && c > 0) || (a < 0 && b < 0 && c < 0 ))
{
return 1;
}
else
{
return 0;
}
}
}
vector<struct line> SortLines( vector<struct line> &lines ) //先theta后r
{
int i,j;
struct line temp;
for (i = 0; i < lines.size(); i++)
{
for (j = 0; j < lines.size() - i - 1; j++)
{
if (lines[j].theta < lines[j + 1].theta)
{
temp = lines[j];
lines[j] = lines[j + 1];
lines[j + 1] = temp;
}
}
}
int based = FindBase(lines); //众数
Point base(lines[based].theta, lines[based].r);
for (i = 0; i < lines.size(); i++)
{
for (j = 0; j < lines.size() - i - 1; j++)
{
if (lines[j].r < lines[j + 1].r&&abs(lines[j].theta- lines[j + 1].theta)<6)
{
temp = lines[j];
lines[j] = lines[j + 1];
lines[j + 1] = temp;
}
}
}
struct line newline;
newline.theta = base.x;
newline.r = base.y;
lines.push_back(newline);
return lines;
}
vector<struct line> Filter(vector<struct line> &lines)
{
int i, j;
struct line base;
vector<struct line> correct_lines;
base = lines[lines.size()-1];
/*
for (i = 0; i < lines.size() - 2; i++)
{
if ((lines[i].theta - lines[i + 1].theta < 5 && abs(lines[i].r - lines[i + 1].r) < 150)
&& (lines[i].theta - lines[i + 2].theta < 5 && abs(lines[i].r - lines[i + 2].r) < 150)
&& (lines[i].theta - lines[i + 3].theta < 5 && abs(lines[i].r - lines[i + 3].r) < 150)
&& (lines[i].theta - lines[i + 4].theta < 5 && abs(lines[i].r - lines[i + 4].r) < 150)
&& (lines[i].theta - lines[i + 5].theta < 5 && abs(lines[i].r - lines[i + 5].r) < 150))
{
base=lines[i];
break;
}
}
*/
for (i = 0; i < lines.size()-1; i++)
{
if (abs(lines[i].theta - base.theta) < 3|| abs(abs(base.theta - lines[i].theta) - 90) < 3
|| abs(abs(base.theta - lines[i].theta) - 180) < 3 || abs(abs(base.theta - lines[i].theta) - 270) < 3
|| abs(abs(base.theta - lines[i].theta) - 360) < 3)
{
correct_lines.push_back(lines[i]);
}
}
return correct_lines;
}
int FindBase(vector<struct line> &lines)
{
int many = 1, less = 1;
int temp;
int value = 0;
for (int i = 0; i < lines.size()-1; i++)
{
for (int j = i; j < lines.size()-1; j++)
{
if (lines[j].theta == lines[j + 1].theta)
//if(abs(lines[j].theta-lines[j+1].theta)<=3)
{
less++;
}
else
{
if (many < less)
{
temp = many;
many = less;
less = temp;
value = j;
}
less = 1;
break;
}
}
}
return value;
}
int minn(int a,int b,int c)
{
if (a <= b && a <= c)
{
return a;
}
else if (b <= a && b <= c)
{
return b;
}
else
{
return c;
}
}
bool contain(Point point,vector<Point> &result_points)
{
for (int i = 0; i < result_points.size(); i++)
{
if (result_points[i] == point)
{
return true;
}
}
return false;
}