test_gms_matcher.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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_precomp.hpp"
  5. namespace opencv_test { namespace {
  6. class CV_GMSMatcherTest : public cvtest::BaseTest
  7. {
  8. public:
  9. CV_GMSMatcherTest();
  10. ~CV_GMSMatcherTest();
  11. protected:
  12. virtual void run(int);
  13. bool combinations[4][2];
  14. double eps[3][4]; //3 imgs x 4 combinations
  15. double correctMatchDistThreshold;
  16. };
  17. CV_GMSMatcherTest::CV_GMSMatcherTest()
  18. {
  19. combinations[0][0] = false; combinations[0][1] = false;
  20. combinations[1][0] = false; combinations[1][1] = true;
  21. combinations[2][0] = true; combinations[2][1] = false;
  22. combinations[3][0] = true; combinations[3][1] = true;
  23. eps[0][0] = 0.91;
  24. eps[0][1] = 0.91;
  25. eps[0][2] = 0.91;
  26. eps[0][3] = 0.91;
  27. eps[1][0] = 0.80;
  28. eps[1][1] = 0.78;
  29. eps[1][2] = 0.80;
  30. eps[1][3] = 0.78;
  31. eps[2][0] = 0.6;
  32. eps[2][1] = 0.6;
  33. eps[2][2] = 0.6;
  34. eps[2][3] = 0.6;
  35. correctMatchDistThreshold = 5.0;
  36. }
  37. CV_GMSMatcherTest::~CV_GMSMatcherTest() {}
  38. void CV_GMSMatcherTest::run( int )
  39. {
  40. ts->set_failed_test_info(cvtest::TS::OK);
  41. Mat imgRef = imread(string(ts->get_data_path()) + "detectors_descriptors_evaluation/images_datasets/graf/img1.png");
  42. Ptr<Feature2D> orb = ORB::create(10000);
  43. vector<KeyPoint> keypointsRef, keypointsCur;
  44. Mat descriptorsRef, descriptorsCur;
  45. orb->detectAndCompute(imgRef, noArray(), keypointsRef, descriptorsRef);
  46. vector<DMatch> matchesAll, matchesGMS;
  47. Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("BruteForce-Hamming");
  48. const int startImg = 2;
  49. const int nImgs = 3;
  50. for (int num = startImg; num < startImg+nImgs; num++)
  51. {
  52. string fileName = cv::format("img%d.png", num);
  53. string imgPath = string(ts->get_data_path()) + "detectors_descriptors_evaluation/images_datasets/graf/" + fileName;
  54. Mat imgCur = imread(imgPath);
  55. orb->detectAndCompute(imgCur, noArray(), keypointsCur, descriptorsCur);
  56. matcher->match(descriptorsCur, descriptorsRef, matchesAll);
  57. string xml = string(ts->get_data_path()) + format("detectors_descriptors_evaluation/images_datasets/graf/H1to%dp.xml", num);
  58. FileStorage fs(xml, FileStorage::READ);
  59. if (!fs.isOpened())
  60. {
  61. ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
  62. return;
  63. }
  64. Mat H1toCur;
  65. fs[format("H1%d", num)] >> H1toCur;
  66. for (int comb = 0; comb < 4; comb++)
  67. {
  68. matchGMS(imgCur.size(), imgRef.size(), keypointsCur, keypointsRef, matchesAll, matchesGMS, combinations[comb][0], combinations[comb][1]);
  69. int nbCorrectMatches = 0;
  70. for (size_t i = 0; i < matchesGMS.size(); i++)
  71. {
  72. Point2f ptRef = keypointsRef[matchesGMS[i].trainIdx].pt;
  73. Point2f ptCur = keypointsCur[matchesGMS[i].queryIdx].pt;
  74. Mat matRef = (Mat_<double>(3,1) << ptRef.x, ptRef.y, 1);
  75. Mat matTrans = H1toCur * matRef;
  76. Point2f ptTrans( (float) (matTrans.at<double>(0,0)/matTrans.at<double>(2,0)),
  77. (float) (matTrans.at<double>(1,0)/matTrans.at<double>(2,0)));
  78. if (cv::norm(ptTrans-ptCur) < correctMatchDistThreshold)
  79. nbCorrectMatches++;
  80. }
  81. double ratio = nbCorrectMatches / (double) matchesGMS.size();
  82. EXPECT_GT(ratio, eps[num-startImg][comb]) <<
  83. cv::format("Invalid accuracy for image %s and combination withRotation=%d withScale=%d, "
  84. "matches ratio is %g, ratio threshold is %g, distance threshold is %g.",
  85. fileName.c_str(), combinations[comb][0], combinations[comb][1], ratio,
  86. eps[num-startImg][comb], correctMatchDistThreshold);
  87. }
  88. }
  89. }
  90. TEST(XFeatures2d_GMSMatcher, gms_matcher_regression) { CV_GMSMatcherTest test; test.safe_run(); }
  91. }} // namespace