-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageDetector.cpp
282 lines (230 loc) · 7.98 KB
/
ImageDetector.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
#include "ImageDetector.h"
#include <stdio.h>
namespace ImageDetector
{
class ImageDetails
{
public:
ImageDetails()
{
x = 0;
y = 0;
h = 0;
w = 0;
}
ImageDetails(int x, int y, int h, int w)
{
this.x = x;
this.y = y;
this.h = h;
this.w = w;
}
int area()
{
return h * w;
}
int x;
int y;
int h;
int w;
};
ImageDetector::ImageDetails detect_v2(cv::Mat src)
{
// get two versions of the cropped images. Based on the incoming image
// and where ite was cropped from, the bitwise_not may do an inverse
// where not needed.
ImageDetails id_a = detect_inverse_optional(src, true);
ImageDetails id_b = detect_inverse_optional(src, false);
return id_a.area() > id_b.area() ? id_a : id_b;
} // detect_v2
ImageDetector::ImageDetails detect_inverse_optional(cv::Mat src, bool inverse = false)
{
cv::Mat dst;
src.copyTo(dst);
// apply some filters to get started
cv::cvtColor(dst, dst, cv::COLOR_BGR2GRAY);
cv::medianBlur(dst, dst, 5);
cv::threshold(dst, dst, 0, 500, cv::THRESH_TRIANGLE);
cv::morphologyEx(dst, dst, cv::MORPH_ERODE, cv::Mat());
if (inverse)
{
cv::bitwise_not(dst, dst);
}
// find potential squares
std::vector<std::vector<cv::Point>> maybe_squares;
find_squares(dst, maybe_squares);
// find_squares sometimes returns rhombuses so we need to
// "expand" the four corners to be the max x and y values of it.
std::vector<std::vector<cv::Point>> squares;
max_square_edges(maybe_squares, squares);
// get largest square.
std::vector<cv::Point> l_sq;
largest_area(squares, l_sq);
if (l_sq.size() != 4)
{
return ImageDetails();
}
ImageDetails id = ImageDetails();
id.x = l_sq[0].x;
id.y = l_sq[0].y;
id.w = l_sq[3].x - l_sq[1].x;
id.h = l_sq[1].x - l_sq[0].x;
return id;
} // detect_inverse_optional
std::vector<cv::Point> detect(cv::Mat src)
{
cv::Mat dst;
src.copyTo(dst);
// apply some filters to get started
cv::cvtColor(dst, dst, cv::COLOR_BGR2GRAY);
cv::medianBlur(dst, dst, 5);
cv::threshold(dst, dst, 0, 500, cv::THRESH_TRIANGLE);
cv::morphologyEx(dst, dst, cv::MORPH_ERODE, cv::Mat());
// at this point, determine if the image is a dark or light mode UI.
// background color must be black for this to work
if (first_row_is_white(dst))
{
cv::bitwise_not(dst, dst);
}
// find potential squares
std::vector<std::vector<cv::Point>> maybe_squares;
find_squares(dst, maybe_squares);
// find_squares sometimes returns rhombuses so we need to
// "expand" the four corners to be the max x and y values of it.
std::vector<std::vector<cv::Point>> squares;
max_square_edges(maybe_squares, squares);
// get largest square.
std::vector<cv::Point> l_sq;
largest_area(squares, l_sq);
return l_sq;
} // detect
double angle(cv::Point pt1, cv::Point pt2, cv::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);
} // angle
void find_squares(cv::Mat &src, std::vector<std::vector<cv::Point>> &squares)
{
squares.clear();
std::vector<std::vector<cv::Point>> contours;
cv::findContours(src, contours, cv::RETR_LIST, cv::CHAIN_APPROX_SIMPLE);
std::vector<cv::Point> approx;
for (size_t i = 0; i < contours.size(); i++)
{
cv::approxPolyDP(contours[i], approx, cv::arcLength(contours[i], true) * 0.02, true);
if (approx.size() == 4 &&
fabs(cv::contourArea(approx)) > 1000 &&
cv::isContourConvex(approx))
{
double maxCosine = 0;
for (int j = 2; j < 5; j++)
{
double cosine = fabs(angle(approx[j % 4], approx[j - 2], approx[j - 1]));
maxCosine = MAX(maxCosine, cosine);
}
if (maxCosine < 0.3)
{
squares.push_back(approx);
}
}
}
} // find_squares
void max_square_edges(std::vector<std::vector<cv::Point>> src, std::vector<std::vector<cv::Point>> &dst)
{
dst.clear();
// first get the max min x and y values for each set of points
for (size_t i = 0; i < src.size(); i++)
{
int max_x = INT_MIN;
int min_x = INT_MAX;
int max_y = INT_MIN;
int min_y = INT_MAX;
for (size_t j = 0; j < src[i].size(); j++)
{
if (max_x == INT_MIN)
max_x = src[i][j].x;
if (min_x == INT_MAX)
max_x = src[i][j].x;
if (max_y == INT_MIN)
max_y = src[i][j].y;
if (min_y == INT_MAX)
min_y = src[i][j].y;
max_x = MAX(max_x, src[i][j].x);
min_x = MIN(min_x, src[i][j].x);
max_y = MAX(max_y, src[i][j].y);
min_y = MIN(min_y, src[i][j].y);
}
std::vector<cv::Point> square_p;
// index 0 - top left, min x, min y
cv::Point top_left;
top_left.x = min_x;
top_left.y = min_y;
square_p.push_back(top_left);
// index 1 - bottom left, min x, max y
cv::Point bottom_left;
bottom_left.x = min_x;
bottom_left.y = max_y;
square_p.push_back(bottom_left);
// index 2 - bottom right, max x, max y
cv::Point bottom_right;
bottom_right.x = max_x;
bottom_right.y = max_y;
square_p.push_back(bottom_right);
// index 3 - top right, max x, min y
cv::Point top_right;
top_right.x = max_x;
top_right.y = min_y;
square_p.push_back(top_right);
dst.push_back(square_p);
}
} // max_square_edges
int avg_color_row(cv::Mat row)
{
// get values as 0-255
std::vector<int> shape = row.reshape(0);
// get average and determine if closer to 0 or 255
size_t len = shape.size();
int total = 0;
for (size_t i = 0; i < shape.size(); i++)
{
total += shape[i];
}
return total / len;
} // avg_color_row
bool first_row_is_white(cv::Mat src)
{
int avg = avg_color_row(src.row(0));
int d_white = abs(255 - avg);
int d_black = abs(0 - avg);
return d_white < d_black;
} // first_row_is_white
void largest_area(std::vector<std::vector<cv::Point>> squares, std::vector<cv::Point> &dst)
{
dst.clear();
int l_area = 0;
std::vector<cv::Point> l_square = squares[0];
// loop over squares
for (size_t i = 0; i < squares.size(); i++)
{
// 3rd point is the bottom right so we can calculate the area off that
int h = squares[i][0].y - squares[i][1].y;
int w = squares[i][1].x - squares[i][2].x;
int area = h * w;
if (i == 0)
{
l_square = squares[i];
l_area = area;
continue;
}
if (area > l_area)
{
l_area = area;
l_square = squares[i];
}
}
dst = l_square;
} // largest_area
} // namespace ImageDetector