lsd_lines.cpp 2.1 KB

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