-
Notifications
You must be signed in to change notification settings - Fork 0
/
2020.8.15
406 lines (348 loc) · 12.7 KB
/
2020.8.15
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
404
405
406
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include <math.h>
#include <string.h>
#define elemType int
using namespace cv;
using namespace std;
int thresh = 50, N = 5;
const char* wndname = "Square Detection Demo";
// helper function:
// finds a cosine of angle between vectors
// from pt0->pt1 and from pt0->pt2
static double angle(Point pt1, Point pt2, Point pt0)
{
double dx1 = pt1.x - pt0.x;
double dy1 = pt1.y - pt0.y;
double dx2 = pt2.x - pt0.x;
double dy2 = pt2.y - pt0.y;
return (dx1 * dx2 + dy1 * dy2) / sqrt((dx1 * dx1 + dy1 * dy1) * (dx2 * dx2 + dy2 * dy2) + 1e-10);
}
int getColSum(Mat src, int col)//统计所有列像素的总和
{
int sum = 0;
int height = src.rows;
int width = src.cols;
for (int i = 0; i < height; i++)
{
sum = sum + src.at <uchar>(i, col);
}
return sum;
}
int getRowSum(Mat src, int row)//统计所有行像素的总和
{
int sum = 0;
int height = src.rows;
int width = src.cols;
for (int i = 0; i < width; i++)
{
sum += src.at <uchar>(row, i);
}
return sum;
}
int cutLeft(Mat& src, Mat& leftImg)//左右切割
{
int left, right;
left = 0;
right = src.cols;
int i;
for (i = 1; i < src.cols; i++)
{
int colValue = getColSum(src, i);//统计所有列像素的总和
//cout <<i<<" th "<< colValue << endl;
if (colValue > 0)//扫描直到列像素的总和大于0时,记下当前位置left
{
left = i;
break;
}
}
if (left == 0)
{
return 1;
}
//继续扫描
for (; i < src.cols; i++)
{
int colValue = getColSum(src, i);//统计所有列像素的总和
//cout << i << " th " << colValue << endl;
if (colValue == 0)//继续扫描直到列像素的总和等于0时,记下当前位置right
{
right = i;
break;
}
}
int width = right - left;//分割图片的宽度则为right - left
Rect rect(left, 0, width, src.rows);//构造一个矩形,参数分别为矩形左边顶部的X坐标、Y坐标,右边底部的X坐标、Y坐标(左上角坐标为0,0)
leftImg = src(rect).clone();
return 0;
}
void cutTop(Mat& src, Mat& dstImg)//上下切割
{
int top, bottom;
top = 1;
bottom = src.rows;
int i;
for (i = src.rows - 1; i > 0; i--)
{
int colValue = getRowSum(src, i);//统计所有行像素的总和
//cout <<i<<" th "<< colValue << endl;
if (colValue > 0)//扫描直到行像素的总和大于0时,记下当前位置
{
bottom = i;
break;
}
}
for (; i > 0; i--)
{
int colValue = getRowSum(src, i);//统计所有行像素的总和
//cout << i << " th " << colValue << endl;
if (colValue == 0)//继续扫描直到行像素的总和等于0时,记下当前位置bottom
{
top = i;
break;
}
}
int height = bottom - top;
Rect rect(0, top, src.cols, height);
dstImg = src(rect).clone();
cutLeft(dstImg, dstImg);//上下切割
}
// returns sequence of squares detected on the image.
// the sequence is stored in the specified memory storage
static void findSquares(const Mat& image, vector<vector<Point> >& squares)
{
squares.clear();
//s Mat pyr, timg, gray0(image.size(), CV_8U), gray;
// down-scale and upscale the image to filter out the noise
//pyrDown(image, pyr, Size(image.cols/2, image.rows/2));
//pyrUp(pyr, timg, image.size());
// blur will enhance edge detection
Mat timg(image);
Mat gray0(timg.size(), CV_8U), gray;
vector<vector<Point> > contours;
// find squares in every color plane of the image
for (int c = 0; c < 3; c++)
{
int ch[] = { c, 0 };
mixChannels(&timg, 1, &gray0, 1, ch, 1);
// try several threshold levels
for (int l = 0; l < N; l++)
{
// hack: use Canny instead of zero threshold level.
// Canny helps to catch squares with gradient shading
if (l == 0)
{
// apply Canny. Take the upper threshold from slider
// and set the lower to 0 (which forces edges merging)
Canny(gray0, gray, 5, thresh, 5);
// dilate canny output to remove potential
// holes between edge segments
dilate(gray, gray, Mat(), Point(-1, -1));
}
else
{
// apply threshold if l!=0:
// tgray(x,y) = gray(x,y) < (l+1)*255/N ? 255 : 0
gray = gray0 >= (l + 1) * 255 / N;
}
// find contours and store them all as a list
findContours(gray, contours, RETR_LIST, CHAIN_APPROX_SIMPLE);
vector<Point> approx;
// test each contour
for (size_t i = 0; i < contours.size(); i++)
{
// approximate contour with accuracy proportional
// to the contour perimeter
approxPolyDP(Mat(contours[i]), approx, arcLength(Mat(contours[i]), true) * 0.02, true);
// square contours should have 4 vertices after approximation
// relatively large area (to filter out noisy contours)
// and be convex.
// Note: absolute value of an area is used because
// area may be positive or negative - in accordance with the
// contour orientation
if (approx.size() == 4 &&
fabs(contourArea(Mat(approx))) > 1000 &&
isContourConvex(Mat(approx)))
{
double maxCosine = 0;
for (int j = 2; j < 5; j++)
{
// find the maximum cosine of the angle between joint edges
double cosine = fabs(angle(approx[j % 4], approx[j - 2], approx[j - 1]));
maxCosine = MAX(maxCosine, cosine);
}
// if cosines of all angles are small
// (all angles are ~90 degree) then write quandrange
// vertices to resultant sequence
if (maxCosine < 0.2)
squares.push_back(approx);
}
}
}
}
}
// the function draws all the squares in the image
static void drawSquares(Mat& image, const vector<vector<Point> >& squares)
{
for (size_t i = 0; i < squares.size(); i++)
{
const Point* p = &squares[i][0];
int n = (int)squares[i].size();
//dont detect the border
if (p->x > 3 && p->y > 3)
polylines(image, &p, &n, 1, true, Scalar(0, 255, 0), 3, LINE_AA);
}
}
void getPXSum(Mat& src, int& a)//获取所有像素点和
{
threshold(src, src, 100, 255, CV_THRESH_BINARY);
a = 0;
for (int i = 0; i < src.rows; i++)
{
for (int j = 0; j < src.cols; j++)
{
a += src.at <uchar>(i, j);
}
}
}
int diff = 0;
int serieNum = 0;
int getSubtract(Mat& src, int TemplateNum) //数字识别
{
Mat img_result;
int min = 100000000;
//threshold(src, src, 100, 255, CV_THRESH_BINARY);
char name[50];
for (int i = 0; i < 10; i++) {
int diff1 = 0, diff2 = 0;
sprintf_s(name, "%d.jpg", i);
Mat Template = imread(name, CV_LOAD_IMAGE_GRAYSCALE);//读取模板
threshold(Template, Template, 100, 255, CV_THRESH_BINARY_INV);
cutTop(Template, Template);
resize(Template, Template, Size(620, 480));//调整尺寸
cv::imshow("模板数字", Template);
cv::imshow("要识别的数字", src);
cv::waitKey(100);
//getPXSum(Template, diff1);
//getPXSum(src, diff2);
/*让需要匹配的图分别和10个模板对应像素点值相减,然后求返回图片的整个图片的像素点值得平方和,和哪个模板匹配时候返回图片的平方和最小则就可以得到结果*/
absdiff(Template, src, img_result);//AbsDiff,OpenCV中计算两个数组差的绝对值的函数。
getPXSum(img_result, diff);//获取所有像素点和
//std::cout << diff << endl;
if (diff < min)//像素点对比
{
min = diff;
serieNum = i;
}
}
if (min > 13000000)
{
std::printf("没匹配到数字\n");
}
else
{
std::printf("最小距离是%d ", min);
std::printf("匹配到第%d个模板匹配的数字是%d\n", serieNum, serieNum);
return serieNum;
}
}
int main()
{
Mat image = imread("D:\\qq\\qq\\qq\\200.jpg", 1);
namedWindow(wndname, 1);
vector<vector<Point> > squares;
findSquares(image, squares);
drawSquares(image, squares);
std::cout << "矩形个数" << squares.size() << endl;
for (int k = 0; k < squares.size(); k++)
{
//std::cout << squares[k][0] << endl;
//std::cout << squares[k][1] << endl;
//std::cout << squares[k][2] << endl;
//std::cout << squares[k][3] << endl;
/* circle(image, squares[2][0], 12, Scalar(0, 0, 255), -1);
circle(image, squares[1][1], 12, Scalar(255, 0, 255), -1);
circle(image, squares[1][2], 12, Scalar(0, 255, 255), -1);
circle(image, squares[1][3], 12, Scalar(255, 255, 0), -1);*/
cv::imshow(wndname, image);
int chang = 0, kuan = 0;
int aa = abs(squares[k][1].x - squares[k][0].x);
int bb = abs(squares[k][2].x - squares[k][0].x);
int cc = abs(squares[k][1].y - squares[k][0].y);
int dd = abs(squares[k][2].y - squares[k][0].y);
if (aa >= bb)
{
chang = aa;
}
else
{
chang = bb;
}
if (cc >= dd)
{
kuan = cc;
}
else
{
kuan = dd;
}
//std::cout << chang << endl;
//std::cout << kuan << endl;
int zsx = 0, zsy = 0;
int he = squares[k][0].x + squares[k][0].y;
for (int i = 0; i < 4; i++)
{
if (squares[k][i].x + squares[k][i].y <= he)
{
he = squares[k][i].x + squares[k][i].y;
//cout << "he" << he << endl;
zsx = squares[k][i].x;
zsy = squares[k][i].y;
}
}
//std::printf("zsx=%d\n", zsx);
// std::printf("zsy=%d\n", zsy);
Rect rect1(zsx, zsy, chang, kuan);
Mat roi1;
image(rect1).copyTo(roi1); // copy the region rect1 from the image to roi1
//imwrite( "out", image );
resize(roi1, roi1, Size(620, 480));
cv::imshow("数字", roi1);
Rect rect(100, 80, 400, 300);
roi1 = roi1(rect);
//if ((char)c == 27)
// break;
Mat res = roi1;
cv::cvtColor(res, res, CV_BGR2GRAY);
threshold(res, res, 100, 255, CV_THRESH_BINARY_INV);
cutTop(res, res);
resize(res, res, Size(620, 480));
//cv::imshow("1", res);
cv::waitKey(100);
//char name[20];
//for (int i = 0; i < 10; i++) {
// sprintf_s(name, "%d.jpg", i);
// Mat src = imread(name, CV_LOAD_IMAGE_GRAYSCALE);//读取模板
// int diff;
// cout << "第"<< i <<"张图"<< endl;
getSubtract(res, 9);//进行数字识别
int zxx = zsx + chang / 2, zxy = zsy + kuan / 2;
Point p2;
p2.x = zxx;
p2.y = zxy;
if (serieNum == 2)
{
std::printf("数字中心横坐标=%d\n", zxx);
std::printf("数字中心纵坐标=%d\n", zxy);
std::printf("矩形长=%d\n", chang);
std::printf("矩形宽=%d\n", kuan);
circle(image, p2, 10, Scalar(255, 0, 0), -1);
cv::imshow(".", image);
waitKey(0);
break;
}
}
return 0;
}