perf_houghlines.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. #include "perf_precomp.hpp"
  5. namespace opencv_test {
  6. typedef tuple<string, double, double, double> Image_RhoStep_ThetaStep_Threshold_t;
  7. typedef perf::TestBaseWithParam<Image_RhoStep_ThetaStep_Threshold_t> Image_RhoStep_ThetaStep_Threshold;
  8. PERF_TEST_P(Image_RhoStep_ThetaStep_Threshold, HoughLines,
  9. testing::Combine(
  10. testing::Values( "cv/shared/pic5.png", "stitching/a1.png" ),
  11. testing::Values( 1, 10 ),
  12. testing::Values( 0.01, 0.1 ),
  13. testing::Values( 0.5, 1.1 )
  14. )
  15. )
  16. {
  17. string filename = getDataPath(get<0>(GetParam()));
  18. double rhoStep = get<1>(GetParam());
  19. double thetaStep = get<2>(GetParam());
  20. double threshold_ratio = get<3>(GetParam());
  21. Mat image = imread(filename, IMREAD_GRAYSCALE);
  22. if (image.empty())
  23. FAIL() << "Unable to load source image" << filename;
  24. Canny(image, image, 32, 128);
  25. // add some synthetic lines:
  26. line(image, Point(0, 0), Point(image.cols, image.rows), Scalar::all(255), 3);
  27. line(image, Point(image.cols, 0), Point(image.cols/2, image.rows), Scalar::all(255), 3);
  28. vector<Vec2f> lines;
  29. declare.time(60);
  30. int hough_threshold = (int)(std::min(image.cols, image.rows) * threshold_ratio);
  31. TEST_CYCLE() HoughLines(image, lines, rhoStep, thetaStep, hough_threshold);
  32. printf("%dx%d: %d lines\n", image.cols, image.rows, (int)lines.size());
  33. if (threshold_ratio < 1.0)
  34. {
  35. EXPECT_GE(lines.size(), 2u);
  36. }
  37. EXPECT_LT(lines.size(), 3000u);
  38. #if 0
  39. cv::cvtColor(image,image,cv::COLOR_GRAY2BGR);
  40. for( size_t i = 0; i < lines.size(); i++ )
  41. {
  42. float rho = lines[i][0], theta = lines[i][1];
  43. Point pt1, pt2;
  44. double a = cos(theta), b = sin(theta);
  45. double x0 = a*rho, y0 = b*rho;
  46. pt1.x = cvRound(x0 + 1000*(-b));
  47. pt1.y = cvRound(y0 + 1000*(a));
  48. pt2.x = cvRound(x0 - 1000*(-b));
  49. pt2.y = cvRound(y0 - 1000*(a));
  50. line(image, pt1, pt2, Scalar(0,0,255), 1, cv::LINE_AA);
  51. }
  52. cv::imshow("result", image);
  53. cv::waitKey();
  54. #endif
  55. SANITY_CHECK_NOTHING();
  56. }
  57. PERF_TEST_P(Image_RhoStep_ThetaStep_Threshold, HoughLines3f,
  58. testing::Combine(
  59. testing::Values( "cv/shared/pic5.png", "stitching/a1.png" ),
  60. testing::Values( 1, 10 ),
  61. testing::Values( 0.01, 0.1 ),
  62. testing::Values( 0.5, 1.1 )
  63. )
  64. )
  65. {
  66. string filename = getDataPath(get<0>(GetParam()));
  67. double rhoStep = get<1>(GetParam());
  68. double thetaStep = get<2>(GetParam());
  69. double threshold_ratio = get<3>(GetParam());
  70. Mat image = imread(filename, IMREAD_GRAYSCALE);
  71. if (image.empty())
  72. FAIL() << "Unable to load source image" << filename;
  73. Canny(image, image, 32, 128);
  74. // add some synthetic lines:
  75. line(image, Point(0, 0), Point(image.cols, image.rows), Scalar::all(255), 3);
  76. line(image, Point(image.cols, 0), Point(image.cols/2, image.rows), Scalar::all(255), 3);
  77. vector<Vec3f> lines;
  78. declare.time(60);
  79. int hough_threshold = (int)(std::min(image.cols, image.rows) * threshold_ratio);
  80. TEST_CYCLE() HoughLines(image, lines, rhoStep, thetaStep, hough_threshold);
  81. printf("%dx%d: %d lines\n", image.cols, image.rows, (int)lines.size());
  82. if (threshold_ratio < 1.0)
  83. {
  84. EXPECT_GE(lines.size(), 2u);
  85. }
  86. EXPECT_LT(lines.size(), 3000u);
  87. SANITY_CHECK_NOTHING();
  88. }
  89. } // namespace