test_detectors_invariance.impl.hpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html
  4. #include "test_invariance_utils.hpp"
  5. #include <functional>
  6. namespace opencv_test { namespace {
  7. #define SHOW_DEBUG_LOG 1
  8. // NOTE: using factory function (function<Ptr<Type>()>) instead of object instance (Ptr<Type>) as a
  9. // test parameter, because parameters exist during whole test program run and consume a lot of memory
  10. typedef std::function<cv::Ptr<cv::FeatureDetector>()> DetectorFactory;
  11. typedef tuple<std::string, DetectorFactory, float, float> String_FeatureDetector_Float_Float_t;
  12. static
  13. void matchKeyPoints(const vector<KeyPoint>& keypoints0, const Mat& H,
  14. const vector<KeyPoint>& keypoints1,
  15. vector<DMatch>& matches)
  16. {
  17. vector<Point2f> points0;
  18. KeyPoint::convert(keypoints0, points0);
  19. Mat points0t;
  20. if(H.empty())
  21. points0t = Mat(points0);
  22. else
  23. perspectiveTransform(Mat(points0), points0t, H);
  24. matches.clear();
  25. for(int i0 = 0; i0 < static_cast<int>(keypoints0.size()); i0++)
  26. {
  27. int nearestPointIndex = -1;
  28. float maxIntersectRatio = 0.f;
  29. const float r0 = 0.5f * keypoints0[i0].size;
  30. for(size_t i1 = 0; i1 < keypoints1.size(); i1++)
  31. {
  32. float r1 = 0.5f * keypoints1[i1].size;
  33. float intersectRatio = calcIntersectRatio(points0t.at<Point2f>(i0), r0,
  34. keypoints1[i1].pt, r1);
  35. if(intersectRatio > maxIntersectRatio)
  36. {
  37. maxIntersectRatio = intersectRatio;
  38. nearestPointIndex = static_cast<int>(i1);
  39. }
  40. }
  41. matches.push_back(DMatch(i0, nearestPointIndex, maxIntersectRatio));
  42. }
  43. }
  44. class DetectorInvariance : public TestWithParam<String_FeatureDetector_Float_Float_t>
  45. {
  46. protected:
  47. virtual void SetUp() {
  48. // Read test data
  49. const std::string filename = cvtest::TS::ptr()->get_data_path() + get<0>(GetParam());
  50. image0 = imread(filename);
  51. ASSERT_FALSE(image0.empty()) << "couldn't read input image";
  52. featureDetector = get<1>(GetParam())();
  53. minKeyPointMatchesRatio = get<2>(GetParam());
  54. minInliersRatio = get<3>(GetParam());
  55. }
  56. Ptr<FeatureDetector> featureDetector;
  57. float minKeyPointMatchesRatio;
  58. float minInliersRatio;
  59. Mat image0;
  60. };
  61. typedef DetectorInvariance DetectorScaleInvariance;
  62. typedef DetectorInvariance DetectorRotationInvariance;
  63. TEST_P(DetectorRotationInvariance, rotation)
  64. {
  65. Mat image1, mask1;
  66. const int borderSize = 16;
  67. Mat mask0(image0.size(), CV_8UC1, Scalar(0));
  68. mask0(Rect(borderSize, borderSize, mask0.cols - 2*borderSize, mask0.rows - 2*borderSize)).setTo(Scalar(255));
  69. vector<KeyPoint> keypoints0;
  70. featureDetector->detect(image0, keypoints0, mask0);
  71. EXPECT_GE(keypoints0.size(), 15u);
  72. const int maxAngle = 360, angleStep = 15;
  73. for(int angle = 0; angle < maxAngle; angle += angleStep)
  74. {
  75. Mat H = rotateImage(image0, mask0, static_cast<float>(angle), image1, mask1);
  76. vector<KeyPoint> keypoints1;
  77. featureDetector->detect(image1, keypoints1, mask1);
  78. vector<DMatch> matches;
  79. matchKeyPoints(keypoints0, H, keypoints1, matches);
  80. int angleInliersCount = 0;
  81. const float minIntersectRatio = 0.5f;
  82. int keyPointMatchesCount = 0;
  83. for(size_t m = 0; m < matches.size(); m++)
  84. {
  85. if(matches[m].distance < minIntersectRatio)
  86. continue;
  87. keyPointMatchesCount++;
  88. // Check does this inlier have consistent angles
  89. const float maxAngleDiff = 15.f; // grad
  90. float angle0 = keypoints0[matches[m].queryIdx].angle;
  91. float angle1 = keypoints1[matches[m].trainIdx].angle;
  92. ASSERT_FALSE(angle0 == -1 || angle1 == -1) << "Given FeatureDetector is not rotation invariant, it can not be tested here.";
  93. ASSERT_GE(angle0, 0.f);
  94. ASSERT_LT(angle0, 360.f);
  95. ASSERT_GE(angle1, 0.f);
  96. ASSERT_LT(angle1, 360.f);
  97. float rotAngle0 = angle0 + angle;
  98. if(rotAngle0 >= 360.f)
  99. rotAngle0 -= 360.f;
  100. float angleDiff = std::max(rotAngle0, angle1) - std::min(rotAngle0, angle1);
  101. angleDiff = std::min(angleDiff, static_cast<float>(360.f - angleDiff));
  102. ASSERT_GE(angleDiff, 0.f);
  103. bool isAngleCorrect = angleDiff < maxAngleDiff;
  104. if(isAngleCorrect)
  105. angleInliersCount++;
  106. }
  107. float keyPointMatchesRatio = static_cast<float>(keyPointMatchesCount) / keypoints0.size();
  108. EXPECT_GE(keyPointMatchesRatio, minKeyPointMatchesRatio) << "angle: " << angle;
  109. if(keyPointMatchesCount)
  110. {
  111. float angleInliersRatio = static_cast<float>(angleInliersCount) / keyPointMatchesCount;
  112. EXPECT_GE(angleInliersRatio, minInliersRatio) << "angle: " << angle;
  113. }
  114. #if SHOW_DEBUG_LOG
  115. std::cout
  116. << "angle = " << angle
  117. << ", keypoints = " << keypoints1.size()
  118. << ", keyPointMatchesRatio = " << keyPointMatchesRatio
  119. << ", angleInliersRatio = " << (keyPointMatchesCount ? (static_cast<float>(angleInliersCount) / keyPointMatchesCount) : 0)
  120. << std::endl;
  121. #endif
  122. }
  123. }
  124. TEST_P(DetectorScaleInvariance, scale)
  125. {
  126. vector<KeyPoint> keypoints0;
  127. featureDetector->detect(image0, keypoints0);
  128. EXPECT_GE(keypoints0.size(), 15u);
  129. for(int scaleIdx = 1; scaleIdx <= 3; scaleIdx++)
  130. {
  131. float scale = 1.f + scaleIdx * 0.5f;
  132. Mat image1;
  133. resize(image0, image1, Size(), 1./scale, 1./scale, INTER_LINEAR_EXACT);
  134. vector<KeyPoint> keypoints1, osiKeypoints1; // osi - original size image
  135. featureDetector->detect(image1, keypoints1);
  136. EXPECT_GE(keypoints1.size(), 15u);
  137. EXPECT_LE(keypoints1.size(), keypoints0.size()) << "Strange behavior of the detector. "
  138. "It gives more points count in an image of the smaller size.";
  139. scaleKeyPoints(keypoints1, osiKeypoints1, scale);
  140. vector<DMatch> matches;
  141. // image1 is query image (it's reduced image0)
  142. // image0 is train image
  143. matchKeyPoints(osiKeypoints1, Mat(), keypoints0, matches);
  144. const float minIntersectRatio = 0.5f;
  145. int keyPointMatchesCount = 0;
  146. int scaleInliersCount = 0;
  147. for(size_t m = 0; m < matches.size(); m++)
  148. {
  149. if(matches[m].distance < minIntersectRatio)
  150. continue;
  151. keyPointMatchesCount++;
  152. // Check does this inlier have consistent sizes
  153. const float maxSizeDiff = 0.8f;//0.9f; // grad
  154. float size0 = keypoints0[matches[m].trainIdx].size;
  155. float size1 = osiKeypoints1[matches[m].queryIdx].size;
  156. ASSERT_GT(size0, 0);
  157. ASSERT_GT(size1, 0);
  158. if(std::min(size0, size1) > maxSizeDiff * std::max(size0, size1))
  159. scaleInliersCount++;
  160. }
  161. float keyPointMatchesRatio = static_cast<float>(keyPointMatchesCount) / keypoints1.size();
  162. EXPECT_GE(keyPointMatchesRatio, minKeyPointMatchesRatio);
  163. if(keyPointMatchesCount)
  164. {
  165. float scaleInliersRatio = static_cast<float>(scaleInliersCount) / keyPointMatchesCount;
  166. EXPECT_GE(scaleInliersRatio, minInliersRatio);
  167. }
  168. #if SHOW_DEBUG_LOG
  169. std::cout
  170. << "scale = " << scale
  171. << ", keyPointMatchesRatio = " << keyPointMatchesRatio
  172. << ", scaleInliersRatio = " << (keyPointMatchesCount ? static_cast<float>(scaleInliersCount) / keyPointMatchesCount : 0)
  173. << std::endl;
  174. #endif
  175. }
  176. }
  177. #undef SHOW_DEBUG_LOG
  178. }} // namespace
  179. namespace std {
  180. using namespace opencv_test;
  181. static inline void PrintTo(const String_FeatureDetector_Float_Float_t& v, std::ostream* os)
  182. {
  183. *os << "(\"" << get<0>(v)
  184. << "\", " << get<2>(v)
  185. << ", " << get<3>(v)
  186. << ")";
  187. }
  188. } // namespace