Path: blob/master/modules/features2d/test/test_detectors_invariance.impl.hpp
16356 views
// This file is part of OpenCV project.1// It is subject to the license terms in the LICENSE file found in the top-level directory2// of this distribution and at http://opencv.org/license.html34#include "test_invariance_utils.hpp"56namespace opencv_test { namespace {78#define SHOW_DEBUG_LOG 1910typedef tuple<std::string, Ptr<FeatureDetector>, float, float> String_FeatureDetector_Float_Float_t;111213static14void matchKeyPoints(const vector<KeyPoint>& keypoints0, const Mat& H,15const vector<KeyPoint>& keypoints1,16vector<DMatch>& matches)17{18vector<Point2f> points0;19KeyPoint::convert(keypoints0, points0);20Mat points0t;21if(H.empty())22points0t = Mat(points0);23else24perspectiveTransform(Mat(points0), points0t, H);2526matches.clear();27vector<uchar> usedMask(keypoints1.size(), 0);28for(int i0 = 0; i0 < static_cast<int>(keypoints0.size()); i0++)29{30int nearestPointIndex = -1;31float maxIntersectRatio = 0.f;32const float r0 = 0.5f * keypoints0[i0].size;33for(size_t i1 = 0; i1 < keypoints1.size(); i1++)34{35if(nearestPointIndex >= 0 && usedMask[i1])36continue;3738float r1 = 0.5f * keypoints1[i1].size;39float intersectRatio = calcIntersectRatio(points0t.at<Point2f>(i0), r0,40keypoints1[i1].pt, r1);41if(intersectRatio > maxIntersectRatio)42{43maxIntersectRatio = intersectRatio;44nearestPointIndex = static_cast<int>(i1);45}46}4748matches.push_back(DMatch(i0, nearestPointIndex, maxIntersectRatio));49if(nearestPointIndex >= 0)50usedMask[nearestPointIndex] = 1;51}52}5354class DetectorInvariance : public TestWithParam<String_FeatureDetector_Float_Float_t>55{56protected:57virtual void SetUp() {58// Read test data59const std::string filename = cvtest::TS::ptr()->get_data_path() + get<0>(GetParam());60image0 = imread(filename);61ASSERT_FALSE(image0.empty()) << "couldn't read input image";6263featureDetector = get<1>(GetParam());64minKeyPointMatchesRatio = get<2>(GetParam());65minInliersRatio = get<3>(GetParam());66}6768Ptr<FeatureDetector> featureDetector;69float minKeyPointMatchesRatio;70float minInliersRatio;71Mat image0;72};7374typedef DetectorInvariance DetectorScaleInvariance;75typedef DetectorInvariance DetectorRotationInvariance;7677TEST_P(DetectorRotationInvariance, rotation)78{79Mat image1, mask1;80const int borderSize = 16;81Mat mask0(image0.size(), CV_8UC1, Scalar(0));82mask0(Rect(borderSize, borderSize, mask0.cols - 2*borderSize, mask0.rows - 2*borderSize)).setTo(Scalar(255));8384vector<KeyPoint> keypoints0;85featureDetector->detect(image0, keypoints0, mask0);86EXPECT_GE(keypoints0.size(), 15u);8788const int maxAngle = 360, angleStep = 15;89for(int angle = 0; angle < maxAngle; angle += angleStep)90{91Mat H = rotateImage(image0, mask0, static_cast<float>(angle), image1, mask1);9293vector<KeyPoint> keypoints1;94featureDetector->detect(image1, keypoints1, mask1);9596vector<DMatch> matches;97matchKeyPoints(keypoints0, H, keypoints1, matches);9899int angleInliersCount = 0;100101const float minIntersectRatio = 0.5f;102int keyPointMatchesCount = 0;103for(size_t m = 0; m < matches.size(); m++)104{105if(matches[m].distance < minIntersectRatio)106continue;107108keyPointMatchesCount++;109110// Check does this inlier have consistent angles111const float maxAngleDiff = 15.f; // grad112float angle0 = keypoints0[matches[m].queryIdx].angle;113float angle1 = keypoints1[matches[m].trainIdx].angle;114ASSERT_FALSE(angle0 == -1 || angle1 == -1) << "Given FeatureDetector is not rotation invariant, it can not be tested here.";115ASSERT_GE(angle0, 0.f);116ASSERT_LT(angle0, 360.f);117ASSERT_GE(angle1, 0.f);118ASSERT_LT(angle1, 360.f);119120float rotAngle0 = angle0 + angle;121if(rotAngle0 >= 360.f)122rotAngle0 -= 360.f;123124float angleDiff = std::max(rotAngle0, angle1) - std::min(rotAngle0, angle1);125angleDiff = std::min(angleDiff, static_cast<float>(360.f - angleDiff));126ASSERT_GE(angleDiff, 0.f);127bool isAngleCorrect = angleDiff < maxAngleDiff;128if(isAngleCorrect)129angleInliersCount++;130}131132float keyPointMatchesRatio = static_cast<float>(keyPointMatchesCount) / keypoints0.size();133EXPECT_GE(keyPointMatchesRatio, minKeyPointMatchesRatio) << "angle: " << angle;134135if(keyPointMatchesCount)136{137float angleInliersRatio = static_cast<float>(angleInliersCount) / keyPointMatchesCount;138EXPECT_GE(angleInliersRatio, minInliersRatio) << "angle: " << angle;139}140#if SHOW_DEBUG_LOG141std::cout142<< "angle = " << angle143<< ", keypoints = " << keypoints1.size()144<< ", keyPointMatchesRatio = " << keyPointMatchesRatio145<< ", angleInliersRatio = " << (keyPointMatchesCount ? (static_cast<float>(angleInliersCount) / keyPointMatchesCount) : 0)146<< std::endl;147#endif148}149}150151TEST_P(DetectorScaleInvariance, scale)152{153vector<KeyPoint> keypoints0;154featureDetector->detect(image0, keypoints0);155EXPECT_GE(keypoints0.size(), 15u);156157for(int scaleIdx = 1; scaleIdx <= 3; scaleIdx++)158{159float scale = 1.f + scaleIdx * 0.5f;160Mat image1;161resize(image0, image1, Size(), 1./scale, 1./scale, INTER_LINEAR_EXACT);162163vector<KeyPoint> keypoints1, osiKeypoints1; // osi - original size image164featureDetector->detect(image1, keypoints1);165EXPECT_GE(keypoints1.size(), 15u);166EXPECT_LE(keypoints1.size(), keypoints0.size()) << "Strange behavior of the detector. "167"It gives more points count in an image of the smaller size.";168169scaleKeyPoints(keypoints1, osiKeypoints1, scale);170vector<DMatch> matches;171// image1 is query image (it's reduced image0)172// image0 is train image173matchKeyPoints(osiKeypoints1, Mat(), keypoints0, matches);174175const float minIntersectRatio = 0.5f;176int keyPointMatchesCount = 0;177int scaleInliersCount = 0;178179for(size_t m = 0; m < matches.size(); m++)180{181if(matches[m].distance < minIntersectRatio)182continue;183184keyPointMatchesCount++;185186// Check does this inlier have consistent sizes187const float maxSizeDiff = 0.8f;//0.9f; // grad188float size0 = keypoints0[matches[m].trainIdx].size;189float size1 = osiKeypoints1[matches[m].queryIdx].size;190ASSERT_GT(size0, 0);191ASSERT_GT(size1, 0);192if(std::min(size0, size1) > maxSizeDiff * std::max(size0, size1))193scaleInliersCount++;194}195196float keyPointMatchesRatio = static_cast<float>(keyPointMatchesCount) / keypoints1.size();197EXPECT_GE(keyPointMatchesRatio, minKeyPointMatchesRatio);198199if(keyPointMatchesCount)200{201float scaleInliersRatio = static_cast<float>(scaleInliersCount) / keyPointMatchesCount;202EXPECT_GE(scaleInliersRatio, minInliersRatio);203}204#if SHOW_DEBUG_LOG205std::cout206<< "scale = " << scale207<< ", keyPointMatchesRatio = " << keyPointMatchesRatio208<< ", scaleInliersRatio = " << (keyPointMatchesCount ? static_cast<float>(scaleInliersCount) / keyPointMatchesCount : 0)209<< std::endl;210#endif211}212}213214#undef SHOW_DEBUG_LOG215}} // namespace216217namespace std {218using namespace opencv_test;219static inline void PrintTo(const String_FeatureDetector_Float_Float_t& v, std::ostream* os)220{221*os << "(\"" << get<0>(v)222<< "\", " << get<2>(v)223<< ", " << get<3>(v)224<< ")";225}226} // namespace227228229