-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSquareExtractor.cpp
More file actions
58 lines (39 loc) · 1.31 KB
/
Copy pathSquareExtractor.cpp
File metadata and controls
58 lines (39 loc) · 1.31 KB
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
#include "SquareExtractor.h"
#include "vector"
using namespace cv;
using namespace std;
std::vector<cv::Point2f> SquareExtractor::getSquareVertex(cv::InputArray _src, ExtractOption option)
{
double e_approxPolyDP = 0.02;
Mat src = _src.getMat();
Mat bin;
//color region to 255, dark side to 0
threshold(src, bin, 127, 255, THRESH_BINARY | THRESH_OTSU);
//open calc, for reduce noise
cv::Mat element5(5, 5, CV_8U, cv::Scalar(1));
morphologyEx(bin, bin, MORPH_OPEN, element5);
vector<vector<Point>> contours;
double bestApproxSize = -1;
vector<Point2f> bestApprox;
findContours(bin, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
for (int i = 0; i < contours.size(); i++) {
double approxSize;
vector<Point2f> approx;
approxPolyDP(contours[i], approx, arcLength(contours[i], true) * e_approxPolyDP, true);
approxSize = contourArea(approx, false);
//find largest rect
if (bestApproxSize < approxSize) {
bestApprox = approx;
bestApproxSize = approxSize;
}
}
std::vector<cv::Point> result;
//if (bestApprox.size() == 4) {
return bestApprox;
/* }
else {
//TODO, err handle
return std::vector<cv::Point2f>();
}
*/
}