-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCardRecognizer.cpp
executable file
·362 lines (308 loc) · 9.15 KB
/
CardRecognizer.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
#include "opencv2/opencv.hpp"
#include <time.h>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include "opencv2/core/core.hpp"
#include <stdio.h>
using namespace cv;
using namespace std;
///global templates
Mat naipes[3];
Mat card_symbols[13];
void MatchingMethod(Mat card);
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);
}
void loadtempls() {
///naipes
/*
0 -> ouros,
1 -> copas,
2 -> espadas,
3 -> paus,
4 -> Ás ouros
*/
naipes[0] = imread("naipes/Diamonds.png");
naipes[1] = imread("naipes/Hearts.png");
naipes[2] = imread("naipes/Spades.png");
naipes[3] = imread("naipes/Clubs.png");
naipes[4] = imread("naipes/As.png");
///simbolos
/*
0 -> Ás,
1 -> 2,
2 -> 3,
3 -> 4,
4 -> 5,
5 -> 6,
6 -> 7,
7 -> 8,
8 -> 9,
9 -> 10,
10 -> Q,
11 -> J,
12 -> K
*/
card_symbols[0] = imread("cards/As.png");
card_symbols[1] = imread("cards/dois.png");
card_symbols[2] = imread("cards/tres.png");
card_symbols[3] = imread("cards/quatro.png");
card_symbols[4] = imread("cards/cinco.png");
card_symbols[5] = imread("cards/seis.png");
card_symbols[6] = imread("cards/sete.png");
card_symbols[7] = imread("cards/oito.png");
card_symbols[8] = imread("cards/nove.png");
card_symbols[9] = imread("cards/dez.png");
card_symbols[10] = imread("cards/dama.png");
card_symbols[11] = imread("cards/valete.png");
card_symbols[12] = imread("cards/rei.png");
return;
}
int main(int, char**)
{
loadtempls();
VideoCapture cap(1); // open the default camera
if (!cap.isOpened()) // check if we succeeded
return -1;
for (;;) {
Mat edges;
Mat frame;
cap >> frame; // get a new frame from camera
cvtColor(frame, edges, COLOR_BGR2GRAY);
cv::threshold(edges, edges, 128, 255, CV_THRESH_BINARY);
std::vector<std::vector<cv::Point> > contours;
std::vector<Mat> cards = vector<Mat>();;
cv::Mat contourOutput = edges.clone();
cv::findContours(contourOutput, contours, CV_RETR_LIST, CV_CHAIN_APPROX_NONE);
cv::Mat contourImage(edges.size(), CV_8UC3, cv::Scalar(0, 0, 0));
///new
std::vector<std::vector<cv::Point2f> > squares;
std::vector<cv::Point2f> approx;
for (size_t idx = 0; idx < contours.size(); idx++) {
cv::approxPolyDP(cv::Mat(contours[idx]), approx, cv::arcLength(cv::Mat(contours[idx]), true)*0.02, true);
if (approx.size() == 4 && std::fabs(contourArea(cv::Mat(approx))) > 1000 &&
cv::isContourConvex(cv::Mat(approx)))
{
double maxCosine = 0;
for (int j = 2; j < 5; j++)
{
double cosine = std::fabs(angle(approx[j % 4], approx[j - 2], approx[j - 1]));
maxCosine = MAX(maxCosine, cosine);
}
if (maxCosine < 0.3)
squares.push_back(approx);
}
}
if (squares.size() > 0) {
for(size_t ii = 0 ; ii< squares.size(); ii++){
RotatedRect rekt;
int bx = 0, by = 0, sx = 150000, sy = 150000;
int ibx = 0, iby = 0, isx = 0 , isy = 0;
for (size_t i = 0; i < squares[ii].size(); i++) {
cv::circle(frame, squares[ii][i], 4, cv::Scalar(0, 0, 255), cv::FILLED);
if (squares[ii][i].x < sx) {
sx = squares[ii][i].x;
isx = i;
}
if (squares[ii][i].y < sy) {
sy = squares[ii][i].y;
isy = i;
}
if (squares[ii][i].x > bx) {
bx = squares[ii][i].x;
ibx = i;
}
if (squares[ii][i].y > by) {
by = squares[ii][i].y;
iby = i;
}
}
///
/// è preciso criar o rotated rect
rekt = minAreaRect(Mat(squares[ii]));
///
Mat M, rotated, cropped;
// get angle and size from the bounding box
float angle = rekt.angle;
Size rect_size = rekt.size;
if (rekt.angle < -45.) {
angle += 90.0;
swap(rect_size.width, rect_size.height);
}
// get the rotation matrix
M = getRotationMatrix2D(rekt.center, angle, 1.0);
// perform the affine transformation
warpAffine(frame, rotated, M, frame.size(), INTER_CUBIC);
// crop the resulting image
getRectSubPix(rotated, rect_size, rekt.center, cropped);
try {
cards.push_back(cropped);
}
catch (...) {}
}
for (int i = 0; i < cards.size(); i++) {
Mat to_gray;
/// gray and blur
cvtColor(cards[i], to_gray, COLOR_BGR2GRAY);
blur(to_gray, to_gray, Size(3, 3));
//erosao
Mat imageEroded;
Mat structElem = getStructuringElement(MORPH_ELLIPSE, Size(5, 5));
erode(to_gray, imageEroded, structElem, Point(1, 1), 5);
Mat canny_output;
vector<Vec4i> hierarchy;
RNG rng(12345);
/// Detect edges using canny
int value = 40; // we manualy tested out
Canny(to_gray, canny_output, value, value * 2, 3);
/// Find contours
findContours(canny_output, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
/// Draw contours
Mat drawing = Mat::zeros(canny_output.size(), CV_8UC3);
for (size_t i = 0; i< contours.size(); i++)
{
Scalar color = cv::Scalar(255, 255, 255);
drawContours(drawing, contours, (int)i, color, 1, 8, hierarchy, 0, Point());
}
MatchingMethod(drawing);
}
}
//imshow("canny", contourImage);
imshow("frame", frame);
if (waitKey(30) >= 0) break;
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}
string naipe_res[4] = {
"Ouros",
"Copas",
"Espadas",
"Paus"
};
string cards_res[13] = {
"As",
"Dois",
"Tres",
"Quatro",
"Cinco",
"Seis",
"Sete",
"Oito",
"Nove",
"Dez",
"Dama",
"Jota",
"Rei"
};
void MatchingMethod(Mat card)
{
imshow("card", card);
int naipe = -1;
for (int i = 0; i < 5; i++) {
try
{
/// Load image and template
Mat img;
Mat templ;
naipes[i].copyTo(templ);
cvtColor(card, card, CV_BGR2HSV);
cvtColor(templ, templ, CV_BGR2HSV);
threshold(card, card, 255, 255, 255);
threshold(templ, templ, 255, 255, 255);
//imshow("card", card);
/// Source image to display
Mat result;
/// Create the result matrix
int result_cols = card.cols - templ.cols + 1;
int result_rows = card.rows - templ.rows + 1;
result.create(result_rows, result_cols, CV_32FC1);
/// Do the Matching and Normalize
int match_method = CV_TM_SQDIFF_NORMED;
matchTemplate(card, templ, result, match_method);
normalize(result, result, 0, 1, NORM_MINMAX, -1, Mat());
/// Localizing the best match with minMaxLoc
double minVal; double maxVal; Point minLoc; Point maxLoc;
Point matchLoc;
minMaxLoc(result, &minVal, &maxVal, &minLoc, &maxLoc, Mat());
/// For SQDIFF and SQDIFF_NORMED, the best matches are lower values. For all the other methods, the higher the better
if (match_method == CV_TM_SQDIFF || match_method == CV_TM_SQDIFF_NORMED)
{
matchLoc = minLoc;
}
else
{
matchLoc = maxLoc;
}
/// Show me what you got
rectangle(result, matchLoc, Point(matchLoc.x + templ.cols, matchLoc.y + templ.rows), Scalar::all(70), 2, 8, 0);
if (matchLoc.x != 0) {
if (i == 4) {
cout << "encontrei As de ouros" << endl;
return;
}
cout << "encontrei naipe" << naipe_res[naipe] << endl;
naipe = i;
}
}
catch (const std::exception&)
{
cout << "crashou kek" << endl;
}
}
if (naipe == -1)
return;
///simbolos
for (int i = 0; i < 13; i++) {
try
{
/// Load image and template
Mat img;
Mat templ;
card_symbols[i].copyTo(templ);
cvtColor(card, card, CV_BGR2HSV);
cvtColor(templ, templ, CV_BGR2HSV);
threshold(card, card, 255, 255, 255);
threshold(templ, templ, 255, 255, 255);
//imshow("card", card);
/// Source image to display
Mat result;
/// Create the result matrix
int result_cols = card.cols - templ.cols + 1;
int result_rows = card.rows - templ.rows + 1;
result.create(result_rows, result_cols, CV_32FC1);
/// Do the Matching and Normalize
int match_method = CV_TM_SQDIFF_NORMED;
matchTemplate(card, templ, result, match_method);
normalize(result, result, 0, 1, NORM_MINMAX, -1, Mat());
/// Localizing the best match with minMaxLoc
double minVal; double maxVal; Point minLoc; Point maxLoc;
Point matchLoc;
minMaxLoc(result, &minVal, &maxVal, &minLoc, &maxLoc, Mat());
/// For SQDIFF and SQDIFF_NORMED, the best matches are lower values. For all the other methods, the higher the better
if (match_method == CV_TM_SQDIFF || match_method == CV_TM_SQDIFF_NORMED)
{
matchLoc = minLoc;
}
else
{
matchLoc = maxLoc;
}
/// Show me what you got
rectangle(result, matchLoc, Point(matchLoc.x + templ.cols, matchLoc.y + templ.rows), Scalar::all(70), 2, 8, 0);
if (matchLoc.x != 0) {
cout << "Encontrei " << cards_res[i] << " de " << naipe_res[naipe] << endl;
}
}
catch (const std::exception&)
{
cout << "crash" << endl;
}
}
}