-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocScanner.cpp
99 lines (91 loc) · 2.96 KB
/
docScanner.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
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
using namespace cv;
using namespace std;
Mat imgOrg, imgDil, imgGray, imgCanny, imgBlur, preImg, imgWarp;
float w = 620, h = 627;
string path = "Resources/doc1.jpg";
vector<Point> initialPoints, docPoints;
Mat preProcess(Mat img)
{
cvtColor(img, imgGray, COLOR_BGR2GRAY);
GaussianBlur(imgGray, imgBlur, Size(3, 3), 3, 0);
Canny(imgBlur, imgCanny, 25, 75);
Mat kernel = getStructuringElement(MORPH_RECT, Size(2, 2));
dilate(imgCanny, imgDil, kernel);
return imgDil;
}
vector<Point> getContours(Mat preImg)
{
vector<Point> biggestPoints;
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(preImg, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
vector<vector<Point>> cornPoly(contours.size());
vector<Rect> boundRect(contours.size());
string objectType;
int maxArea = 0;
for(int i = 0; i < contours.size(); ++i)
{
int area = contourArea(contours[i]);
if(area >= 17000)
{
float peri = arcLength(contours[i], true);
approxPolyDP(contours[i], cornPoly[i], 0.02*peri, true);
if( area > maxArea && cornPoly[i].size() == 4)
{
biggestPoints = {cornPoly[i][0], cornPoly[i][1], cornPoly[i][2], cornPoly[i][3]};
maxArea = area;
}
drawContours(imgOrg, cornPoly, i, Scalar(255, 0, 255), 2);
}
}
return biggestPoints;
}
void drawPoints(vector<Point> points, Scalar color)
{
for(int i = 0; i < points.size(); ++i)
{
circle(imgOrg, points[i], 10, color, FILLED);
putText(imgOrg, to_string(i), points[i], FONT_HERSHEY_PLAIN, 5, color, 2);
}
}
vector<Point> reorder(vector<Point> points)
{
vector<Point> newPoints;
vector<int> sumPoints, subPoints;
for(int i = 0; i < 4; ++i)
{
sumPoints.push_back(points[i].x + points[i].y);
subPoints.push_back(points[i].x - points[i].y);
}
newPoints.push_back(points[min_element(sumPoints.begin(), sumPoints.end()) - sumPoints.begin()]);
newPoints.push_back(points[max_element(subPoints.begin(), subPoints.end()) - subPoints.begin()]);
newPoints.push_back(points[min_element(subPoints.begin(), subPoints.end()) - subPoints.begin()]);
newPoints.push_back(points[max_element(sumPoints.begin(), sumPoints.end()) - sumPoints.begin()]);
return newPoints;
}
Mat getWarp(Mat img, vector<Point> docPoints, float w, float h)
{
Point2f src[4] = { docPoints[0], docPoints[1], docPoints[2], docPoints[3] };
Point2f dst[4] = { {0.0f, 0.0f}, {w, 0.0f}, {0.0f, h}, {w, h} };
Mat matrix = getPerspectiveTransform(src, dst);
warpPerspective(img, imgWarp, matrix, Point(w, h));
return imgWarp;
}
int main()
{
imgOrg = imread(path);
//Preprocess
preImg = preProcess(imgOrg);
initialPoints = getContours(preImg);
docPoints = reorder(initialPoints);
//drawPoints(docPoints, Scalar(0, 0, 255));
imgWarp = getWarp(imgOrg, docPoints, w, h);
imshow("points", imgWarp);
waitKey(0);
return 0;
}