perf_hough.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 "perf_precomp.hpp"
  43. namespace opencv_test { namespace {
  44. //////////////////////////////////////////////////////////////////////
  45. // HoughLines
  46. namespace
  47. {
  48. struct Vec4iComparator
  49. {
  50. bool operator()(const cv::Vec4i& a, const cv::Vec4i b) const
  51. {
  52. if (a[0] != b[0]) return a[0] < b[0];
  53. else if(a[1] != b[1]) return a[1] < b[1];
  54. else if(a[2] != b[2]) return a[2] < b[2];
  55. else return a[3] < b[3];
  56. }
  57. };
  58. struct Vec3fComparator
  59. {
  60. bool operator()(const cv::Vec3f& a, const cv::Vec3f b) const
  61. {
  62. if(a[0] != b[0]) return a[0] < b[0];
  63. else if(a[1] != b[1]) return a[1] < b[1];
  64. else return a[2] < b[2];
  65. }
  66. };
  67. struct Vec2fComparator
  68. {
  69. bool operator()(const cv::Vec2f& a, const cv::Vec2f b) const
  70. {
  71. if(a[0] != b[0]) return a[0] < b[0];
  72. else return a[1] < b[1];
  73. }
  74. };
  75. }
  76. PERF_TEST_P(Sz, HoughLines,
  77. CUDA_TYPICAL_MAT_SIZES)
  78. {
  79. declare.time(30.0);
  80. const cv::Size size = GetParam();
  81. const float rho = 1.0f;
  82. const float theta = static_cast<float>(CV_PI / 180.0);
  83. const int threshold = 300;
  84. cv::Mat src(size, CV_8UC1, cv::Scalar::all(0));
  85. cv::line(src, cv::Point(0, 100), cv::Point(src.cols, 100), cv::Scalar::all(255), 1);
  86. cv::line(src, cv::Point(0, 200), cv::Point(src.cols, 200), cv::Scalar::all(255), 1);
  87. cv::line(src, cv::Point(0, 400), cv::Point(src.cols, 400), cv::Scalar::all(255), 1);
  88. cv::line(src, cv::Point(100, 0), cv::Point(100, src.rows), cv::Scalar::all(255), 1);
  89. cv::line(src, cv::Point(200, 0), cv::Point(200, src.rows), cv::Scalar::all(255), 1);
  90. cv::line(src, cv::Point(400, 0), cv::Point(400, src.rows), cv::Scalar::all(255), 1);
  91. if (PERF_RUN_CUDA())
  92. {
  93. const cv::cuda::GpuMat d_src(src);
  94. cv::cuda::GpuMat d_lines;
  95. cv::Ptr<cv::cuda::HoughLinesDetector> hough = cv::cuda::createHoughLinesDetector(rho, theta, threshold);
  96. TEST_CYCLE() hough->detect(d_src, d_lines);
  97. cv::Mat gpu_lines(d_lines.row(0));
  98. cv::Vec2f* begin = gpu_lines.ptr<cv::Vec2f>(0);
  99. cv::Vec2f* end = begin + gpu_lines.cols;
  100. std::sort(begin, end, Vec2fComparator());
  101. SANITY_CHECK(gpu_lines);
  102. }
  103. else
  104. {
  105. std::vector<cv::Vec2f> cpu_lines;
  106. TEST_CYCLE() cv::HoughLines(src, cpu_lines, rho, theta, threshold);
  107. SANITY_CHECK(cpu_lines);
  108. }
  109. }
  110. //////////////////////////////////////////////////////////////////////
  111. // HoughLinesP
  112. DEF_PARAM_TEST_1(Image, std::string);
  113. PERF_TEST_P(Image, HoughLinesP,
  114. testing::Values("cv/shared/pic5.png", "stitching/a1.png"))
  115. {
  116. declare.time(30.0);
  117. const std::string fileName = getDataPath(GetParam());
  118. const float rho = 1.0f;
  119. const float theta = static_cast<float>(CV_PI / 180.0);
  120. const int threshold = 100;
  121. const int minLineLength = 50;
  122. const int maxLineGap = 5;
  123. const cv::Mat image = cv::imread(fileName, cv::IMREAD_GRAYSCALE);
  124. ASSERT_FALSE(image.empty());
  125. cv::Mat mask;
  126. cv::Canny(image, mask, 50, 100);
  127. if (PERF_RUN_CUDA())
  128. {
  129. const cv::cuda::GpuMat d_mask(mask);
  130. cv::cuda::GpuMat d_lines;
  131. cv::Ptr<cv::cuda::HoughSegmentDetector> hough = cv::cuda::createHoughSegmentDetector(rho, theta, minLineLength, maxLineGap);
  132. TEST_CYCLE() hough->detect(d_mask, d_lines);
  133. cv::Mat gpu_lines(d_lines);
  134. cv::Vec4i* begin = gpu_lines.ptr<cv::Vec4i>();
  135. cv::Vec4i* end = begin + gpu_lines.cols;
  136. std::sort(begin, end, Vec4iComparator());
  137. SANITY_CHECK(gpu_lines);
  138. }
  139. else
  140. {
  141. std::vector<cv::Vec4i> cpu_lines;
  142. TEST_CYCLE() cv::HoughLinesP(mask, cpu_lines, rho, theta, threshold, minLineLength, maxLineGap);
  143. SANITY_CHECK(cpu_lines);
  144. }
  145. }
  146. //////////////////////////////////////////////////////////////////////
  147. // HoughCircles
  148. DEF_PARAM_TEST(Sz_Dp_MinDist, cv::Size, float, float);
  149. PERF_TEST_P(Sz_Dp_MinDist, HoughCircles,
  150. Combine(CUDA_TYPICAL_MAT_SIZES,
  151. Values(1.0f, 2.0f, 4.0f),
  152. Values(1.0f)))
  153. {
  154. declare.time(30.0);
  155. const cv::Size size = GET_PARAM(0);
  156. const float dp = GET_PARAM(1);
  157. const float minDist = GET_PARAM(2);
  158. const int minRadius = 10;
  159. const int maxRadius = 30;
  160. const int cannyThreshold = 100;
  161. const int votesThreshold = 15;
  162. cv::Mat src(size, CV_8UC1, cv::Scalar::all(0));
  163. cv::circle(src, cv::Point(100, 100), 20, cv::Scalar::all(255), -1);
  164. cv::circle(src, cv::Point(200, 200), 25, cv::Scalar::all(255), -1);
  165. cv::circle(src, cv::Point(200, 100), 25, cv::Scalar::all(255), -1);
  166. if (PERF_RUN_CUDA())
  167. {
  168. const cv::cuda::GpuMat d_src(src);
  169. cv::cuda::GpuMat d_circles;
  170. cv::Ptr<cv::cuda::HoughCirclesDetector> houghCircles = cv::cuda::createHoughCirclesDetector(dp, minDist, cannyThreshold, votesThreshold, minRadius, maxRadius);
  171. TEST_CYCLE() houghCircles->detect(d_src, d_circles);
  172. cv::Mat gpu_circles(d_circles);
  173. cv::Vec3f* begin = gpu_circles.ptr<cv::Vec3f>(0);
  174. cv::Vec3f* end = begin + gpu_circles.cols;
  175. std::sort(begin, end, Vec3fComparator());
  176. SANITY_CHECK(gpu_circles);
  177. }
  178. else
  179. {
  180. std::vector<cv::Vec3f> cpu_circles;
  181. TEST_CYCLE() cv::HoughCircles(src, cpu_circles, cv::HOUGH_GRADIENT, dp, minDist, cannyThreshold, votesThreshold, minRadius, maxRadius);
  182. SANITY_CHECK(cpu_circles);
  183. }
  184. }
  185. //////////////////////////////////////////////////////////////////////
  186. // GeneralizedHough
  187. PERF_TEST_P(Sz, GeneralizedHoughBallard, CUDA_TYPICAL_MAT_SIZES)
  188. {
  189. declare.time(10);
  190. const cv::Size imageSize = GetParam();
  191. const cv::Mat templ = readImage("cv/shared/templ.png", cv::IMREAD_GRAYSCALE);
  192. ASSERT_FALSE(templ.empty());
  193. cv::Mat image(imageSize, CV_8UC1, cv::Scalar::all(0));
  194. templ.copyTo(image(cv::Rect(50, 50, templ.cols, templ.rows)));
  195. cv::Mat edges;
  196. cv::Canny(image, edges, 50, 100);
  197. cv::Mat dx, dy;
  198. cv::Sobel(image, dx, CV_32F, 1, 0);
  199. cv::Sobel(image, dy, CV_32F, 0, 1);
  200. if (PERF_RUN_CUDA())
  201. {
  202. cv::Ptr<cv::GeneralizedHoughBallard> alg = cv::cuda::createGeneralizedHoughBallard();
  203. const cv::cuda::GpuMat d_edges(edges);
  204. const cv::cuda::GpuMat d_dx(dx);
  205. const cv::cuda::GpuMat d_dy(dy);
  206. cv::cuda::GpuMat positions;
  207. alg->setTemplate(cv::cuda::GpuMat(templ));
  208. TEST_CYCLE() alg->detect(d_edges, d_dx, d_dy, positions);
  209. CUDA_SANITY_CHECK(positions);
  210. }
  211. else
  212. {
  213. cv::Ptr<cv::GeneralizedHoughBallard> alg = cv::createGeneralizedHoughBallard();
  214. cv::Mat positions;
  215. alg->setTemplate(templ);
  216. TEST_CYCLE() alg->detect(edges, dx, dy, positions);
  217. CPU_SANITY_CHECK(positions);
  218. }
  219. }
  220. PERF_TEST_P(Sz, DISABLED_GeneralizedHoughGuil, CUDA_TYPICAL_MAT_SIZES)
  221. {
  222. declare.time(10);
  223. const cv::Size imageSize = GetParam();
  224. const cv::Mat templ = readImage("cv/shared/templ.png", cv::IMREAD_GRAYSCALE);
  225. ASSERT_FALSE(templ.empty());
  226. cv::Mat image(imageSize, CV_8UC1, cv::Scalar::all(0));
  227. templ.copyTo(image(cv::Rect(50, 50, templ.cols, templ.rows)));
  228. cv::RNG rng(123456789);
  229. const int objCount = rng.uniform(5, 15);
  230. for (int i = 0; i < objCount; ++i)
  231. {
  232. double scale = rng.uniform(0.7, 1.3);
  233. bool rotate = 1 == rng.uniform(0, 2);
  234. cv::Mat obj;
  235. cv::resize(templ, obj, cv::Size(), scale, scale);
  236. if (rotate)
  237. obj = obj.t();
  238. cv::Point pos;
  239. pos.x = rng.uniform(0, image.cols - obj.cols);
  240. pos.y = rng.uniform(0, image.rows - obj.rows);
  241. cv::Mat roi = image(cv::Rect(pos, obj.size()));
  242. cv::add(roi, obj, roi);
  243. }
  244. cv::Mat edges;
  245. cv::Canny(image, edges, 50, 100);
  246. cv::Mat dx, dy;
  247. cv::Sobel(image, dx, CV_32F, 1, 0);
  248. cv::Sobel(image, dy, CV_32F, 0, 1);
  249. if (PERF_RUN_CUDA())
  250. {
  251. cv::Ptr<cv::GeneralizedHoughGuil> alg = cv::cuda::createGeneralizedHoughGuil();
  252. alg->setMaxAngle(90.0);
  253. alg->setAngleStep(2.0);
  254. const cv::cuda::GpuMat d_edges(edges);
  255. const cv::cuda::GpuMat d_dx(dx);
  256. const cv::cuda::GpuMat d_dy(dy);
  257. cv::cuda::GpuMat positions;
  258. alg->setTemplate(cv::cuda::GpuMat(templ));
  259. TEST_CYCLE() alg->detect(d_edges, d_dx, d_dy, positions);
  260. }
  261. else
  262. {
  263. cv::Ptr<cv::GeneralizedHoughGuil> alg = cv::createGeneralizedHoughGuil();
  264. alg->setMaxAngle(90.0);
  265. alg->setAngleStep(2.0);
  266. cv::Mat positions;
  267. alg->setTemplate(templ);
  268. TEST_CYCLE() alg->detect(edges, dx, dy, positions);
  269. }
  270. // The algorithm is not stable yet.
  271. SANITY_CHECK_NOTHING();
  272. }
  273. }} // namespace