Skip to content

Commit 7c3924f

Browse files
committed
chore: demo algo update
1 parent 42c8aa3 commit 7c3924f

File tree

2 files changed

+19
-153
lines changed

2 files changed

+19
-153
lines changed

demo-snippets/platforms/android/cpp/DocumentDetector.cpp

Lines changed: 9 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -96,53 +96,18 @@ vector<vector<cv::Point>> DocumentDetector::scanPoint(Mat &edged) {
9696
Size size = image.size();
9797
width = size.width;
9898
height = size.height;
99-
cv::Mat blurred;
10099

101-
cv::medianBlur(image, blurred, 9);
102-
cv::Mat threshOutput = cv::Mat(blurred.size(), CV_8U);
103100
std::vector<PointAndArea> squares;
104101
std::vector<PointAndArea> foundSquares;
105-
std::vector<int> indices;
106-
for (int c = 2; c >= 0; c--) {
107-
Mat lBlurred[] = {blurred};
108-
Mat lOutput[] = {threshOutput};
109-
int ch[] = {c, 0};
110-
cv::mixChannels(lBlurred, 1, lOutput, 1, ch, 1);
111-
112-
int thresholdLevel = 3;
113-
114-
int t = 60;
115-
for (int l = thresholdLevel-1 ; l >= 0; l--) {
116-
// for (int l = 0; l < thresholdLevel; l++) {
117-
// if (l == 0) {
118-
// t = 60;
119-
// while (t >= 10) {
120-
// cv::Canny(threshOutput, edged, t, t * 2);
121-
// cv::dilate(edged, edged, cv::Mat(), cv::Point(-1, -1), 2);
122-
// findSquares(
123-
// edged,
124-
// width,
125-
// height,
126-
// foundSquares);
127-
// if (foundSquares.size() > 0) {
128-
// break;
129-
// }
130-
// Call findCannySquares here with appropriate parameters
131-
// t -= 10;
132-
// }
133-
// } else {
134-
cv::threshold(threshOutput, edged, (200 - 175 / (l + 2.0)), 256.0,
135-
cv::THRESH_BINARY);
136-
findSquares(edged, width, height, foundSquares);
137-
// Call findThreshSquares here with appropriate parameters
138-
// }
139-
140-
if (foundSquares.size() > 0) {
141-
// stop as soon as find some
142-
break;
143-
}
144-
}
145-
}
102+
GaussianBlur(image, image, Size(11, 11), 0);
103+
cv::Mat structuringElmt = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(5, 5));
104+
morphologyEx(image, image, cv::MORPH_CLOSE, structuringElmt);
105+
Canny(image, image, 0, 200);
106+
107+
structuringElmt = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(7, 7));
108+
dilate(image, edged, structuringElmt);
109+
findSquares(edged, width, height, foundSquares);
110+
146111

147112
int marge = static_cast<int>(width * 0.01);
148113
std::vector<PointAndArea> squaresProba;
@@ -181,7 +146,6 @@ vector<vector<cv::Point>> DocumentDetector::scanPoint(Mat &edged) {
181146
squares.push_back(squaresProba[id]);
182147
}
183148
}
184-
blurred.release();
185149
image.release();
186150
if (squares.size() > 0) {
187151
sort(squares.begin(), squares.end(), sortByArea);

demo-snippets/platforms/ios/src/DocumentDetector.cpp

Lines changed: 10 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -73,68 +73,12 @@ vector<vector<cv::Point>> DocumentDetector::scanPoint() {
7373
edged.release();
7474
return result;
7575
}
76-
77-
long long DocumentDetector::pointSideLine(Point &lineP1, Point &lineP2, Point &point) {
78-
long x1 = lineP1.x;
79-
long y1 = lineP1.y;
80-
long x2 = lineP2.x;
81-
long y2 = lineP2.y;
82-
long x = point.x;
83-
long y = point.y;
84-
return (x - x1)*(y2 - y1) - (y - y1)*(x2 - x1);
85-
}
86-
vector<cv::Point> DocumentDetector::sortPointClockwise(vector<cv::Point> points) {
87-
if (points.size() != 4) {
88-
return points;
89-
}
90-
91-
Point unFoundPoint;
92-
vector<Point> result = {unFoundPoint, unFoundPoint, unFoundPoint, unFoundPoint};
93-
94-
long minDistance = -1;
95-
for(Point &point : points) {
96-
long distance = point.x * point.x + point.y * point.y;
97-
if(minDistance == -1 || distance < minDistance) {
98-
result[0] = point;
99-
minDistance = distance;
100-
}
101-
}
102-
if (result[0] != unFoundPoint) {
103-
Point &leftTop = result[0];
104-
points.erase(std::remove(points.begin(), points.end(), leftTop));
105-
if ((pointSideLine(leftTop, points[0], points[1]) * pointSideLine(leftTop, points[0], points[2])) < 0) {
106-
result[2] = points[0];
107-
} else if ((pointSideLine(leftTop, points[1], points[0]) * pointSideLine(leftTop, points[1], points[2])) < 0) {
108-
result[2] = points[1];
109-
} else if ((pointSideLine(leftTop, points[2], points[0]) * pointSideLine(leftTop, points[2], points[1])) < 0) {
110-
result[2] = points[2];
111-
}
112-
}
113-
if (result[0] != unFoundPoint && result[2] != unFoundPoint) {
114-
Point &leftTop = result[0];
115-
Point &rightBottom = result[2];
116-
points.erase(std::remove(points.begin(), points.end(), rightBottom));
117-
if (pointSideLine(leftTop, rightBottom, points[0]) > 0) {
118-
result[1] = points[0];
119-
result[3] = points[1];
120-
} else {
121-
result[1] = points[1];
122-
result[3] = points[0];
123-
}
124-
}
125-
126-
if (result[0] != unFoundPoint && result[1] != unFoundPoint && result[2] != unFoundPoint && result[3] != unFoundPoint) {
127-
return result;
128-
}
129-
130-
return points;
131-
}
13276
vector<vector<cv::Point>> DocumentDetector::scanPoint(Mat &edged) {
13377
double width;
13478
double height;
13579
Mat image = resizeImage();
136-
cvtColor(image, image, COLOR_BGR2GRAY);
137-
// convert photo to LUV colorspace to avoid glares caused by lights
80+
// convert photo to LUV colorspace to avoid glares caused by lights
81+
cvtColor(image, image, COLOR_BGR2Luv);
13882
if (imageRotation != 0) {
13983
switch (imageRotation) {
14084
case 90:
@@ -149,62 +93,21 @@ vector<vector<cv::Point>> DocumentDetector::scanPoint(Mat &edged) {
14993

15094
}
15195
}
152-
// cvtColor(image, image, COLOR_BGR2Luv);
15396
Size size = image.size();
15497
width = size.width;
15598
height = size.height;
156-
cv::Mat blurred;
15799

158-
cv::GaussianBlur(image, image, cv::Size(9,9), 0);
159-
// dilate helps to remove potential holes between edge segments
160-
Mat kernel = cv::getStructuringElement(MORPH_RECT,cv::Size(9,9));
161-
cv::morphologyEx(image, image, MORPH_CLOSE, kernel);
162-
cv::Canny(image, image, 0, 84);
163-
// cv::Mat threshOutput = cv::Mat(blurred.size(), CV_8U);
164100
std::vector<PointAndArea> squares;
165101
std::vector<PointAndArea> foundSquares;
166-
std::vector<int> indices;
167-
// for (int c = 2; c >= 0; c--) {
168-
// Mat lBlurred[] = {blurred};
169-
// Mat lOutput[] = {threshOutput};
170-
// int ch[] = {c, 0};
171-
// cv::mixChannels(lBlurred, 1, lOutput, 1, ch, 1);
172-
173-
int thresholdLevel = 3;
102+
GaussianBlur(image, image, Size(11, 11), 0);
103+
cv::Mat structuringElmt = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(5, 5));
104+
morphologyEx(image, image, cv::MORPH_CLOSE, structuringElmt);
105+
Canny(image, image, 0, 200);
174106

175-
int t = 60;
176-
int l =2;
177-
// for (int l = thresholdLevel-1 ; l >= 0; l--) {
178-
// for (int l = 0; l < thresholdLevel; l++) {
179-
// if (l == 0) {
180-
// t = 60;
181-
// while (t >= 10) {
182-
// cv::Canny(threshOutput, edged, t, t * 2);
183-
// cv::dilate(edged, edged, cv::Mat(), cv::Point(-1, -1), 2);
184-
// findSquares(
185-
// edged,
186-
// width,
187-
// height,
188-
// foundSquares);
189-
// if (foundSquares.size() > 0) {
190-
// break;
191-
// }
192-
// Call findCannySquares here with appropriate parameters
193-
// t -= 10;
194-
// }
195-
// } else {
196-
// cv::threshold(blurred, edged, (200 - 175 / (l + 2.0)), 256.0,
197-
// cv::THRESH_BINARY);
198-
findSquares(image, width, height, foundSquares);
199-
// Call findThreshSquares here with appropriate parameters
200-
// }
107+
structuringElmt = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(7, 7));
108+
dilate(image, edged, structuringElmt);
109+
findSquares(edged, width, height, foundSquares);
201110

202-
// if (foundSquares.size() > 0) {
203-
// stop as soon as find some
204-
// break;
205-
// }
206-
// }
207-
// }
208111

209112
int marge = static_cast<int>(width * 0.01);
210113
std::vector<PointAndArea> squaresProba;
@@ -243,7 +146,6 @@ vector<vector<cv::Point>> DocumentDetector::scanPoint(Mat &edged) {
243146
squares.push_back(squaresProba[id]);
244147
}
245148
}
246-
blurred.release();
247149
image.release();
248150
if (squares.size() > 0) {
249151
sort(squares.begin(), squares.end(), sortByArea);
@@ -253,7 +155,7 @@ vector<vector<cv::Point>> DocumentDetector::scanPoint(Mat &edged) {
253155
for (int j = 0; j < points.size(); j++) {
254156
points[j] *= resizeScale;
255157
}
256-
result.push_back(sortPointClockwise(points));
158+
result.push_back(points);
257159
}
258160
return result;
259161

0 commit comments

Comments
 (0)