test_hough.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
  14. // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
  15. // Third party copyrights are property of their respective owners.
  16. //
  17. // Redistribution and use in source and binary forms, with or without modification,
  18. // are permitted provided that the following conditions are met:
  19. //
  20. // * Redistribution's of source code must retain the above copyright notice,
  21. // this list of conditions and the following disclaimer.
  22. //
  23. // * Redistribution's in binary form must reproduce the above copyright notice,
  24. // this list of conditions and the following disclaimer in the documentation
  25. // and/or other materials provided with the distribution.
  26. //
  27. // * The name of the copyright holders may not be used to endorse or promote products
  28. // derived from this software without specific prior written permission.
  29. //
  30. // This software is provided by the copyright holders and contributors "as is" and
  31. // any express or implied warranties, including, but not limited to, the implied
  32. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  33. // In no event shall the Intel Corporation or contributors be liable for any direct,
  34. // indirect, incidental, special, exemplary, or consequential damages
  35. // (including, but not limited to, procurement of substitute goods or services;
  36. // loss of use, data, or profits; or business interruption) however caused
  37. // and on any theory of liability, whether in contract, strict liability,
  38. // or tort (including negligence or otherwise) arising in any way out of
  39. // the use of this software, even if advised of the possibility of such damage.
  40. //
  41. //M*/
  42. #include "test_precomp.hpp"
  43. #ifdef HAVE_CUDA
  44. namespace opencv_test { namespace {
  45. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  46. // HoughLines
  47. PARAM_TEST_CASE(HoughLines, cv::cuda::DeviceInfo, cv::Size, UseRoi)
  48. {
  49. static void generateLines(cv::Mat& img)
  50. {
  51. img.setTo(cv::Scalar::all(0));
  52. cv::line(img, cv::Point(20, 0), cv::Point(20, img.rows), cv::Scalar::all(255));
  53. cv::line(img, cv::Point(0, 50), cv::Point(img.cols, 50), cv::Scalar::all(255));
  54. cv::line(img, cv::Point(0, 0), cv::Point(img.cols, img.rows), cv::Scalar::all(255));
  55. cv::line(img, cv::Point(img.cols, 0), cv::Point(0, img.rows), cv::Scalar::all(255));
  56. }
  57. static void drawLines(cv::Mat& dst, const std::vector<cv::Vec2f>& lines)
  58. {
  59. dst.setTo(cv::Scalar::all(0));
  60. for (size_t i = 0; i < lines.size(); ++i)
  61. {
  62. float rho = lines[i][0], theta = lines[i][1];
  63. cv::Point pt1, pt2;
  64. double a = std::cos(theta), b = std::sin(theta);
  65. double x0 = a*rho, y0 = b*rho;
  66. pt1.x = cvRound(x0 + 1000*(-b));
  67. pt1.y = cvRound(y0 + 1000*(a));
  68. pt2.x = cvRound(x0 - 1000*(-b));
  69. pt2.y = cvRound(y0 - 1000*(a));
  70. cv::line(dst, pt1, pt2, cv::Scalar::all(255));
  71. }
  72. }
  73. };
  74. CUDA_TEST_P(HoughLines, Accuracy)
  75. {
  76. const cv::cuda::DeviceInfo devInfo = GET_PARAM(0);
  77. cv::cuda::setDevice(devInfo.deviceID());
  78. const cv::Size size = GET_PARAM(1);
  79. const bool useRoi = GET_PARAM(2);
  80. const float rho = 1.0f;
  81. const float theta = (float) (1.5 * CV_PI / 180.0);
  82. const int threshold = 100;
  83. cv::Mat src(size, CV_8UC1);
  84. generateLines(src);
  85. cv::Ptr<cv::cuda::HoughLinesDetector> hough = cv::cuda::createHoughLinesDetector(rho, theta, threshold);
  86. cv::cuda::GpuMat d_lines;
  87. hough->detect(loadMat(src, useRoi), d_lines);
  88. std::vector<cv::Vec2f> lines;
  89. hough->downloadResults(d_lines, lines);
  90. cv::Mat dst(size, CV_8UC1);
  91. drawLines(dst, lines);
  92. ASSERT_MAT_NEAR(src, dst, 0.0);
  93. }
  94. INSTANTIATE_TEST_CASE_P(CUDA_ImgProc, HoughLines, testing::Combine(
  95. ALL_DEVICES,
  96. DIFFERENT_SIZES,
  97. WHOLE_SUBMAT));
  98. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  99. // HoughCircles
  100. PARAM_TEST_CASE(HoughCircles, cv::cuda::DeviceInfo, cv::Size, UseRoi)
  101. {
  102. static void drawCircles(cv::Mat& dst, const std::vector<cv::Vec3f>& circles, bool fill)
  103. {
  104. dst.setTo(cv::Scalar::all(0));
  105. for (size_t i = 0; i < circles.size(); ++i)
  106. cv::circle(dst, cv::Point2f(circles[i][0], circles[i][1]), (int)circles[i][2], cv::Scalar::all(255), fill ? -1 : 1);
  107. }
  108. };
  109. CUDA_TEST_P(HoughCircles, Accuracy)
  110. {
  111. const cv::cuda::DeviceInfo devInfo = GET_PARAM(0);
  112. cv::cuda::setDevice(devInfo.deviceID());
  113. const cv::Size size = GET_PARAM(1);
  114. const bool useRoi = GET_PARAM(2);
  115. const float dp = 2.0f;
  116. const float minDist = 0.0f;
  117. const int minRadius = 10;
  118. const int maxRadius = 20;
  119. const int cannyThreshold = 100;
  120. const int votesThreshold = 20;
  121. std::vector<cv::Vec3f> circles_gold(4);
  122. circles_gold[0] = cv::Vec3i(20, 20, minRadius);
  123. circles_gold[1] = cv::Vec3i(90, 87, minRadius + 3);
  124. circles_gold[2] = cv::Vec3i(30, 70, minRadius + 8);
  125. circles_gold[3] = cv::Vec3i(80, 10, maxRadius);
  126. cv::Mat src(size, CV_8UC1);
  127. drawCircles(src, circles_gold, true);
  128. cv::Ptr<cv::cuda::HoughCirclesDetector> houghCircles = cv::cuda::createHoughCirclesDetector(dp, minDist, cannyThreshold, votesThreshold, minRadius, maxRadius);
  129. cv::cuda::GpuMat d_circles;
  130. houghCircles->detect(loadMat(src, useRoi), d_circles);
  131. std::vector<cv::Vec3f> circles;
  132. d_circles.download(circles);
  133. ASSERT_FALSE(circles.empty());
  134. for (size_t i = 0; i < circles.size(); ++i)
  135. {
  136. cv::Vec3f cur = circles[i];
  137. bool found = false;
  138. for (size_t j = 0; j < circles_gold.size(); ++j)
  139. {
  140. cv::Vec3f gold = circles_gold[j];
  141. if (std::fabs(cur[0] - gold[0]) < 5 && std::fabs(cur[1] - gold[1]) < 5 && std::fabs(cur[2] - gold[2]) < 5)
  142. {
  143. found = true;
  144. break;
  145. }
  146. }
  147. ASSERT_TRUE(found);
  148. }
  149. }
  150. INSTANTIATE_TEST_CASE_P(CUDA_ImgProc, HoughCircles, testing::Combine(
  151. ALL_DEVICES,
  152. DIFFERENT_SIZES,
  153. WHOLE_SUBMAT));
  154. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  155. // GeneralizedHough
  156. PARAM_TEST_CASE(GeneralizedHough, cv::cuda::DeviceInfo, UseRoi)
  157. {
  158. };
  159. CUDA_TEST_P(GeneralizedHough, Ballard)
  160. {
  161. const cv::cuda::DeviceInfo devInfo = GET_PARAM(0);
  162. cv::cuda::setDevice(devInfo.deviceID());
  163. const bool useRoi = GET_PARAM(1);
  164. cv::Mat templ = readImage("../cv/shared/templ.png", cv::IMREAD_GRAYSCALE);
  165. ASSERT_FALSE(templ.empty());
  166. cv::Point templCenter(templ.cols / 2, templ.rows / 2);
  167. const size_t gold_count = 3;
  168. cv::Point pos_gold[gold_count];
  169. pos_gold[0] = cv::Point(templCenter.x + 10, templCenter.y + 10);
  170. pos_gold[1] = cv::Point(2 * templCenter.x + 40, templCenter.y + 10);
  171. pos_gold[2] = cv::Point(2 * templCenter.x + 40, 2 * templCenter.y + 40);
  172. cv::Mat image(templ.rows * 3, templ.cols * 3, CV_8UC1, cv::Scalar::all(0));
  173. for (size_t i = 0; i < gold_count; ++i)
  174. {
  175. cv::Rect rec(pos_gold[i].x - templCenter.x, pos_gold[i].y - templCenter.y, templ.cols, templ.rows);
  176. cv::Mat imageROI = image(rec);
  177. templ.copyTo(imageROI);
  178. }
  179. cv::Ptr<cv::GeneralizedHoughBallard> alg = cv::cuda::createGeneralizedHoughBallard();
  180. alg->setVotesThreshold(200);
  181. alg->setTemplate(loadMat(templ, useRoi));
  182. cv::cuda::GpuMat d_pos;
  183. alg->detect(loadMat(image, useRoi), d_pos);
  184. std::vector<cv::Vec4f> pos;
  185. d_pos.download(pos);
  186. ASSERT_EQ(gold_count, pos.size());
  187. for (size_t i = 0; i < gold_count; ++i)
  188. {
  189. cv::Point gold = pos_gold[i];
  190. bool found = false;
  191. for (size_t j = 0; j < pos.size(); ++j)
  192. {
  193. cv::Point2f p(pos[j][0], pos[j][1]);
  194. if (::fabs(p.x - gold.x) < 2 && ::fabs(p.y - gold.y) < 2)
  195. {
  196. found = true;
  197. break;
  198. }
  199. }
  200. ASSERT_TRUE(found);
  201. }
  202. }
  203. INSTANTIATE_TEST_CASE_P(CUDA_ImgProc, GeneralizedHough, testing::Combine(
  204. ALL_DEVICES,
  205. WHOLE_SUBMAT));
  206. }} // namespace
  207. #endif // HAVE_CUDA