Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/samples/cpp/lsd_lines.cpp
16337 views
1
#include "opencv2/imgproc.hpp"
2
#include "opencv2/imgcodecs.hpp"
3
#include "opencv2/highgui.hpp"
4
#include <iostream>
5
6
using namespace std;
7
using namespace cv;
8
9
int main(int argc, char** argv)
10
{
11
cv::CommandLineParser parser(argc, argv,
12
"{input i|../data/building.jpg|input image}"
13
"{refine r|false|if true use LSD_REFINE_STD method, if false use LSD_REFINE_NONE method}"
14
"{canny c|false|use Canny edge detector}"
15
"{overlay o|false|show result on input image}"
16
"{help h|false|show help message}");
17
18
if (parser.get<bool>("help"))
19
{
20
parser.printMessage();
21
return 0;
22
}
23
24
parser.printMessage();
25
26
String filename = parser.get<String>("input");
27
bool useRefine = parser.get<bool>("refine");
28
bool useCanny = parser.get<bool>("canny");
29
bool overlay = parser.get<bool>("overlay");
30
31
Mat image = imread(filename, IMREAD_GRAYSCALE);
32
33
if( image.empty() )
34
{
35
cout << "Unable to load " << filename;
36
return 1;
37
}
38
39
imshow("Source Image", image);
40
41
if (useCanny)
42
{
43
Canny(image, image, 50, 200, 3); // Apply Canny edge detector
44
}
45
46
// Create and LSD detector with standard or no refinement.
47
Ptr<LineSegmentDetector> ls = useRefine ? createLineSegmentDetector(LSD_REFINE_STD) : createLineSegmentDetector(LSD_REFINE_NONE);
48
49
double start = double(getTickCount());
50
vector<Vec4f> lines_std;
51
52
// Detect the lines
53
ls->detect(image, lines_std);
54
55
double duration_ms = (double(getTickCount()) - start) * 1000 / getTickFrequency();
56
std::cout << "It took " << duration_ms << " ms." << std::endl;
57
58
// Show found lines
59
if (!overlay || useCanny)
60
{
61
image = Scalar(0, 0, 0);
62
}
63
64
ls->drawSegments(image, lines_std);
65
66
String window_name = useRefine ? "Result - standard refinement" : "Result - no refinement";
67
window_name += useCanny ? " - Canny edge detector used" : "";
68
69
imshow(window_name, image);
70
71
waitKey();
72
return 0;
73
}
74
75