test_detectors_invariance.impl.hpp 7.9 KB

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