test_houghlines.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. // Copyright (C) 2014, Itseez, Inc., all rights reserved.
  5. // Third party copyrights are property of their respective owners.
  6. #include "../test_precomp.hpp"
  7. #include "opencv2/ts/ocl_test.hpp"
  8. #ifdef HAVE_OPENCL
  9. namespace opencv_test {
  10. namespace ocl {
  11. struct Vec2fComparator
  12. {
  13. bool operator()(const Vec2f& a, const Vec2f b) const
  14. {
  15. if(a[0] != b[0]) return a[0] < b[0];
  16. else return a[1] < b[1];
  17. }
  18. };
  19. /////////////////////////////// HoughLines ////////////////////////////////////
  20. PARAM_TEST_CASE(HoughLines, double, double, int)
  21. {
  22. double rhoStep, thetaStep;
  23. int threshold;
  24. Size src_size;
  25. Mat src, dst;
  26. UMat usrc, udst;
  27. virtual void SetUp()
  28. {
  29. rhoStep = GET_PARAM(0);
  30. thetaStep = GET_PARAM(1);
  31. threshold = GET_PARAM(2);
  32. }
  33. void generateTestData()
  34. {
  35. src_size = randomSize(500, 1920);
  36. src.create(src_size, CV_8UC1);
  37. src.setTo(Scalar::all(0));
  38. line(src, Point(0, 100), Point(100, 100), Scalar::all(255), 1);
  39. line(src, Point(0, 200), Point(100, 200), Scalar::all(255), 1);
  40. line(src, Point(0, 400), Point(100, 400), Scalar::all(255), 1);
  41. line(src, Point(100, 0), Point(100, 200), Scalar::all(255), 1);
  42. line(src, Point(200, 0), Point(200, 200), Scalar::all(255), 1);
  43. line(src, Point(400, 0), Point(400, 200), Scalar::all(255), 1);
  44. src.copyTo(usrc);
  45. }
  46. void readRealTestData()
  47. {
  48. Mat img = readImage("shared/pic5.png", IMREAD_GRAYSCALE);
  49. Canny(img, src, 100, 150, 3);
  50. src.copyTo(usrc);
  51. }
  52. void Near(double eps = 0.)
  53. {
  54. EXPECT_EQ(dst.size(), udst.size());
  55. if (dst.total() > 0)
  56. {
  57. Mat lines_cpu, lines_gpu;
  58. dst.copyTo(lines_cpu);
  59. udst.copyTo(lines_gpu);
  60. std::sort(lines_cpu.begin<Vec2f>(), lines_cpu.end<Vec2f>(), Vec2fComparator());
  61. std::sort(lines_gpu.begin<Vec2f>(), lines_gpu.end<Vec2f>(), Vec2fComparator());
  62. EXPECT_LE(TestUtils::checkNorm2(lines_cpu, lines_gpu), eps);
  63. }
  64. }
  65. };
  66. OCL_TEST_P(HoughLines, RealImage)
  67. {
  68. readRealTestData();
  69. OCL_OFF(cv::HoughLines(src, dst, rhoStep, thetaStep, threshold));
  70. OCL_ON(cv::HoughLines(usrc, udst, rhoStep, thetaStep, threshold));
  71. Near(1e-5);
  72. }
  73. OCL_TEST_P(HoughLines, GeneratedImage)
  74. {
  75. for (int j = 0; j < test_loop_times; j++)
  76. {
  77. generateTestData();
  78. OCL_OFF(cv::HoughLines(src, dst, rhoStep, thetaStep, threshold));
  79. OCL_ON(cv::HoughLines(usrc, udst, rhoStep, thetaStep, threshold));
  80. Near(1e-5);
  81. }
  82. }
  83. /////////////////////////////// HoughLinesP ///////////////////////////////////
  84. PARAM_TEST_CASE(HoughLinesP, int, double, double)
  85. {
  86. double rhoStep, thetaStep, minLineLength, maxGap;
  87. int threshold;
  88. Size src_size;
  89. Mat src, dst;
  90. UMat usrc, udst;
  91. virtual void SetUp()
  92. {
  93. rhoStep = 1.0;
  94. thetaStep = CV_PI / 180;
  95. threshold = GET_PARAM(0);
  96. minLineLength = GET_PARAM(1);
  97. maxGap = GET_PARAM(2);
  98. }
  99. void readRealTestData()
  100. {
  101. Mat img = readImage("shared/pic5.png", IMREAD_GRAYSCALE);
  102. Canny(img, src, 50, 200, 3);
  103. src.copyTo(usrc);
  104. }
  105. void Near(double eps = 0.)
  106. {
  107. Mat lines_gpu = udst.getMat(ACCESS_READ);
  108. if (dst.total() > 0 && lines_gpu.total() > 0)
  109. {
  110. Mat result_cpu(src.size(), CV_8UC1, Scalar::all(0));
  111. Mat result_gpu(src.size(), CV_8UC1, Scalar::all(0));
  112. MatConstIterator_<Vec4i> it = dst.begin<Vec4i>(), end = dst.end<Vec4i>();
  113. for ( ; it != end; it++)
  114. {
  115. Vec4i p = *it;
  116. line(result_cpu, Point(p[0], p[1]), Point(p[2], p[3]), Scalar(255));
  117. }
  118. it = lines_gpu.begin<Vec4i>(), end = lines_gpu.end<Vec4i>();
  119. for ( ; it != end; it++)
  120. {
  121. Vec4i p = *it;
  122. line(result_gpu, Point(p[0], p[1]), Point(p[2], p[3]), Scalar(255));
  123. }
  124. EXPECT_MAT_SIMILAR(result_cpu, result_gpu, eps);
  125. }
  126. }
  127. };
  128. OCL_TEST_P(HoughLinesP, RealImage)
  129. {
  130. readRealTestData();
  131. OCL_OFF(cv::HoughLinesP(src, dst, rhoStep, thetaStep, threshold, minLineLength, maxGap));
  132. OCL_ON(cv::HoughLinesP(usrc, udst, rhoStep, thetaStep, threshold, minLineLength, maxGap));
  133. Near(0.25);
  134. }
  135. OCL_INSTANTIATE_TEST_CASE_P(Imgproc, HoughLines, Combine(Values(1, 0.5), // rhoStep
  136. Values(CV_PI / 180.0, CV_PI / 360.0), // thetaStep
  137. Values(85, 150))); // threshold
  138. OCL_INSTANTIATE_TEST_CASE_P(Imgproc, HoughLinesP, Combine(Values(100, 150), // threshold
  139. Values(50, 100), // minLineLength
  140. Values(5, 10))); // maxLineGap
  141. } } // namespace opencv_test::ocl
  142. #endif // HAVE_OPENCL