-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudoku.cpp
195 lines (151 loc) · 5.17 KB
/
sudoku.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
#include "sudoku.hpp"
extern "C" {
#include "neural_network.h"
#include "repository.h"
#include "utils.h"
}
#include "opencv2/opencv.hpp"
using namespace cv;
using namespace std;
int main(int argc, char* argv[]) {
if(argc != 2) {
cout << "Expected 1 argument : the image path";
return EXIT_FAILURE;
}
cout << find_sudoku(argv[1]);
return EXIT_SUCCESS;
}
Mat preprocess(const Mat &img);
tuple<vector<Point>, double> biggest_contours(const vector<vector<Point>>& contours);
void reorder(vector<Point> points);
Mat remove_lines_static(Mat wholeImg, int lowerRow, int upperRow, int lowerColumn, int upperColumn);
vector<Mat> split_boxes(Mat img, Mat (*remove_lines)(Mat, int, int, int, int));
char predict(neural_network* network, const Mat &img);
inline string find_sudoku(const string &filename) {
const Mat img = imread(filename);
const Mat threshold = preprocess(img);
vector<vector<Point>> contours;
findContours(threshold, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
auto [biggest, maxArea] = biggest_contours(contours);
if(biggest.empty()) return "_No valid area found";
reorder(biggest); //error here TODO
const vector objective{Point(0, 0), Point(450, 0), Point(0, 450), Point(450, 450)};
const Mat matrix = getPerspectiveTransform(biggest, objective);
Mat warp;
warpPerspective(img, warp, matrix, Size(450, 450));
vector<Mat> boxes = split_boxes(warp, remove_lines_static);
neural_network* network = initialize("", nullptr);
string result;
for(const auto& box : boxes) {
result += predict(network, box);
}
return result;
}
Mat preprocess(const Mat &img) {
Mat gray, blur, threshold;
cvtColor(img, gray, COLOR_BGR2GRAY);
GaussianBlur(gray, blur, Size(3, 3), 1);
adaptiveThreshold(blur, threshold, 255, 1, 1, 11, 2);
return threshold;
}
tuple<vector<Point>, double> biggest_contours(const vector<vector<Point>>& contours) {
vector<Point> biggest;
double maxArea = 0;
for (const auto& contour : contours) {
const double area = contourArea(contour);
if(area < 30 || area < maxArea) continue; //TODO adapt to image size
const double peri = arcLength(contour, true);
vector<Point> approx;
approxPolyDP(contour, approx, 0.02 * peri, true);
if(approx.size() != 4) continue;
biggest = approx;
maxArea = area;
}
return {biggest, maxArea};
}
void lower_first(Point arr[]) {
if(arr[0].y > arr[1].y) {
const Point buffer = arr[0];
arr[0] = arr[1];
arr[1] = buffer;
}
}
void reorder(vector<Point> points) {
Point left[2];
Point right[2];
int lCursor = 0;
int rCursor = 0;
for(int i = 0; i < 4; i++) {
int l = 0;
int r = 0;
const Point& curr = points.at(i);
for(int j = i + 1; j < 4; j++) {
if(curr.x < points.at(j).x) l++;
else r++;
}
if(l > r) left[lCursor++] = curr;
else if(r > l) right[rCursor++] = curr;
else if(lCursor > rCursor) right[rCursor++] = curr;
else left[lCursor++] = curr;
}
lower_first(left);
lower_first(right);
points[0] = left[0];
points[1] = right[0];
points[2] = left[1];
points[3] = right[1];
}
Mat remove_lines_static(Mat wholeImg, int lowerRow, int upperRow, int lowerColumn, int upperColumn) {
const int rDelta = upperRow - lowerRow;
lowerRow += rDelta;
upperRow -= rDelta;
const int cDelta = upperColumn - lowerColumn;
lowerColumn += cDelta;
upperColumn -= cDelta;
Mat result(Size(upperRow - lowerRow, upperColumn - lowerColumn), CV_8UC1);
for(int r = lowerRow; r < upperRow; r++) {
for(int c = lowerColumn; c < upperColumn; c++) {
result.at<unsigned char>(r - lowerRow, c - lowerColumn) = wholeImg.at<unsigned char>(r, c);
}
}
return result;
}
vector<Mat> split_boxes(Mat img, Mat (*remove_lines)(Mat, int, int, int, int)) {
vector<Mat> result;
const int rSize = img.rows / 9;
const int cSize = img.cols / 9;
for(int r = 0; r < img.rows; r += rSize) {
for(int c = 0; c < img.cols; c += cSize) {
result.push_back(remove_lines(img, r, r + rSize, c, c + cSize));
}
}
return result;
}
bool isEmpty(Mat img) {
for(int r = 0; r < img.rows; r++) {
for(int c = 0; c < img.cols; c++) {
if(img.at<unsigned char>(r, c) < 250) return false;
}
}
return true;
}
input_data* to_input(Mat mat) {
input_data* result = alloc_input_data(mat.rows * mat.cols);
for(int r = 0; r < mat.rows; r++) {
for(int c = 0; c < mat.cols; c++) {
const unsigned char v = mat.at<unsigned char>(r, c);
result->values[r * mat.cols + c] = static_cast<double>(v) / 255.0;
}
}
return result;
}
char predict(neural_network* network, const Mat &img) {
if(isEmpty(img)) return '0';
//TODO resize to 28, 28
input_data* data = to_input(img);
input_data* prediction = predict(network, data);
char v = static_cast<char>('0' + max_index(prediction->values, prediction->count));
free_input_data(data);
free_input_data(prediction);
return v;
}