-
Notifications
You must be signed in to change notification settings - Fork 0
/
processing.cpp
380 lines (327 loc) · 11.5 KB
/
processing.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
368
369
370
371
372
373
374
375
376
377
378
379
/*
* This file is part of SudokuEz
*
* Copyright (C) 2012-2017 Zhong Xu
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
//#define SUDOKU_DEBUG
#include <iostream>
#include <sstream>
#include <fstream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <ml.h>
#include <algorithm>
#include <numeric>
#include "box.h"
using namespace std;
using namespace cv;
const int MIN_BOX_SIZE = 200;
const int MAX_APPROX = 10;
const double FITTING_SQUARE_AREA_RATIO = .8;
const int MIN_BOXES_DISTANCE = 3;
const int FEATURE_SIZE = 80;
const int MAX_SAMPLES = 1000;
bool extract_feature(Mat, float [], Mat&);
void morphology_filter(vector<vector<Point> >& contours, vector<Box>& boxes)
{
boxes.clear();
for (vector<vector<Point> >::iterator pc = contours.begin();
pc < contours.end(); pc++)
{
//reject contours with too small perimeter
if (contourArea(*pc) < MIN_BOX_SIZE)
continue;
vector<Point> poly_contour;
for (int approx = 0; approx < MAX_APPROX; approx++)
{
approxPolyDP(*pc, poly_contour, approx, true);
approxPolyDP(poly_contour, poly_contour, approx, true);
//only 4-side polygons are accepted
if (poly_contour.size() != 4)
continue;
Box box(poly_contour);
//polygons are approx square
int length = box.get_sidelength();
double area = box.get_area();
double rarea = pow(length, 2);
if (area / rarea < FITTING_SQUARE_AREA_RATIO)
continue;
boxes.push_back(box);
}
}
}
void majority_filter(vector<Box>& boxes)
{
double sum_sl = 0.0;
for (vector<Box>::iterator ib = boxes.begin();
ib < boxes.end(); ib++)
{
sum_sl += ib->get_sidelength();
}
double avg_sl = sum_sl / boxes.size();
for (vector<Box>::iterator ib = boxes.begin(); ib < boxes.end();)
{
double var = abs(ib->get_sidelength() / avg_sl - 1);
if (var > 0.3)
ib = boxes.erase(ib);
else
ib += 1;
}
}
double dist(Point a, Point b)
{
return sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2));
}
void distinct_filter(vector<Box>& boxes)
{
for (vector<Box>::iterator ib1 = boxes.begin(); ib1 < boxes.end(); ib1++)
{
for (vector<Box>::iterator ib2 = ib1 + 1; ib2 < boxes.end();)
{
if (dist(ib1->get_center(), ib2->get_center()) < MIN_BOXES_DISTANCE)
ib2 = boxes.erase(ib2);
else
ib2 += 1;
}
}
}
//get boxes' offsets from one original box.
bool get_offset(Box* origin, Point pos, vector<Box>& boxes,
double& length, map<Box*, Point >& offset,
int& min_x, int& min_y, int& max_x, int& max_y)
{
int x0 = pos.x, y0 = pos.y;
vector<Box*> visited;
//search neighbour boxes from near to far
for (int l = 1; l < 9; l++)//9 levels(distance)
{
//l =
//9999999999999999999
//9888888888888888889
//...
//9876543222223456789
//9876543211123456789
//9876543210123456789
//9876543211123456789
//9876543222223456789
//...
//9888888888888888889
//9999999999999999999
for (int i = 0; i < 4; i++)// 4 directions
{
//i =
//30000
//33001
//33*11
//32211
//22221
for (int da = (i<2?-1:1) * (l - 1); da != (i<2?1:-1) * (l + 1); da += (i<2?1:-1))
{
//get dx, dy from l and i
int dp = ((i+1)%4<2?-1:1) * l;
int dx, dy;
if (i%2 == 0) { dx = da; dy = dp; } // x-axis change
else { dx = dp; dy = da; } // y-axis change
bool has_visited = false;
for (map<Box*, Point >::iterator io = offset.begin();
io != offset.end(); io++)
{
Point visited_pos = io->second;
if (dx + x0 == visited_pos.x && dy + y0 == visited_pos.y)
{
has_visited = true;
break;
}
}
if (has_visited)
continue;
double dl = sqrt(dx * dx + dy * dy);
for (vector<Box>::iterator ib = boxes.begin(); ib < boxes.end(); ib++)
{
Point exp = origin->get_center() + Point((int)(dx * length), (int)(dy * length));//expected pos
double error = dist(ib->get_center(), exp);
if (error < length * .2 || error < MIN(dl * length * .1, error < length * .9))
{ //captured
offset[&(*ib)] = Point(x0 + dx, y0 + dy);
visited.push_back(&(*ib));
if (x0 + dx < min_x) min_x = x0 + dx;
if (x0 + dx > max_x) max_x = x0 + dx;
if (y0 + dy < min_y) min_y = y0 + dy;
if (y0 + dy > max_y) max_y = y0 + dy;
//adjust length
length = dist(ib->get_center(), origin->get_center()) / dl;
}
}
}
}
}
if (max_x - min_x == 8 && max_y - min_y == 8)
return true;
// if we can not find boxes on all boundaries, try to search recursively from another box.
for (vector<Box*>::reverse_iterator ib = visited.rbegin(); ib < visited.rend(); ib++)
{
Point pos = offset[*ib];
bool result = get_offset(*ib, pos, boxes,
length, offset,
min_x, min_y, max_x, max_y);
if (result == true)
return true;
}
return false;
}
//calculate cof_mat from offset by the least squares technique. the cof_mat is used to obtain fitted coords.
Mat get_cof_mat(Box* origin, map<Box*, Point>& offset)
{
Mat m(offset.size(), 2, CV_64FC1), s(offset.size(), 2, CV_64FC1);
int x0 = origin->get_center().x;
int y0 = origin->get_center().y;
int row = 0;
for (map<Box*, Point>::iterator io = offset.begin(); io != offset.end(); io++)
{
Box* box = io->first;
int offx = io->second.x;
int offy = io->second.y;
int x = box->get_center().x - x0;
int y = box->get_center().y - y0;
m.at<double>(row, 0) = offx;
m.at<double>(row, 1) = offy;
s.at<double>(row, 0) = x;
s.at<double>(row, 1) = y;
row += 1;
#ifdef SUDOKU_DEBUG
cout << box->num << "\t" << offx << "\t" << offy << endl;
#endif
}
return (m.t() * m).inv() * m.t() * s;
}
Point get_fitted_coord(Point pos, Mat cof, Point origin_pos)
{
Mat pos_mat(pos);
pos_mat.convertTo(pos_mat, CV_64FC1);
Mat result = pos_mat.t() * cof;
return Point((int)result.at<double>(0, 0), (int)result.at<double>(0, 1)) + origin_pos;
}
#ifdef SUDOKU_DEBUG
void show_boxes(Mat img, vector<Box>& boxes, Scalar color)
{
vector<vector<Point> > boxes_contours;
for (vector<Box>::iterator ib = boxes.begin(); ib < boxes.end(); ib++)
{
boxes_contours.push_back(ib->get_contour());
}
drawContours(img, boxes_contours, -1, color, 3);
namedWindow("selected boxes", CV_WINDOW_NORMAL);
imshow("selected boxes", img);
waitKey(0);
}
#endif
bool get_cropped_imgs(Mat img, Mat cropped_imgs[], Rect rects[], vector<Box>& detected_boxes)
{
//convert to binary
Mat gray, bin;
cvtColor(img, gray, CV_BGR2GRAY);
int block_size = (int)(MIN(img.cols,img.rows) * 0.1)|1;
adaptiveThreshold( gray, bin, 255,
CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY_INV, block_size, 3 );
//dilate img
Mat dil_bin;
dilate(bin, dil_bin, Mat(), Point(-1,-1), 2);
#ifdef SUDOKU_DEBUG
cout << "block_size = " << block_size << endl;
namedWindow("dil_bin", CV_WINDOW_NORMAL);
imshow("dil_bin", dil_bin);
waitKey(0);
#endif
//find contours of img
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours(dil_bin, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
#ifdef SUDOKU_DEBUG
cout << contours.size() << " contours found.\n";
Mat img1 = img.clone();
drawContours(img1, contours, -1, Scalar(0, 0, 255), 1);
namedWindow("contours", CV_WINDOW_NORMAL);
imshow("contours", img1);
waitKey(0);
#endif
//get boxes from contours
vector<Box> boxes;
morphology_filter(contours, boxes);
#ifdef SUDOKU_DEBUG
show_boxes(img.clone(), boxes, Scalar(255, 0, 0));
#endif
detected_boxes = boxes;
majority_filter(boxes);
#ifdef SUDOKU_DEBUG
show_boxes(img.clone(), boxes, Scalar(0, 255, 0));
#endif
distinct_filter(boxes);
#ifdef SUDOKU_DEBUG
show_boxes(img.clone(), boxes, Scalar(0, 0, 255));
#endif
#ifdef SUDOKU_DEBUG
fstream fout;
fout.open("debug/coords.txt", fstream::out);
for (vector<Box>::iterator ib = boxes.begin(); ib < boxes.end(); ib++)
{
vector<Point> contour = ib->get_contour();
Point center = ib->get_center();
fout << ib->num << " " << center.x << " " << center.y << " ";
for (vector<Point>::iterator ip = contour.begin(); ip < contour.end(); ip++)
{
fout << ip->x << " " << ip->y << " ";
}
fout << endl;
}
fout.close();
#endif
if (boxes.size() == 0)
return false;
//get offset of important boxes by finding neighbours of boxes
Box* origin = &boxes[0];
double length = origin->get_sidelength() * 1.1;
map<Box*, Point > offset;
offset[origin] = Point(0, 0);
int min_offx = 0, min_offy = 0, max_offx = 0, max_offy = 0;
bool succeed = get_offset(origin, offset[origin], boxes,
length, offset,
min_offx, min_offy, max_offx, max_offy);
if (succeed)
{
Mat cof = get_cof_mat(origin, offset);
int r = (int)(length / 2);
for (int y = 0; y < 9; y++)
{
int dy = y + min_offy;
for (int x = 0; x < 9; x++)
{
int dx = x + min_offx;
Point fp = get_fitted_coord(Point(dx, dy), cof, origin->get_center());
cropped_imgs[y * 9 + x] = img.rowRange(fp.y - r, fp.y + r).colRange(fp.x - r, fp.x + r);
rects[y * 9 + x] = Rect(fp.x - r, fp.y - r, 2 * r, 2 * r);
}
}
}
return succeed;
}