test_surf.cuda.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. #if defined(HAVE_CUDA) && defined(OPENCV_ENABLE_NONFREE)
  44. namespace opencv_test { namespace {
  45. /////////////////////////////////////////////////////////////////////////////////////////////////
  46. // SURF
  47. #ifdef HAVE_OPENCV_CUDAARITHM
  48. namespace
  49. {
  50. IMPLEMENT_PARAM_CLASS(SURF_HessianThreshold, double)
  51. IMPLEMENT_PARAM_CLASS(SURF_Octaves, int)
  52. IMPLEMENT_PARAM_CLASS(SURF_OctaveLayers, int)
  53. IMPLEMENT_PARAM_CLASS(SURF_Extended, bool)
  54. IMPLEMENT_PARAM_CLASS(SURF_Upright, bool)
  55. }
  56. PARAM_TEST_CASE(CUDA_SURF, SURF_HessianThreshold, SURF_Octaves, SURF_OctaveLayers, SURF_Extended, SURF_Upright)
  57. {
  58. double hessianThreshold;
  59. int nOctaves;
  60. int nOctaveLayers;
  61. bool extended;
  62. bool upright;
  63. virtual void SetUp()
  64. {
  65. hessianThreshold = GET_PARAM(0);
  66. nOctaves = GET_PARAM(1);
  67. nOctaveLayers = GET_PARAM(2);
  68. extended = GET_PARAM(3);
  69. upright = GET_PARAM(4);
  70. }
  71. };
  72. CUDA_TEST_P(CUDA_SURF, Detector)
  73. {
  74. cv::Mat image = readImage("../gpu/features2d/aloe.png", cv::IMREAD_GRAYSCALE);
  75. ASSERT_FALSE(image.empty());
  76. cv::cuda::SURF_CUDA surf;
  77. surf.hessianThreshold = hessianThreshold;
  78. surf.nOctaves = nOctaves;
  79. surf.nOctaveLayers = nOctaveLayers;
  80. surf.extended = extended;
  81. surf.upright = upright;
  82. surf.keypointsRatio = 0.05f;
  83. std::vector<cv::KeyPoint> keypoints;
  84. surf(loadMat(image), cv::cuda::GpuMat(), keypoints);
  85. cv::Ptr<cv::Feature2D> surf_gold = cv::xfeatures2d::SURF::create(hessianThreshold, nOctaves, nOctaveLayers, extended, upright);
  86. std::vector<cv::KeyPoint> keypoints_gold;
  87. surf_gold->detect(image, keypoints_gold);
  88. ASSERT_EQ(keypoints_gold.size(), keypoints.size());
  89. int matchedCount = getMatchedPointsCount(keypoints_gold, keypoints);
  90. double matchedRatio = static_cast<double>(matchedCount) / keypoints_gold.size();
  91. EXPECT_GT(matchedRatio, 0.95);
  92. }
  93. CUDA_TEST_P(CUDA_SURF, Detector_Masked)
  94. {
  95. cv::Mat image = readImage("../gpu/features2d/aloe.png", cv::IMREAD_GRAYSCALE);
  96. ASSERT_FALSE(image.empty());
  97. cv::Mat mask(image.size(), CV_8UC1, cv::Scalar::all(1));
  98. mask(cv::Range(0, image.rows / 2), cv::Range(0, image.cols / 2)).setTo(cv::Scalar::all(0));
  99. cv::cuda::SURF_CUDA surf;
  100. surf.hessianThreshold = hessianThreshold;
  101. surf.nOctaves = nOctaves;
  102. surf.nOctaveLayers = nOctaveLayers;
  103. surf.extended = extended;
  104. surf.upright = upright;
  105. surf.keypointsRatio = 0.05f;
  106. std::vector<cv::KeyPoint> keypoints;
  107. surf(loadMat(image), loadMat(mask), keypoints);
  108. cv::Ptr<cv::Feature2D> surf_gold = cv::xfeatures2d::SURF::create(hessianThreshold, nOctaves, nOctaveLayers, extended, upright);
  109. std::vector<cv::KeyPoint> keypoints_gold;
  110. surf_gold->detect(image, keypoints_gold, mask);
  111. ASSERT_EQ(keypoints_gold.size(), keypoints.size());
  112. int matchedCount = getMatchedPointsCount(keypoints_gold, keypoints);
  113. double matchedRatio = static_cast<double>(matchedCount) / keypoints_gold.size();
  114. EXPECT_GT(matchedRatio, 0.95);
  115. }
  116. CUDA_TEST_P(CUDA_SURF, Descriptor)
  117. {
  118. cv::Mat image = readImage("../gpu/features2d/aloe.png", cv::IMREAD_GRAYSCALE);
  119. ASSERT_FALSE(image.empty());
  120. cv::cuda::SURF_CUDA surf;
  121. surf.hessianThreshold = hessianThreshold;
  122. surf.nOctaves = nOctaves;
  123. surf.nOctaveLayers = nOctaveLayers;
  124. surf.extended = extended;
  125. surf.upright = upright;
  126. surf.keypointsRatio = 0.05f;
  127. cv::Ptr<cv::Feature2D> surf_gold = cv::xfeatures2d::SURF::create(hessianThreshold, nOctaves, nOctaveLayers, extended, upright);
  128. std::vector<cv::KeyPoint> keypoints;
  129. surf_gold->detect(image, keypoints);
  130. cv::cuda::GpuMat descriptors;
  131. surf(loadMat(image), cv::cuda::GpuMat(), keypoints, descriptors, true);
  132. cv::Mat descriptors_gold;
  133. surf_gold->compute(image, keypoints, descriptors_gold);
  134. cv::BFMatcher matcher(surf.defaultNorm());
  135. std::vector<cv::DMatch> matches;
  136. matcher.match(descriptors_gold, cv::Mat(descriptors), matches);
  137. int matchedCount = getMatchedPointsCount(keypoints, keypoints, matches);
  138. double matchedRatio = static_cast<double>(matchedCount) / keypoints.size();
  139. EXPECT_GT(matchedRatio, 0.6);
  140. }
  141. #if defined (__x86_64__) || defined (_M_X64)
  142. testing::internal::ValueArray3<SURF_HessianThreshold, SURF_HessianThreshold, SURF_HessianThreshold> thresholdValues =
  143. testing::Values(
  144. SURF_HessianThreshold(100.0),
  145. SURF_HessianThreshold(500.0),
  146. SURF_HessianThreshold(1000.0));
  147. #else
  148. // hessian computation is not bit-exact and lower threshold causes different count of detection
  149. testing::internal::ValueArray2<SURF_HessianThreshold, SURF_HessianThreshold> thresholdValues =
  150. testing::Values(
  151. SURF_HessianThreshold(830.0),
  152. SURF_HessianThreshold(1000.0));
  153. #endif
  154. INSTANTIATE_TEST_CASE_P(CUDA_Features2D, CUDA_SURF, testing::Combine(
  155. thresholdValues,
  156. testing::Values(SURF_Octaves(3), SURF_Octaves(4)),
  157. testing::Values(SURF_OctaveLayers(2), SURF_OctaveLayers(3)),
  158. testing::Values(SURF_Extended(false), SURF_Extended(true)),
  159. testing::Values(SURF_Upright(false), SURF_Upright(true))));
  160. #endif // HAVE_OPENCV_CUDAARITHM
  161. }} // namespace
  162. #endif // HAVE_CUDA && OPENCV_ENABLE_NONFREE