fld_lines.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include <iostream>
  2. #include "opencv2/imgproc.hpp"
  3. #include "opencv2/ximgproc.hpp"
  4. #include "opencv2/imgcodecs.hpp"
  5. #include "opencv2/highgui.hpp"
  6. using namespace std;
  7. using namespace cv;
  8. using namespace cv::ximgproc;
  9. int main(int argc, char** argv)
  10. {
  11. string in;
  12. CommandLineParser parser(argc, argv, "{@input|corridor.jpg|input image}{help h||show help message}");
  13. if (parser.has("help"))
  14. {
  15. parser.printMessage();
  16. return 0;
  17. }
  18. in = samples::findFile(parser.get<string>("@input"));
  19. Mat image = imread(in, IMREAD_GRAYSCALE);
  20. if( image.empty() )
  21. {
  22. return -1;
  23. }
  24. // Create FLD detector
  25. // Param Default value Description
  26. // length_threshold 10 - Segments shorter than this will be discarded
  27. // distance_threshold 1.41421356 - A point placed from a hypothesis line
  28. // segment farther than this will be
  29. // regarded as an outlier
  30. // canny_th1 50 - First threshold for
  31. // hysteresis procedure in Canny()
  32. // canny_th2 50 - Second threshold for
  33. // hysteresis procedure in Canny()
  34. // canny_aperture_size 3 - Aperturesize for the sobel operator in Canny().
  35. // If zero, Canny() is not applied and the input
  36. // image is taken as an edge image.
  37. // do_merge false - If true, incremental merging of segments
  38. // will be performed
  39. int length_threshold = 10;
  40. float distance_threshold = 1.41421356f;
  41. double canny_th1 = 50.0;
  42. double canny_th2 = 50.0;
  43. int canny_aperture_size = 3;
  44. bool do_merge = false;
  45. Ptr<FastLineDetector> fld = createFastLineDetector(length_threshold,
  46. distance_threshold, canny_th1, canny_th2, canny_aperture_size,
  47. do_merge);
  48. vector<Vec4f> lines;
  49. // Because of some CPU's power strategy, it seems that the first running of
  50. // an algorithm takes much longer. So here we run the algorithm 10 times
  51. // to see the algorithm's processing time with sufficiently warmed-up
  52. // CPU performance.
  53. for (int run_count = 0; run_count < 5; run_count++) {
  54. double freq = getTickFrequency();
  55. lines.clear();
  56. int64 start = getTickCount();
  57. // Detect the lines with FLD
  58. fld->detect(image, lines);
  59. double duration_ms = double(getTickCount() - start) * 1000 / freq;
  60. cout << "Elapsed time for FLD " << duration_ms << " ms." << endl;
  61. }
  62. // Show found lines with FLD
  63. Mat line_image_fld(image);
  64. fld->drawSegments(line_image_fld, lines);
  65. imshow("FLD result", line_image_fld);
  66. waitKey(1);
  67. Ptr<EdgeDrawing> ed = createEdgeDrawing();
  68. ed->params.EdgeDetectionOperator = EdgeDrawing::SOBEL;
  69. ed->params.GradientThresholdValue = 38;
  70. ed->params.AnchorThresholdValue = 8;
  71. vector<Vec6d> ellipses;
  72. for (int run_count = 0; run_count < 5; run_count++) {
  73. double freq = getTickFrequency();
  74. lines.clear();
  75. int64 start = getTickCount();
  76. // Detect edges
  77. //you should call this before detectLines() and detectEllipses()
  78. ed->detectEdges(image);
  79. // Detect lines
  80. ed->detectLines(lines);
  81. double duration_ms = double(getTickCount() - start) * 1000 / freq;
  82. cout << "Elapsed time for EdgeDrawing detectLines " << duration_ms << " ms." << endl;
  83. start = getTickCount();
  84. // Detect circles and ellipses
  85. ed->detectEllipses(ellipses);
  86. duration_ms = double(getTickCount() - start) * 1000 / freq;
  87. cout << "Elapsed time for EdgeDrawing detectEllipses " << duration_ms << " ms." << endl;
  88. }
  89. Mat edge_image_ed = Mat::zeros(image.size(), CV_8UC3);
  90. vector<vector<Point> > segments = ed->getSegments();
  91. for (size_t i = 0; i < segments.size(); i++)
  92. {
  93. const Point* pts = &segments[i][0];
  94. int n = (int)segments[i].size();
  95. polylines(edge_image_ed, &pts, &n, 1, false, Scalar((rand() & 255), (rand() & 255), (rand() & 255)), 1);
  96. }
  97. imshow("EdgeDrawing detected edges", edge_image_ed);
  98. Mat line_image_ed(image);
  99. fld->drawSegments(line_image_ed, lines);
  100. // Draw circles and ellipses
  101. for (size_t i = 0; i < ellipses.size(); i++)
  102. {
  103. Point center((int)ellipses[i][0], (int)ellipses[i][1]);
  104. Size axes((int)ellipses[i][2] + (int)ellipses[i][3], (int)ellipses[i][2] + (int)ellipses[i][4]);
  105. double angle(ellipses[i][5]);
  106. Scalar color = ellipses[i][2] == 0 ? Scalar(255, 255, 0) : Scalar(0, 255, 0);
  107. ellipse(line_image_ed, center, axes, angle, 0, 360, color, 2, LINE_AA);
  108. }
  109. imshow("EdgeDrawing result", line_image_ed);
  110. waitKey();
  111. return 0;
  112. }