test_surf.ocl.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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) 2010-2012, Multicoreware, Inc., all rights reserved.
  14. // Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
  15. // Third party copyrights are property of their respective owners.
  16. //
  17. // @Authors
  18. // Peng Xiao, pengxiao@multicorewareinc.com
  19. //
  20. // Redistribution and use in source and binary forms, with or without modification,
  21. // are permitted provided that the following conditions are met:
  22. //
  23. // * Redistribution's of source code must retain the above copyright notice,
  24. // this list of conditions and the following disclaimer.
  25. //
  26. // * Redistribution's in binary form must reproduce the above copyright notice,
  27. // this list of conditions and the following disclaimer in the documentation
  28. // and/or other materials provided with the distribution.
  29. //
  30. // * The name of the copyright holders may not be used to endorse or promote products
  31. // derived from this software without specific prior written permission.
  32. //
  33. // This software is provided by the copyright holders and contributors as is and
  34. // any express or implied warranties, including, but not limited to, the implied
  35. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  36. // In no event shall the Intel Corporation or contributors be liable for any direct,
  37. // indirect, incidental, special, exemplary, or consequential damages
  38. // (including, but not limited to, procurement of substitute goods or services;
  39. // loss of use, data, or profits; or business interruption) however caused
  40. // and on any theory of liability, whether in contract, strict liability,
  41. // or tort (including negligence or otherwise) arising in any way out of
  42. // the use of this software, even if advised of the possibility of such damage.
  43. //
  44. //M*/
  45. #include "test_precomp.hpp"
  46. #if defined(HAVE_OPENCL) && defined(OPENCV_ENABLE_NONFREE)
  47. namespace opencv_test { namespace {
  48. static bool keyPointsEquals(const cv::KeyPoint& p1, const cv::KeyPoint& p2)
  49. {
  50. const double maxPtDif = 0.1;
  51. const double maxSizeDif = 0.1;
  52. const double maxAngleDif = 0.1;
  53. const double maxResponseDif = 0.01;
  54. double dist = cv::norm(p1.pt - p2.pt);
  55. if (dist < maxPtDif &&
  56. fabs(p1.size - p2.size) < maxSizeDif &&
  57. abs(p1.angle - p2.angle) < maxAngleDif &&
  58. abs(p1.response - p2.response) < maxResponseDif &&
  59. p1.octave == p2.octave &&
  60. p1.class_id == p2.class_id)
  61. {
  62. return true;
  63. }
  64. return false;
  65. }
  66. static int getMatchedPointsCount(std::vector<cv::KeyPoint>& gold, std::vector<cv::KeyPoint>& actual)
  67. {
  68. std::sort(actual.begin(), actual.end(), perf::comparators::KeypointGreater());
  69. std::sort(gold.begin(), gold.end(), perf::comparators::KeypointGreater());
  70. int validCount = 0;
  71. for (size_t i = 0; i < gold.size(); ++i)
  72. {
  73. const cv::KeyPoint& p1 = gold[i];
  74. const cv::KeyPoint& p2 = actual[i];
  75. if (keyPointsEquals(p1, p2))
  76. ++validCount;
  77. }
  78. return validCount;
  79. }
  80. static int getMatchedPointsCount(const std::vector<cv::KeyPoint>& keypoints1, const std::vector<cv::KeyPoint>& keypoints2, const std::vector<cv::DMatch>& matches)
  81. {
  82. int validCount = 0;
  83. for (size_t i = 0; i < matches.size(); ++i)
  84. {
  85. const cv::DMatch& m = matches[i];
  86. const cv::KeyPoint& p1 = keypoints1[m.queryIdx];
  87. const cv::KeyPoint& p2 = keypoints2[m.trainIdx];
  88. if (keyPointsEquals(p1, p2))
  89. ++validCount;
  90. }
  91. return validCount;
  92. }
  93. IMPLEMENT_PARAM_CLASS(HessianThreshold, double)
  94. IMPLEMENT_PARAM_CLASS(Octaves, int)
  95. IMPLEMENT_PARAM_CLASS(OctaveLayers, int)
  96. IMPLEMENT_PARAM_CLASS(Extended, bool)
  97. IMPLEMENT_PARAM_CLASS(Upright, bool)
  98. PARAM_TEST_CASE(SURF, HessianThreshold, Octaves, OctaveLayers, Extended, Upright)
  99. {
  100. bool useOpenCL;
  101. double hessianThreshold;
  102. int nOctaves;
  103. int nOctaveLayers;
  104. bool extended;
  105. bool upright;
  106. virtual void SetUp()
  107. {
  108. useOpenCL = cv::ocl::useOpenCL();
  109. hessianThreshold = get<0>(GetParam());
  110. nOctaves = get<1>(GetParam());
  111. nOctaveLayers = get<2>(GetParam());
  112. extended = get<3>(GetParam());
  113. upright = get<4>(GetParam());
  114. }
  115. virtual void TearDown()
  116. {
  117. cv::ocl::setUseOpenCL(useOpenCL);
  118. }
  119. };
  120. TEST_P(SURF, Detector)
  121. {
  122. cv::UMat image;
  123. cv::ocl::setUseOpenCL(true);
  124. cv::imread(string(cvtest::TS::ptr()->get_data_path()) + "shared/fruits.png", cv::IMREAD_GRAYSCALE).copyTo(image);
  125. ASSERT_FALSE(image.empty());
  126. cv::Ptr<cv::xfeatures2d::SURF> surf = cv::xfeatures2d::SURF::create(hessianThreshold, nOctaves, nOctaveLayers, extended, upright);
  127. std::vector<cv::KeyPoint> keypoints;
  128. surf->detect(image, keypoints, cv::noArray());
  129. cv::ocl::setUseOpenCL(false);
  130. std::vector<cv::KeyPoint> keypoints_gold;
  131. surf->detect(image, keypoints_gold, cv::noArray());
  132. ASSERT_EQ(keypoints_gold.size(), keypoints.size());
  133. int matchedCount = getMatchedPointsCount(keypoints_gold, keypoints);
  134. double matchedRatio = static_cast<double>(matchedCount) / keypoints_gold.size();
  135. EXPECT_GT(matchedRatio, 0.99);
  136. }
  137. TEST_P(SURF, Descriptor)
  138. {
  139. cv::UMat image;
  140. cv::ocl::setUseOpenCL(true);
  141. cv::imread(string(cvtest::TS::ptr()->get_data_path()) + "shared/fruits.png", cv::IMREAD_GRAYSCALE).copyTo(image);
  142. ASSERT_FALSE(image.empty());
  143. cv::Ptr<cv::xfeatures2d::SURF> surf = cv::xfeatures2d::SURF::create(hessianThreshold, nOctaves, nOctaveLayers, extended, upright);
  144. std::vector<cv::KeyPoint> keypoints;
  145. surf->detect(image, keypoints, cv::noArray());
  146. cv::UMat descriptors;
  147. surf->detectAndCompute(image, cv::noArray(), keypoints, descriptors, true);
  148. cv::ocl::setUseOpenCL(false);
  149. cv::Mat descriptors_gold;
  150. surf->detectAndCompute(image, cv::noArray(), keypoints, descriptors_gold, true);
  151. cv::BFMatcher matcher(surf->defaultNorm());
  152. std::vector<cv::DMatch> matches;
  153. matcher.match(descriptors_gold, descriptors, matches);
  154. int matchedCount = getMatchedPointsCount(keypoints, keypoints, matches);
  155. double matchedRatio = static_cast<double>(matchedCount) / keypoints.size();
  156. EXPECT_GT(matchedRatio, 0.35);
  157. }
  158. INSTANTIATE_TEST_CASE_P(OCL_Features2D, SURF, testing::Combine(
  159. testing::Values(HessianThreshold(500.0), HessianThreshold(1000.0)),
  160. testing::Values(Octaves(3), Octaves(4)),
  161. testing::Values(OctaveLayers(2), OctaveLayers(3)),
  162. testing::Values(Extended(false), Extended(true)),
  163. testing::Values(Upright(false), Upright(true))));
  164. }} // namespace
  165. #endif // HAVE_OPENCL && OPENCV_ENABLE_NONFREE