-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathscanner.cpp
199 lines (164 loc) · 4.79 KB
/
scanner.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
#include <opencv2/opencv.hpp>
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
using namespace cv;
using namespace std;
bool compareContourAreas(std::vector<cv::Point> contour1, std::vector<cv::Point> contour2)
{
double i = fabs(contourArea(cv::Mat(contour1)));
double j = fabs(contourArea(cv::Mat(contour2)));
return (i > j);
}
bool compareXCords(Point p1, Point p2)
{
return (p1.x < p2.x);
}
bool compareYCords(Point p1, Point p2)
{
return (p1.y < p2.y);
}
bool compareDistance(pair<Point, Point> p1, pair<Point, Point> p2)
{
return (norm(p1.first - p1.second) < norm(p2.first - p2.second));
}
double _distance(Point p1, Point p2)
{
return sqrt(((p1.x - p2.x) * (p1.x - p2.x)) +
((p1.y - p2.y) * (p1.y - p2.y)));
}
void resizeToHeight(Mat src, Mat &dst, int height)
{
Size s = Size(src.cols * (height / double(src.rows)), height);
resize(src, dst, s, INTER_AREA);
}
void orderPoints(vector<Point> inpts, vector<Point> &ordered)
{
sort(inpts.begin(), inpts.end(), compareXCords);
vector<Point> lm(inpts.begin(), inpts.begin() + 2);
vector<Point> rm(inpts.end() - 2, inpts.end());
sort(lm.begin(), lm.end(), compareYCords);
Point tl(lm[0]);
Point bl(lm[1]);
vector<pair<Point, Point>> tmp;
for (size_t i = 0; i < rm.size(); i++)
{
tmp.push_back(make_pair(tl, rm[i]));
}
sort(tmp.begin(), tmp.end(), compareDistance);
Point tr(tmp[0].second);
Point br(tmp[1].second);
ordered.push_back(tl);
ordered.push_back(tr);
ordered.push_back(br);
ordered.push_back(bl);
}
void fourPointTransform(Mat src, Mat &dst, vector<Point> pts)
{
vector<Point> ordered_pts;
orderPoints(pts, ordered_pts);
double wa = _distance(ordered_pts[2], ordered_pts[3]);
double wb = _distance(ordered_pts[1], ordered_pts[0]);
double mw = max(wa, wb);
double ha = _distance(ordered_pts[1], ordered_pts[2]);
double hb = _distance(ordered_pts[0], ordered_pts[3]);
double mh = max(ha, hb);
Point2f src_[] =
{
Point2f(ordered_pts[0].x, ordered_pts[0].y),
Point2f(ordered_pts[1].x, ordered_pts[1].y),
Point2f(ordered_pts[2].x, ordered_pts[2].y),
Point2f(ordered_pts[3].x, ordered_pts[3].y),
};
Point2f dst_[] =
{
Point2f(0, 0),
Point2f(mw - 1, 0),
Point2f(mw - 1, mh - 1),
Point2f(0, mh - 1)};
Mat m = getPerspectiveTransform(src_, dst_);
warpPerspective(src, dst, m, Size(mw, mh));
}
void preProcess(Mat src, Mat &dst)
{
cv::Mat imageGrayed;
cv::Mat imageOpen, imageClosed, imageBlurred;
cvtColor(src, imageGrayed, COLOR_BGR2GRAY);
cv::Mat structuringElmt = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(4, 4));
morphologyEx(imageGrayed, imageOpen, cv::MORPH_OPEN, structuringElmt);
morphologyEx(imageOpen, imageClosed, cv::MORPH_CLOSE, structuringElmt);
GaussianBlur(imageClosed, imageBlurred, Size(7, 7), 0);
Canny(imageBlurred, dst, 75, 100);
}
string getOutputFileName(string path, string name)
{
fs::path filePath = path;
return filePath.stem().string() + "_" + name + filePath.extension().string();
}
int main(int argc, char **argv)
{
static const char *const keys = "{ @image |<none>| }";
CommandLineParser parser(argc, argv, keys);
if (!parser.has("@image"))
{
parser.printMessage();
return -1;
}
printf("OpenCV version: %s\n", CV_VERSION);
string image_name(parser.get<String>("@image"));
Mat image = imread(image_name);
if (image.empty())
{
printf("Cannot read image file: %s\n", image_name.c_str());
return -1;
}
double ratio = image.rows / 500.0;
Mat orig = image.clone();
resizeToHeight(image, image, 500);
Mat gray, edged, warped;
preProcess(image, edged);
#ifndef NDEBUG
imwrite(getOutputFileName(image_name, "edged"), edged);
#endif
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
vector<vector<Point>> approx;
findContours(edged, contours, hierarchy, RETR_LIST, CHAIN_APPROX_SIMPLE);
approx.resize(contours.size());
size_t i, j;
for (i = 0; i < contours.size(); i++)
{
double peri = arcLength(contours[i], true);
approxPolyDP(contours[i], approx[i], 0.02 * peri, true);
}
sort(approx.begin(), approx.end(), compareContourAreas);
for (i = 0; i < approx.size(); i++)
{
drawContours(image, approx, i, Scalar(255, 255, 0), 2);
if (approx[i].size() == 4)
{
break;
}
}
if (i < approx.size())
{
drawContours(image, approx, i, Scalar(0, 255, 0), 2);
#ifndef NDEBUG
imwrite(getOutputFileName(image_name, "outline"), image);
#endif
for (j = 0; j < approx[i].size(); j++)
{
approx[i][j] *= ratio;
}
fourPointTransform(orig, warped, approx[i]);
#ifndef NDEBUG
imwrite(getOutputFileName(image_name, "flat"), warped);
#endif
cvtColor(warped, warped, COLOR_BGR2GRAY, 1);
adaptiveThreshold(warped, warped, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 9, 15);
GaussianBlur(warped, warped, Size(3, 3), 0);
imwrite(getOutputFileName(image_name, "scanned"), warped);
}
}