-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDocumentContourProcessorAdvanced.cpp
executable file
·178 lines (152 loc) · 5.98 KB
/
DocumentContourProcessorAdvanced.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
#include <iostream>
#include "../include/DocumentContourProcessorAdvanced.h"
#include "../include/Utils.h"
using namespace std;
using namespace cv;
vector<Point> DocumentContourProcessorAdvanced::findContour(Mat &src, bool showResult) {
Mat gray;
Utils::toGray(src, gray);
if (showResult) { Utils::showImage(gray); }
Mat blurred;
int iterations = 3;
Utils::medianBlur(gray, blurred, iterations);
if (showResult) { Utils::showImage(blurred); }
Mat edges;
Utils::cannyEdges(blurred, edges);
if (showResult) { Utils::showImage(edges); }
Mat dilated;
Utils::dilate(edges, dilated);
if (showResult) { Utils::showImage(dilated); }
std::vector<Vec4i> houghLines = Utils::searchAllHoughLinesP(dilated);
Mat hough;
Utils::initNewMat(src.cols, src.rows, hough);
Utils::drawHoughLines(hough, houghLines);
if (showResult) { Utils::showImage(hough); }
Mat srcCopy;
src.copyTo(srcCopy);
std::vector<Point> intersectionsAll = findValidIntersections(houghLines, src.cols, src.rows);
Utils::showPoints(intersectionsAll, srcCopy);
vector<Point> hullPoints;
Utils::calculateConvexHull(intersectionsAll, hullPoints);
Mat hullMat;
Utils::initNewMat(src.cols, src.rows, hullMat);
if (showResult) { Utils::showPoints(intersectionsAll, hullMat); }
Mat gradient;
Utils::calculateSobelGradient(gray, gradient);
if (showResult) { Utils::showImage(gradient); }
vector<Point> borders = reduceCountOfPointsWithAverage(hullPoints);
Mat bordersMat;
Utils::initNewMat(src.cols, src.rows, bordersMat);
if (showResult) { Utils::showPoints(borders, bordersMat); }
vector<Point> border4Polygon = get4CornersPolygon(borders);
Mat bordersPolygonMat;
Utils::initNewMat(src.cols, src.rows, bordersPolygonMat);
if (showResult) { Utils::showPoints(border4Polygon, bordersPolygonMat); }
vector<Point> preciseContour = Utils::findPreciseContoursThroughPoints(gradient, intersectionsAll, border4Polygon);
Mat srcCopyPrecise;
src.copyTo(srcCopyPrecise);
if (showResult) { Utils::showPoints(preciseContour, srcCopyPrecise); }
return preciseContour;
};
vector<Point> DocumentContourProcessorAdvanced::findValidIntersections(vector<Vec4i>& lines, int cols, int rows){
std::vector<Point> points;
for (int i = 0; i < lines.size(); i++){
Vec4i line = lines[i];
Point2f o1 (line[0], line[1]);
Point2f p1 (line[2], line[3]);
for (int j = i; j < lines.size(); j++){
Vec4i lineCheck = lines[j];
Point2f o2 (lineCheck[0], lineCheck[1]);
Point2f p2 (lineCheck[2], lineCheck[3]);
Point2f intersection;
if (Utils::intersections(o1, p1, o2, p2, intersection)){
double angle = Utils::innerAngle(p1.x, p1.y, p2.x, p2.y, intersection.x, intersection.y);
if ((angle > 80) && (angle < 105)) {
if (intersection.x > 0 && intersection.y > 0 && (intersection.x < cols) && (intersection.y < rows)){
points.push_back(intersection);
}
}
}
}
}
std::cout << "Found " << points.size() << " intersections\n";
return points;
}
vector<Point> DocumentContourProcessorAdvanced::reduceCountOfPointsWithAverage(vector<Point> points) {
std::vector<Point> vec;
std::copy (points.begin(), points.end(), std::back_inserter(vec));
double distanceMin = 50;
bool isProcessing = true;
while(vec.size() > 4 && isProcessing){
Point point1;
Point point2;
bool isBreak = false;
int insertIndex = -1;
for (int i = 0; i < vec.size() - 1 ; i++){
point1 = vec[i];
isBreak = false;
for (int j = i + 1; j < vec.size(); j++){
point2 = vec[j];
double distance = sqrt ((point1.x-point2.x)*(point1.x-point2.x) + (point1.y - point2.y)*(point1.y - point2.y));
if (distance < distanceMin){
vec.erase(vec.begin() + i);
vec.erase(vec.begin() + j - 1);
isBreak = true;
break;
}
}
if (isBreak){
insertIndex = i;
break;
}
}
if (isBreak) {
int x = (abs(point1.x + point2.x)) / 2;
int y = (abs(point1.y + point2.y)) / 2;
Point pointNew(x, y);
vec.insert(vec.begin() + insertIndex,pointNew);
} else {
isProcessing = false;
}
}
return vec;
}
vector<Point> DocumentContourProcessorAdvanced::get4CornersPolygon(vector<Point>& points) {
Point point1;
Point point2;
Point point3;
Point point4;
double maxArea = 0;
std::vector<Point> result;
if (points.size() > 4){
for (int i = 0; i < points.size() - 3; i++){
for (int j = i + 1; j < points.size() - 2; j++){
for (int k = j + 1; k < points.size() - 1; k++){
for (int l = k + 1; l < points.size(); l++){
std::vector<Point> pointsArea;
pointsArea.clear();
pointsArea.push_back(points[i]);
pointsArea.push_back(points[j]);
pointsArea.push_back(points[k]);
pointsArea.push_back(points[l]);
double area = contourArea(pointsArea);
if (area > maxArea){
point1 = points[i];
point2 = points[j];
point3 = points[k];
point4 = points[l];
maxArea = area;
}
}
}
}
}
result.push_back(point1);
result.push_back(point2);
result.push_back(point3);
result.push_back(point4);
} else {
result = points;
}
return result;
}