-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathtest_EdgesSubPix.cpp
88 lines (78 loc) · 2.86 KB
/
test_EdgesSubPix.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
#include <opencv2/opencv.hpp>
#include <opencv2/core/ocl.hpp>
#include <iostream>
#include "EdgesSubPix.h"
using namespace cv;
using namespace std;
int main(int argc, char *argv[])
{
ocl::setUseOpenCL(false);
const String keys =
"{help h usage ? | | print this message }"
"{@image | | image for edge detection }"
"{@output |edge.tiff | image for draw contours }"
"{data | | edges data in txt format }"
"{low |20 | low threshold }"
"{high |40 | high threshold }"
"{mode |1 | same as cv::findContours }"
"{alpha |1.0 | gaussian alpha }";
CommandLineParser parser(argc, argv, keys);
parser.about("subpixel edge detection");
if (!parser.check())
{
parser.printErrors();
return 0;
}
if (parser.has("help"))
{
parser.printMessage();
return 0;
}
if (!parser.has("@image"))
{
parser.printMessage();
return 0;
}
String imageFile = parser.get<String>(0);
String outputFile = parser.get<String>("@output");
int low = parser.get<int>("low");
int high = parser.get<int>("high");
double alpha = parser.get<double>("alpha");
int mode = parser.get<int>("mode");
Mat image = imread(imageFile, IMREAD_GRAYSCALE);
vector<Contour> contours;
vector<Vec4i> hierarchy;
int64 t0 = getCPUTickCount();
EdgesSubPix(image, alpha, low, high, contours, hierarchy, mode);
int64 t1 = getCPUTickCount();
cout << "execution time is " << (t1 - t0) / (double)getTickFrequency() << " seconds" << endl;
if (parser.has("data"))
{
FileStorage fs(parser.get<String>("data"), FileStorage::WRITE | FileStorage::FORMAT_YAML);
fs << "contours" << "[";
for (size_t i = 0; i < contours.size(); ++i)
{
fs << "{:";
fs << "points" << contours[i].points;
fs << "response" << contours[i].response;
fs << "direction" << contours[i].direction;
fs << "}";
}
fs << "]";
fs.release();
}
if (parser.has("@output")) {
cv::Mat colorImage;
cv::cvtColor(image, colorImage, COLOR_GRAY2BGR);
vector<vector<Point>> ocvContours;
for (size_t i = 0; i < contours.size(); ++i) {
cv::Mat pts(contours[i].points);
vector<Point> c;
pts.convertTo(c, CV_32S);
ocvContours.push_back(std::move(c));
}
cv::drawContours(colorImage, ocvContours, -1, cv::Scalar(0, 255, 0));
cv::imwrite(parser.get<String>("@output"), colorImage);
}
return 0;
}