-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreenshot.cpp
367 lines (320 loc) · 12 KB
/
screenshot.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
//
// Created by xsy on 18-7-4.
//
#include "screenshot.h"
#include <fstream>
#include <opencv2/imgproc.hpp>
#include <iostream>
#include <time.h>
#include <QDir>
#include <QDebug>
#include "applicationutils.h"
#include "knndetection.h"
ScreenShot::ScreenShot() :
left_top_(0, 0), right_bottom_(0, 0), max_circle_radius_(0), chess_window_size_(0, 0) {
p_detection_ = new KnnDection();
int i = 0;
chess_position_type_[i++] = Chess::B_CHE;
chess_position_type_[i++] = Chess::B_MA;
chess_position_type_[i++] = Chess::B_XIANG;
chess_position_type_[i++] = Chess::B_SHI;
chess_position_type_[i++] = Chess::B_JIANG;
chess_position_type_[i++] = Chess::B_SHI;
chess_position_type_[i++] = Chess::B_XIANG;
chess_position_type_[i++] = Chess::B_MA;
chess_position_type_[i++] = Chess::B_CHE;
chess_position_type_[i++] = Chess::B_PAO;
chess_position_type_[i++] = Chess::B_PAO;
chess_position_type_[i++] = Chess::B_ZU;
chess_position_type_[i++] = Chess::B_ZU;
chess_position_type_[i++] = Chess::B_ZU;
chess_position_type_[i++] = Chess::B_ZU;
chess_position_type_[i++] = Chess::B_ZU;
chess_position_type_[i++] = Chess::R_ZU;
chess_position_type_[i++] = Chess::R_ZU;
chess_position_type_[i++] = Chess::R_ZU;
chess_position_type_[i++] = Chess::R_ZU;
chess_position_type_[i++] = Chess::R_ZU;
chess_position_type_[i++] = Chess::B_PAO;
chess_position_type_[i++] = Chess::B_PAO;
chess_position_type_[i++] = Chess::B_CHE;
chess_position_type_[i++] = Chess::B_MA;
chess_position_type_[i++] = Chess::R_XIANG;
chess_position_type_[i++] = Chess::R_SHI;
chess_position_type_[i++] = Chess::R_JIANG;
chess_position_type_[i++] = Chess::R_SHI;
chess_position_type_[i++] = Chess::R_XIANG;
chess_position_type_[i++] = Chess::B_MA;
chess_position_type_[i] = Chess::B_CHE;
}
ScreenShot::~ScreenShot() {
delete p_detection_;
}
/*
int ScreenShot::detect_chess_position(std::map<unsigned int, int>* map, cv::Mat &screen) {
std::vector<cv::Vec3f> circle_vector;
std::list<Circle> circle_list;
if (chess_window_size_.width != 0) {
if (chess_window_size_.width != screen.cols && chess_window_size_.height != screen.rows) {
// chess window is not current active window
std::cout << " Chess window is not active, can't get it's screen" << std::endl;
return DETECT_WINDOW_IS_NOT_ACTIVE;
}
}
hough_detection_circle(screen, circle_vector);
for (cv::Vec3f vf : circle_vector) {
cv::Point center(cvRound(vf[0]), cvRound(vf[1]));
int radius = cvRound(vf[2]);
if (left_top_.y == right_bottom_.y) {
circle_list.push_back(Circle(center, radius));
} else {
// fiter out noise circle
cv::Point p1(left_top_.x - 30, left_top_.y - 30);
cv::Point p2(right_bottom_.x + 30, right_bottom_.y + 30);
unsigned int low = Chess::point_to_uint32(p1);
unsigned int high = Chess::point_to_uint32(p2);
unsigned int value = Chess::point_to_uint32(center);
if (value > low && value < high) {
circle_list.push_back(Circle(center, radius));
}
}
}
if (circle_list.size() < 3) {
return DETECT_AUTOTRAIN_CIRCLE_LITTILE;
}
circle_list.sort();
#ifdef _TEST_STD_OUT
std::cout << "input circle size:" << circle_vector.size() << " filter noise:"
<< circle_vector.size() - circle_list.size() << std::endl;
print_circle_position(circle_list, screen);
#endif
if (left_top_.y == right_bottom_.y) {
if (circle_list.size() < 32) {
return DETECT_STUDY_CIRCLE_TO_LITTILE;
}
study(circle_list);
if (left_top_.y == right_bottom_.y) {
return DETECT_STUDY_FAILED;
} else {
chess_window_size_.height = screen.rows;
chess_window_size_.width = screen.cols;
return DETECT_STUDY_SUCCESS;
}
}
if (!p_detection_->is_trained()) {
return auto_train(circle_list, screen);
}
std::list<Sample> samle_list;
grab_samles(circle_list, screen, samle_list);
std::list<Sample>::iterator iter;
for(iter = samle_list.begin(); iter != samle_list.end(); iter ++) {
int type = p_detection_->predict(iter->mat());
int pos = coordinate_screen_to_chess(iter->position());
if (type >= 10) {
type = type - detect_chess_color(*iter);
}
(*map)[pos] = type;
}
if (prev_chess_snap.size() < 1) {
std::map<unsigned int, int>::iterator it = map->begin();
while (it != map->end()) {
prev_chess_snap.insert(it->first, it->second);
it ++;
}
} else {
std::map<unsigned int, int>::iterator it = map->begin();
while (it != map->end()) {
if (!prev_chess_snap.contains(it->first) || it->second != prev_chess_snap.value(it->first)) {
qDebug() << "chess snap change, grab samples";
for(iter = samle_list.begin(); iter != samle_list.end(); iter ++) {
int type = p_detection_->predict(iter->mat());
if (type >= 10) {
type = type - detect_chess_color(*iter);
}
QString path = Hub::current_dir().append("/resources/train/");
path.append(QString::number(type));
QDir dir(path);
if (!dir.exists()) {
dir.mkpath(path);
}
path.append("/").append(QString::number(qrand())).append(".jpg");
cv::Mat out;
cv::inRange(iter->mat(), cv::Scalar(0,0,47), cv::Scalar(255,255,183), out);
cv::threshold(out, out, 0, 255.0, CV_THRESH_BINARY_INV);
out.convertTo(out, CV_32F);
cv::imwrite(path.toStdString(), out);
}
break;
}
it ++;
}
}
return 0;
}
void ScreenShot::study(std::list<Circle> &circle_list) {
std::list<Circle>::iterator list_iter = circle_list.begin();
cv::Point start_point(0, 0);
int continues_cout = 0;
while (list_iter != circle_list.end()) {
max_circle_radius_ = MAX(max_circle_radius_, list_iter->radius());
cv::Point p1 = list_iter->center();
list_iter++;
if (list_iter == circle_list.end()) {
break;
}
cv::Point p2 = list_iter->center();
list_iter++;
if (list_iter == circle_list.end()) {
break;
}
cv::Point p3 = list_iter->center();
double d1 = Chess::get_distance_by_position(p1, p2);
double d2 = Chess::get_distance_by_position(p2, p3);
if (abs(d1 - d2) < 12) {
if (start_point.x == 0 && start_point.y == 0) {
start_point.x = p1.x;
start_point.y = p1.y;
}
continues_cout++;
} else {
if (continues_cout == 7) {
if (left_top_.x == 0 && left_top_.y == 0) {
left_top_.x = start_point.x;
left_top_.y = start_point.y;
} else {
right_bottom_.x = p2.x;
right_bottom_.y = p2.y;
std::cout << "Study result: (x = "
<< left_top_.x << ",y = " << left_top_.y << ")(x = "
<< right_bottom_.x << ",y = " << right_bottom_.y << ")"
<< " max_circle_radius_ = " << max_circle_radius_
<< std::endl;
return;
}
} else {
start_point.x = 0;
start_point.y = 0;
}
continues_cout = 0;
}
list_iter--;
}
}
int max_redius(std::list<Circle> &circle_list) {
int max_value = 0;
std::list<Circle>::iterator list_iter = circle_list.begin();
while (list_iter != circle_list.end()) {
max_value = MAX(max_value, list_iter->radius());
list_iter++;
}
return max_value;
}
int ScreenShot::auto_train(std::list<Circle> &circle_list, cv::Mat &screen) {
std::list<Sample> samle_list;
if (circle_list.size() != 32) {
return DETECT_AUTOTRAIN_CIRCLE_LITTILE;
}
grab_samles(circle_list, screen, samle_list);
p_detection_->train(samle_list);
// check is the train is correct
std::list<Sample>::iterator iter = samle_list.begin();
int wrong_num = 0;
while (iter != samle_list.end()) {
int type = p_detection_->predict(iter->mat());
if (type != iter->label()) {
std::cout << "type:" << type << "label:" << iter->label() << std::endl;
wrong_num++;
}
iter++;
}
if (wrong_num > 0) {
return DETECT_AUTOTRAIN_ERR_RATE_HIGH;
}
std::cout << "knn model train success" << std::endl;
return DETECT_AUTOTRAIN_SUCCESS;
}
void ScreenShot::grab_samles(std::list<Circle> &circle_list, cv::Mat &screen, std::list<Sample> &samle_list) {
int size = max_redius(circle_list)*2 + 10;
int index = 0;
std::list<Circle>::iterator list_iter = circle_list.begin();
while (list_iter != circle_list.end()) {
cv::Rect rect(list_iter->center().x - size/2, list_iter->center().y - size/2, size, size);
cv::Mat roi = screen(rect);
cv::Mat split = cv::Mat::zeros(roi.rows, roi.cols, screen.type());
cv::Mat mask = cv::Mat::zeros(split.rows, split.cols, screen.type());
Circle re_circle;
if (!hough_detection_circle_single(roi, re_circle)) {
QString path = Hub::current_dir().append("/resources/abnormal");
QDir dir(path);
if (!dir.exists()) {
dir.mkpath(path);
}
path.append("/").append(QString::number(qrand())).append(".jpg");
cv::imwrite(path.toStdString(), roi);
qDebug() << " abnormal img";
list_iter++;
index++;
continue;
}
cv::circle(mask, re_circle.center(), re_circle.radius() - 3, CV_RGB(255, 255, 255), -1);
roi.copyTo(split, mask);
cv::Rect re_rect(re_circle.center().x - 22, re_circle.center().y - 22, 44, 44);
split = split(re_rect);
samle_list.push_back(Sample(split, chess_position_type_[index], list_iter->center()));
list_iter++;
index++;
}
}
int ScreenShot::coordinate_screen_to_chess(cv::Point &point) {
int y = 0, x = 0;
int dx = 0, dy = 0;
dx = (right_bottom_.x - left_top_.x) / 8;
dy = (right_bottom_.y - left_top_.y) / 9;
for (int i = 0; i < 9; i++) {
if (abs(point.x - left_top_.x - dx * i) < 8) {
x = i;
break;
}
}
for (int i = 0; i < 10; i++) {
if (abs(point.y - left_top_.y - dy * i) < 8) {
y = i;
}
}
return Chess::point_to_uint32(x, y);
}
void ScreenShot::coordinate_chess_to_screen(int in, cv::Point &point) {
int dx = 0, dy = 0;
dx = (right_bottom_.x - left_top_.x) / 8;
dy = (right_bottom_.y - left_top_.y) / 9;
cv::Point pos = Chess::uint32_to_point(in);
point.x = left_top_.x + dx * pos.x;
point.y = left_top_.y + dy * pos.y;
}
int ScreenShot::detect_chess_color(Sample &sample) {
cv::Mat roi = sample.mat();
cv::Mat threshold;
cv::inRange(roi, cv::Scalar(0,0,84), cv::Scalar(255,255,255), threshold);
cv::threshold(threshold, threshold, 0, 255.0, CV_THRESH_BINARY);
const int channels = threshold.channels();
unsigned int cols = channels * threshold.cols;
uchar *p;
cv::Scalar scalar;
int counter = 0;
for (int j = 0; j < roi.rows; j++) {
p = threshold.ptr<uchar>(j);
for (int i = 0; i < cols; i++) {
int filter = (int)p[i];
if (filter == 0) {
counter ++;
}
}
}
if (counter > 50) {
// black
return 0;
}
// else is red
return 10;
}
*/