-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvision.cpp
More file actions
476 lines (380 loc) · 10.9 KB
/
Copy pathvision.cpp
File metadata and controls
476 lines (380 loc) · 10.9 KB
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
#pragma once
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include "config.cpp"
#include "cube.cpp"
#include "utils.cpp"
#include "solution.cpp"
/**
* Color ranges in HSV used for filtering the color of the cube faces.
*/
const cv::Scalar ranges[12] = {
WHITE_MIN,
WHITE_MAX,
BLUE_MIN,
BLUE_MAX,
YELLOW_MIN,
YELLOW_MAX,
GREEN_MIN,
GREEN_MAX,
ORANGE_MIN,
ORANGE_MAX,
RED_MIN,
RED_MAX
};
/**
* Represents a quad detected in the image.
*/
struct Quad {
/**
* List of points that compose the quad.
*
* Must always be composed of four points
*/
std::vector<cv::Point> points;
/**
* Color of the quad.
*
* Based on the rubix cube color faces
*/
int color = 7;
/**
* Get the center of the quad.
*/
cv::Point center() {
cv::Point center = cv::Point(0.0, 0.0);
for (int i = 0; i < this->points.size(); i++)
{
center += this->points[i];
}
center /= float(this->points.size());
return center;
}
};
/**
* Compares quads based on their position.
*
* Used to sort quads by their coordinates from top to bottom, left to right.
*/
bool quad_sort(Quad a, Quad b) {
const float tolerance = CONFIG_SORT_Y_TOL;
cv::Point a_p = a.center();
cv::Point b_p = b.center();
if (abs(a_p.y - b_p.y) > tolerance) {
return a_p.y < b_p.y;
}
return a_p.x < b_p.x;
}
enum State {
IDLE,
SCANNING,
SOLVE
};
cv::Scalar CubeColors[] = {
cv::Scalar(255, 255, 255), // W
cv::Scalar(255, 0, 0), // B
cv::Scalar(0, 255, 255), // Y
cv::Scalar(0, 255, 0), // G
cv::Scalar(0, 140, 230), // O
cv::Scalar(0, 0, 255), // R
cv::Scalar(0, 0, 0)
};
class Vision {
public:
/**
* Cube object to store result in.
*/
Cube cube;
/**
* Solution to the cube. Needs to be manually triggered.
*/
CubeSolution sol;
/**
* State of the vision system.
*/
State state;
/**
* Number of iteration used to solve the cube.
*/
int iterations = 5;
Vision() {
this->cube = Cube();
this->sol = CubeSolution();
this->state = SCANNING;
}
/**
* @brief Start the vision module to detect cube.
*/
void detect() {
cv::VideoCapture cap(CONFIG_CAMERA);
if (!cap.isOpened())
{
std::cout << "Error accessing camera." << std::endl;
return;
}
// Read the image file
cv::Mat image;
// Create window
std::string window = CONFIG_WINDOW;
cv::namedWindow(window);
while (true) {
// Capture image camera
cap >> image;
// Check for failure
if (image.empty())
{
std::cout << "Could not open or find the image" << std::endl;
return;
}
// Detect quads
std::vector<Quad> quads = this->quads(image);
// Figure which quad belongs to witch face
if (quads.size() == 9) {
// Sort based on x, y positions
std::sort(quads.begin(), quads.end(), quad_sort);
// Check colors of each quad
for (int i = 0; i < quads.size(); i++)
{
Quad quad = quads[i];
// Create mask of the square
cv::Mat mask = cv::Mat::zeros(image.rows, image.cols, CV_8U);
std::vector<std::vector<cv::Point>> contours;
contours.push_back(quad.points);
cv::drawContours(mask, contours, 0, cv::Scalar(255, 255, 255), cv::FILLED);
// Mask draw
// cv::imshow("Mask", mask);
// Filter square using mask
cv::Mat square;
image.copyTo(square, mask);
// Area of the square
int area_square = cv::countNonZero(mask);
// Amount of area to accept the color
const float area_threshold = CONFIG_AREA_THRESHOLD;
for (int j = 0; j < 6; j++) {
cv::Mat filter = this->segment_colors(square, ranges[j * 2], ranges[j * 2 + 1]);
int area_filter = cv::countNonZero(filter);
float ratio = float(area_filter) / float(area_square);
// Accept color
if (ratio > area_threshold) {
quads[i].color = j;
break;
}
}
}
// Check if the color of all cells was detected
bool complete = true;
for (int j = 0; j < 9; j++) {
if (quads[j].color == 7) {
complete = false;
}
}
// Fill information of the cube face
if (complete) {
// Center color define the face to fill info
int face = quads[4].color;
// Copy data from the detected model to the cube face
for (int j = 0; j < 9; j++) {
this->cube.cube[face][j] = quads[j].color;
}
}
}
// Debug the cube in action
this->draw_quads(image, quads);
// Info
this->draw_info(image);
this->draw_cube(image);
// Display image
cv::imshow(window, image);
int key = cv::waitKey(1);
// S
if (key == 115) {
CubeSolution sol = CubeSolver::solveBF(cube, this->iterations);
if (sol.solved) {
this->sol = sol;
}
std::cout << sol.toString() << std::endl;
}
// R
else if (key == 114) {this->cube.clear();}
// +
else if (key == 171) {this->iterations++;}
// -
else if (key == 173) {this->iterations--;}
// ESC
else if (key == 27) {break;}
// std::cout << key << std::endl;
}
cv::destroyWindow(window);
}
/**
* Draw textual information to screen.
*/
void draw_info(cv::Mat src) {
cv::Scalar color = cv::Scalar(0, 0, 255);
cv::putText(src, "State: " + std::to_string(this->state), cv::Point(10, 20), cv::FONT_HERSHEY_DUPLEX, 0.5, color, 1, false);
cv::putText(src, "R to Reset", cv::Point(10, 40), cv::FONT_HERSHEY_DUPLEX, 0.5, color, 1, false);
cv::putText(src, "S to Solve", cv::Point(10, 60), cv::FONT_HERSHEY_DUPLEX, 0.5, color, 1, false);
cv::putText(src, "-/+ Solve Depth: " + std::to_string(this->iterations), cv::Point(10, 80), cv::FONT_HERSHEY_DUPLEX, 0.5, color, 1, false);
if (this->sol.solved) {
std::string sol = this->sol.toString();
cv::putText(src, sol, cv::Point(10, src.rows - 30), cv::FONT_HERSHEY_DUPLEX, 0.5, color, 1, false);
} else {
cv::putText(src, "Scan cube and press S to solve", cv::Point(10, src.rows - 30), cv::FONT_HERSHEY_DUPLEX, 0.5, color, 1, false);
}
}
/**
* Draw the cube into the mat.
*/
void draw_cube(cv::Mat src) {
int size = 30;
int space = size * 3;
int x = 130;
int y = 100;
draw_cube_face(src, x, y, 4, size);
draw_cube_face(src, x, y + space, 0, size);
draw_cube_face(src, x, y + space * 2, 5, size);
draw_cube_face(src, x, y + space * 3, 2, size);
draw_cube_face(src, x - space, y + space, 1, size);
draw_cube_face(src, x + space, y + space, 3, size);
}
/**
* Draw the cube face into the matrix
*/
void draw_cube_face(cv::Mat src, int off_x, int off_y, int idx, int s = 20) {
for (int i = 0; i < 9; i++) {
int x = i % 3;
int y = (i / 3);
cv::Point2i offset = cv::Point2i(off_x, off_y);
cv::Scalar color = CubeColors[this->cube.cube[idx][i]];
cv::rectangle(src, cv::Rect(x * s + offset.x, y * s + offset.y, s, s), color, -1);
}
}
/**
* Method to debug the quads detected in the image.
*
* Creates a mat with debug information and displays it to the screen.
*/
void draw_quads(cv::Mat src, std::vector<Quad> quads) {
cv::Scalar color = cv::Scalar(255, 255, 255);
cv::Scalar colors[] = {
cv::Scalar(255, 0, 0),
cv::Scalar(0, 255, 0),
cv::Scalar(0, 0, 255),
cv::Scalar(255, 255, 0)
};
for (int i = 0; i < quads.size(); i++)
{
std::vector<cv::Point> quad = quads[i].points;
// Draw Quad
cv::line(src, quad[0], quad[1], colors[0]);
cv::line(src, quad[1], quad[2], colors[1]);
cv::line(src, quad[2], quad[3], colors[2]);
cv::line(src, quad[3], quad[0], colors[3]);
// Index
cv::putText(src, std::to_string(i), quads[i].center(), cv::FONT_HERSHEY_DUPLEX, 0.5, color, 1, false);
// Color
cv::putText(src, CubeFaceColors[quads[i].color], quad[0], cv::FONT_HERSHEY_DUPLEX, 0.5, color, 1, false);
}
}
/**
* @brief Detect quadrilaters in the image.
*/
std::vector<Quad> quads(cv::Mat src) {
// Convert to grayscale
cv::Mat gray;
cv::cvtColor(src, gray, cv::COLOR_BGR2GRAY);
// Use Canny instead of threshold to catch squares with gradient shading
cv::Mat bw;
cv::Canny(gray, bw, 0, 50, 7);
// Find contours
std::vector<std::vector<cv::Point>> contours;
cv::findContours(bw, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_NONE);
// Quads detected
std::vector<Quad> quads;
for (int i = 0; i < contours.size(); i++)
{
std::vector<cv::Point> contour = contours[i];
const int min_area = CONFIG_MIN_AREA;
const int max_area = CONFIG_MAX_AREA;
// Approximated points
std::vector<cv::Point> approx;
// Approximate contour with accuracy proportional to the contour perimeter
cv::approxPolyDP(contour, approx, cv::arcLength(contours[i], true) * CONFIG_APPROX_TOL, true);
if (approx.size() == 4)
{
// Skip objects by size
int area = cv::contourArea(approx);
if (area < min_area || area > max_area) {
continue;
}
// Skip non-convex objects
if (!cv::isContourConvex(approx)) {
continue;
}
// Length of all edges
std::vector<double> length;
for (int j = 1; j < 4; j++) {
length.push_back(cv::norm(approx[j] - approx[j - 1]));
}
// Similar sized edges (square)
const double max_diff = CONFIG_EDGE_DIFF;
double max = 0.0;
for (int i = 0; i < 4; i++) {
for (int j = i; j < 4; j++) {
double diff = abs(length[j] - length[i]);
if (max < diff) {
max = diff;
}
}
}
if(max > max_diff) {
continue;
}
// Get the cosines of all corners
std::vector<double> cos;
for (int j = 2; j <= 4; j++) {
cos.push_back(angle(approx[j%4], approx[j-2], approx[j-1]));
}
// Filter by the angle of the corners (should be close to 90 deg)
std::sort(cos.begin(), cos.end());
double mincos = cos.front();
double maxcos = cos.back();
if (mincos < CONFIG_MIN_COSINE && maxcos > CONFIG_MAX_COSINE) {
continue;
}
Quad quad = Quad();
quad.points = approx;
// It is a valid quad add to the list~
quads.push_back(quad);
}
}
return quads;
}
/**
* Segment image based on rubix cube face colors.
*/
cv::Mat segment_colors(cv::Mat src, cv::Scalar min, cv::Scalar max) {
const bool debug = false;
// Indicate if the close operation should be applied
const bool close_op = CONFIG_SEGMENT_CLOSE;
// Convert to HLS
cv::Mat hls;
cv::cvtColor(src, hls, cv::COLOR_BGR2HLS);
cv::Mat mask;
cv::inRange(hls, min, max, mask);
if (close_op) {
cv::Mat element = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(7, 7), cv::Point(3, 3));
cv::dilate(mask, mask, element);
cv::erode(mask, mask, element);
}
if (debug) {
cv::Mat rgb;
src.copyTo(rgb, mask);
cv::imshow("Filtered", rgb);
}
return mask;
}
};