perf_features2d.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. // FAST
  46. DEF_PARAM_TEST(Image_Threshold_NonMaxSuppression, string, int, bool);
  47. PERF_TEST_P(Image_Threshold_NonMaxSuppression, FAST,
  48. Combine(Values<string>("gpu/perf/aloe.png"),
  49. Values(20),
  50. Bool()))
  51. {
  52. const cv::Mat img = readImage(GET_PARAM(0), cv::IMREAD_GRAYSCALE);
  53. ASSERT_FALSE(img.empty());
  54. const int threshold = GET_PARAM(1);
  55. const bool nonMaxSuppersion = GET_PARAM(2);
  56. if (PERF_RUN_CUDA())
  57. {
  58. cv::Ptr<cv::cuda::FastFeatureDetector> d_fast =
  59. cv::cuda::FastFeatureDetector::create(threshold, nonMaxSuppersion,
  60. cv::FastFeatureDetector::TYPE_9_16,
  61. 0.5 * img.size().area());
  62. const cv::cuda::GpuMat d_img(img);
  63. cv::cuda::GpuMat d_keypoints;
  64. TEST_CYCLE() d_fast->detectAsync(d_img, d_keypoints);
  65. std::vector<cv::KeyPoint> gpu_keypoints;
  66. d_fast->convert(d_keypoints, gpu_keypoints);
  67. sortKeyPoints(gpu_keypoints);
  68. SANITY_CHECK_KEYPOINTS(gpu_keypoints);
  69. }
  70. else
  71. {
  72. std::vector<cv::KeyPoint> cpu_keypoints;
  73. TEST_CYCLE() cv::FAST(img, cpu_keypoints, threshold, nonMaxSuppersion);
  74. SANITY_CHECK_KEYPOINTS(cpu_keypoints);
  75. }
  76. }
  77. //////////////////////////////////////////////////////////////////////
  78. // ORB
  79. DEF_PARAM_TEST(Image_NFeatures, string, int);
  80. PERF_TEST_P(Image_NFeatures, ORB,
  81. Combine(Values<string>("gpu/perf/aloe.png"),
  82. Values(4000)))
  83. {
  84. declare.time(300.0);
  85. const cv::Mat img = readImage(GET_PARAM(0), cv::IMREAD_GRAYSCALE);
  86. ASSERT_FALSE(img.empty());
  87. const int nFeatures = GET_PARAM(1);
  88. if (PERF_RUN_CUDA())
  89. {
  90. cv::Ptr<cv::cuda::ORB> d_orb = cv::cuda::ORB::create(nFeatures);
  91. const cv::cuda::GpuMat d_img(img);
  92. cv::cuda::GpuMat d_keypoints, d_descriptors;
  93. TEST_CYCLE() d_orb->detectAndComputeAsync(d_img, cv::noArray(), d_keypoints, d_descriptors);
  94. std::vector<cv::KeyPoint> gpu_keypoints;
  95. d_orb->convert(d_keypoints, gpu_keypoints);
  96. cv::Mat gpu_descriptors(d_descriptors);
  97. gpu_keypoints.resize(10);
  98. gpu_descriptors = gpu_descriptors.rowRange(0, 10);
  99. sortKeyPoints(gpu_keypoints, gpu_descriptors);
  100. SANITY_CHECK_KEYPOINTS(gpu_keypoints, 1e-4);
  101. SANITY_CHECK(gpu_descriptors);
  102. }
  103. else
  104. {
  105. cv::Ptr<cv::ORB> orb = cv::ORB::create(nFeatures);
  106. std::vector<cv::KeyPoint> cpu_keypoints;
  107. cv::Mat cpu_descriptors;
  108. TEST_CYCLE() orb->detectAndCompute(img, cv::noArray(), cpu_keypoints, cpu_descriptors);
  109. SANITY_CHECK_KEYPOINTS(cpu_keypoints);
  110. SANITY_CHECK(cpu_descriptors);
  111. }
  112. }
  113. //////////////////////////////////////////////////////////////////////
  114. // BFMatch
  115. DEF_PARAM_TEST(DescSize_Norm, int, NormType);
  116. PERF_TEST_P(DescSize_Norm, BFMatch,
  117. Combine(Values(64, 128, 256),
  118. Values(NormType(cv::NORM_L1), NormType(cv::NORM_L2), NormType(cv::NORM_HAMMING))))
  119. {
  120. declare.time(20.0);
  121. const int desc_size = GET_PARAM(0);
  122. const int normType = GET_PARAM(1);
  123. const int type = normType == cv::NORM_HAMMING ? CV_8U : CV_32F;
  124. cv::Mat query(3000, desc_size, type);
  125. declare.in(query, WARMUP_RNG);
  126. cv::Mat train(3000, desc_size, type);
  127. declare.in(train, WARMUP_RNG);
  128. if (PERF_RUN_CUDA())
  129. {
  130. cv::Ptr<cv::cuda::DescriptorMatcher> d_matcher = cv::cuda::DescriptorMatcher::createBFMatcher(normType);
  131. const cv::cuda::GpuMat d_query(query);
  132. const cv::cuda::GpuMat d_train(train);
  133. cv::cuda::GpuMat d_matches;
  134. TEST_CYCLE() d_matcher->matchAsync(d_query, d_train, d_matches);
  135. std::vector<cv::DMatch> gpu_matches;
  136. d_matcher->matchConvert(d_matches, gpu_matches);
  137. SANITY_CHECK_MATCHES(gpu_matches);
  138. }
  139. else
  140. {
  141. cv::BFMatcher matcher(normType);
  142. std::vector<cv::DMatch> cpu_matches;
  143. TEST_CYCLE() matcher.match(query, train, cpu_matches);
  144. SANITY_CHECK_MATCHES(cpu_matches);
  145. }
  146. }
  147. //////////////////////////////////////////////////////////////////////
  148. // BFKnnMatch
  149. static void toOneRowMatches(const std::vector< std::vector<cv::DMatch> >& src, std::vector<cv::DMatch>& dst)
  150. {
  151. dst.clear();
  152. for (size_t i = 0; i < src.size(); ++i)
  153. for (size_t j = 0; j < src[i].size(); ++j)
  154. dst.push_back(src[i][j]);
  155. }
  156. DEF_PARAM_TEST(DescSize_K_Norm, int, int, NormType);
  157. PERF_TEST_P(DescSize_K_Norm, BFKnnMatch,
  158. Combine(Values(64, 128, 256),
  159. Values(2, 3),
  160. Values(NormType(cv::NORM_L1), NormType(cv::NORM_L2))))
  161. {
  162. declare.time(30.0);
  163. const int desc_size = GET_PARAM(0);
  164. const int k = GET_PARAM(1);
  165. const int normType = GET_PARAM(2);
  166. const int type = normType == cv::NORM_HAMMING ? CV_8U : CV_32F;
  167. cv::Mat query(3000, desc_size, type);
  168. declare.in(query, WARMUP_RNG);
  169. cv::Mat train(3000, desc_size, type);
  170. declare.in(train, WARMUP_RNG);
  171. if (PERF_RUN_CUDA())
  172. {
  173. cv::Ptr<cv::cuda::DescriptorMatcher> d_matcher = cv::cuda::DescriptorMatcher::createBFMatcher(normType);
  174. const cv::cuda::GpuMat d_query(query);
  175. const cv::cuda::GpuMat d_train(train);
  176. cv::cuda::GpuMat d_matches;
  177. TEST_CYCLE() d_matcher->knnMatchAsync(d_query, d_train, d_matches, k);
  178. std::vector< std::vector<cv::DMatch> > matchesTbl;
  179. d_matcher->knnMatchConvert(d_matches, matchesTbl);
  180. std::vector<cv::DMatch> gpu_matches;
  181. toOneRowMatches(matchesTbl, gpu_matches);
  182. SANITY_CHECK_MATCHES(gpu_matches);
  183. }
  184. else
  185. {
  186. cv::BFMatcher matcher(normType);
  187. std::vector< std::vector<cv::DMatch> > matchesTbl;
  188. TEST_CYCLE() matcher.knnMatch(query, train, matchesTbl, k);
  189. std::vector<cv::DMatch> cpu_matches;
  190. toOneRowMatches(matchesTbl, cpu_matches);
  191. SANITY_CHECK_MATCHES(cpu_matches);
  192. }
  193. }
  194. //////////////////////////////////////////////////////////////////////
  195. // BFRadiusMatch
  196. PERF_TEST_P(DescSize_Norm, BFRadiusMatch,
  197. Combine(Values(64, 128, 256),
  198. Values(NormType(cv::NORM_L1), NormType(cv::NORM_L2))))
  199. {
  200. declare.time(30.0);
  201. const int desc_size = GET_PARAM(0);
  202. const int normType = GET_PARAM(1);
  203. const int type = normType == cv::NORM_HAMMING ? CV_8U : CV_32F;
  204. const float maxDistance = 10000;
  205. cv::Mat query(3000, desc_size, type);
  206. declare.in(query, WARMUP_RNG);
  207. cv::Mat train(3000, desc_size, type);
  208. declare.in(train, WARMUP_RNG);
  209. if (PERF_RUN_CUDA())
  210. {
  211. cv::Ptr<cv::cuda::DescriptorMatcher> d_matcher = cv::cuda::DescriptorMatcher::createBFMatcher(normType);
  212. const cv::cuda::GpuMat d_query(query);
  213. const cv::cuda::GpuMat d_train(train);
  214. cv::cuda::GpuMat d_matches;
  215. TEST_CYCLE() d_matcher->radiusMatchAsync(d_query, d_train, d_matches, maxDistance);
  216. std::vector< std::vector<cv::DMatch> > matchesTbl;
  217. d_matcher->radiusMatchConvert(d_matches, matchesTbl);
  218. std::vector<cv::DMatch> gpu_matches;
  219. toOneRowMatches(matchesTbl, gpu_matches);
  220. SANITY_CHECK_MATCHES(gpu_matches);
  221. }
  222. else
  223. {
  224. cv::BFMatcher matcher(normType);
  225. std::vector< std::vector<cv::DMatch> > matchesTbl;
  226. TEST_CYCLE() matcher.radiusMatch(query, train, matchesTbl, maxDistance);
  227. std::vector<cv::DMatch> cpu_matches;
  228. toOneRowMatches(matchesTbl, cpu_matches);
  229. SANITY_CHECK_MATCHES(cpu_matches);
  230. }
  231. }
  232. }} // namespace