-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplicationutils.cpp
169 lines (147 loc) · 4.56 KB
/
applicationutils.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
//
// Created by xushy on 18-7-2.
//
#include "applicationutils.h"
#include <QDir>
#include <QDebug>
#include <opencv2/imgcodecs.hpp>
cv::Point Chess::uint32_to_point(unsigned int position) {
return cv::Point(position % 10000, position / 10000);
}
unsigned int Chess::point_to_uint32(cv::Point point) {
return point_to_uint32(point.x, point.y);
}
unsigned int Chess::point_to_uint32(int x, int y) {
return y * 10000 + x;
}
double Chess::get_distance_by_position(QPoint point1, QPoint point2) {
double distance = sqrt(abs(point1.x() - point2.x()) * abs(point1.x() - point2.x()) + abs(point1.y() - point2.y()) * abs(point1.y() - point2.y()));
return distance;
}
QString Chess::get_chess_name(int type) {
int tmp = type;
QString prefix;
QString name;
if (tmp >= 10) {
prefix.append("ºÚ");
tmp = tmp - 10;
} else {
prefix = "ºì";
}
switch (tmp) {
case R_CHE:
name = "³µ";
break;
case R_MA:
name = "Âí";
break;
case R_XIANG:
name = "Ïó";
break;
case R_SHI:
name = "Ê¿";
break;
case R_JIANG:
name = "½«";
break;
case R_PAO:
name = "ÅÚ";
break;
case R_ZU:
name = "×ä";
break;
}
return prefix.append(name);
}
void debug_print_all_circle_to_screen(QList<QRect> &list, cv::Mat &screen) {
for(QRect rect : list) {
cv::circle(screen, cv::Point(rect.left(), rect.top()), rect.width(), cv::Scalar(255, 0, 0), 2);
QString path = "D:\\test.jpg";
cv::imwrite(path.toStdString(), screen);
}
}
bool Chess::sort_circle(QRect &rt1, QRect &rt2) {
if(abs(rt1.top() - rt2.top()) < 15) {
return rt1.left() < rt2.left();
} else {
return rt1.top() < rt2.top();
}
}
QRect Chess::detect_chess_board(cv::Mat &screen) {
QRect rect(0, 0, 0, 0);
QPoint start_point(0, 0);
int continues_cout = 0;
QList<QRect> list = hough_detection_circle(screen);
qDebug() << "detect_chess_board circle size:" << list.size();
if (list.size() < 32) {
qErrnoWarning("detect_chess_board failed for circle is too little!");
return rect;
}
qSort(list.begin(), list.end(), sort_circle);
QList<QRect>::iterator list_iter = list.begin();
while (list_iter != list.end()) {
QPoint p1 = list_iter->topLeft();
list_iter++;
if (list_iter == list.end()) {
break;
}
QPoint p2 = list_iter->topLeft();
list_iter++;
if (list_iter == list.end()) {
break;
}
QPoint p3 = list_iter->topLeft();
double d1 = get_distance_by_position(p1, p2);
double d2 = get_distance_by_position(p2, p3);
if (abs(d1 - d2) < 12) {
if (start_point.x() == 0 && start_point.y() == 0) {
start_point = p1;
}
continues_cout++;
} else {
if (continues_cout == 7) {
if (rect.left() == 0 && rect.top() == 0) {
qDebug() << "detect chess board start_point:(" << start_point.x() << "," << start_point.y() << ")";
rect.setTopLeft(start_point);
} else {
rect.setWidth(p2.x() - rect.left());
rect.setHeight(p2.y() - rect.top());
qDebug() << "detect_chess_board end_point:(" << p2.x() << "," << p2.y() << ")";
break;
}
} else {
start_point.setX(0);
start_point.setY(0);
}
continues_cout = 0;
}
list_iter--;
}
return rect;
}
QList<QRect> Chess::hough_detection_circle(cv::Mat &src) {
QList<QRect> circles;
cv::Mat src_gray;
std::vector<cv::Vec3f> cirs;
cvtColor(src, src_gray, cv::COLOR_BGR2GRAY);
GaussianBlur(src_gray, src_gray, cv::Size(3, 3), 2, 2);
HoughCircles(src_gray, cirs, cv::HOUGH_GRADIENT, 1, 25, 208, 40, 15, 45);
if (cirs.size() > 0) {
for (cv::Vec3f vec3f : cirs) {
int radius = cvRound(vec3f[2]);
circles.push_back(QRect(vec3f[0], vec3f[1], radius, radius));
}
}
return circles;
}
bool Chess::hough_detection_single_circle(cv::Mat &src, QRect &out) {
QList<QRect> rects = hough_detection_circle(src);
if (rects.size() < 1) {
return false;
}
out = rects.front();
return true;
}
QString Hub::current_dir() {
return qEnvironmentVariable(PWD, QDir::currentPath());
}