opencv/samples/cpp/lsd_lines.cpp

74 lines
2.1 KiB
C++
Raw Normal View History

2016-02-15 21:37:29 +08:00
#include "opencv2/imgproc.hpp"
2014-07-04 22:48:15 +08:00
#include "opencv2/imgcodecs.hpp"
2016-02-15 21:37:29 +08:00
#include "opencv2/highgui.hpp"
2017-08-31 21:39:28 +08:00
#include <iostream>
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
2017-08-31 21:39:28 +08:00
cv::CommandLineParser parser(argc, argv,
"{input i|../data/building.jpg|input image}"
"{refine r|false|if true use LSD_REFINE_STD method, if false use LSD_REFINE_NONE method}"
"{canny c|false|use Canny edge detector}"
"{overlay o|false|show result on input image}"
"{help h|false|show help message}");
if (parser.get<bool>("help"))
{
2015-08-01 23:24:23 +08:00
parser.printMessage();
return 0;
}
2017-08-31 21:39:28 +08:00
parser.printMessage();
String filename = parser.get<String>("input");
bool useRefine = parser.get<bool>("refine");
bool useCanny = parser.get<bool>("canny");
bool overlay = parser.get<bool>("overlay");
Mat image = imread(filename, IMREAD_GRAYSCALE);
2016-02-15 21:37:29 +08:00
if( image.empty() )
2017-08-31 21:39:28 +08:00
{
cout << "Unable to load " << filename;
return 1;
}
2016-02-15 21:37:29 +08:00
2017-08-31 21:39:28 +08:00
imshow("Source Image", image);
if (useCanny)
{
Canny(image, image, 50, 200, 3); // Apply Canny edge detector
}
2013-07-22 18:49:33 +08:00
// Create and LSD detector with standard or no refinement.
2017-08-31 21:39:28 +08:00
Ptr<LineSegmentDetector> ls = useRefine ? createLineSegmentDetector(LSD_REFINE_STD) : createLineSegmentDetector(LSD_REFINE_NONE);
2013-07-22 18:49:33 +08:00
double start = double(getTickCount());
vector<Vec4f> lines_std;
2013-07-22 18:49:33 +08:00
// Detect the lines
ls->detect(image, lines_std);
2013-07-22 18:49:33 +08:00
double duration_ms = (double(getTickCount()) - start) * 1000 / getTickFrequency();
std::cout << "It took " << duration_ms << " ms." << std::endl;
2013-07-22 18:49:33 +08:00
// Show found lines
2017-08-31 21:39:28 +08:00
if (!overlay || useCanny)
{
image = Scalar(0, 0, 0);
}
ls->drawSegments(image, lines_std);
String window_name = useRefine ? "Result - standard refinement" : "Result - no refinement";
window_name += useCanny ? " - Canny edge detector used" : "";
imshow(window_name, image);
waitKey();
return 0;
}