@@ -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- }
13276vector<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