test_logos_matcher.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. static void loadKeypoints(const std::string& vP_path,
  7. const std::string& oP_path,
  8. const std::string& sP_path,
  9. const std::string& w_path,
  10. std::vector<cv::KeyPoint>& keypoints,
  11. std::vector<int>& nn)
  12. {
  13. {
  14. std::ifstream file(vP_path.c_str());
  15. if (file.is_open())
  16. {
  17. float x = 0, y = 0;
  18. while (file >> x >> y)
  19. {
  20. keypoints.push_back(cv::KeyPoint(x, y, 0));
  21. }
  22. }
  23. }
  24. {
  25. std::ifstream file(oP_path.c_str());
  26. if (file.is_open())
  27. {
  28. float orientation = 0;
  29. size_t idx = 0;
  30. while (file >> orientation)
  31. {
  32. keypoints[idx].angle = static_cast<float>(orientation * 180.0 / CV_PI);
  33. idx++;
  34. }
  35. }
  36. }
  37. {
  38. std::ifstream file(sP_path.c_str());
  39. if (file.is_open())
  40. {
  41. float scale = 0;
  42. size_t idx = 0;
  43. while (file >> scale)
  44. {
  45. keypoints[idx].size = scale;
  46. idx++;
  47. }
  48. }
  49. }
  50. {
  51. std::ifstream file(w_path.c_str());
  52. if (file.is_open())
  53. {
  54. int neighborIdx = 0;
  55. while (file >> neighborIdx)
  56. {
  57. nn.push_back(neighborIdx);
  58. }
  59. }
  60. }
  61. ASSERT_TRUE(!keypoints.empty());
  62. }
  63. static void loadGroundTruth(const std::string& d1_path,
  64. const std::string& b1_path,
  65. std::vector<cv::DMatch>& groundTruth)
  66. {
  67. std::vector<int> d1_vec;
  68. {
  69. std::ifstream file(d1_path.c_str());
  70. if (file.is_open())
  71. {
  72. int idx = 0;
  73. while (file >> idx)
  74. {
  75. d1_vec.push_back(idx-1);
  76. }
  77. }
  78. }
  79. std::vector<int> b1_vec;
  80. {
  81. std::ifstream file(b1_path.c_str());
  82. if (file.is_open())
  83. {
  84. int idx = 0;
  85. while (file >> idx)
  86. {
  87. b1_vec.push_back(idx-1);
  88. }
  89. }
  90. }
  91. ASSERT_TRUE(!d1_vec.empty());
  92. ASSERT_EQ(d1_vec.size(), b1_vec.size());
  93. for (size_t i = 0; i < d1_vec.size(); i++)
  94. {
  95. groundTruth.push_back(cv::DMatch(d1_vec[i], b1_vec[i], 0));
  96. }
  97. }
  98. TEST(XFeatures2d_LogosMatcher, logos_matcher_regression)
  99. {
  100. const std::string vP1_path = cvtest::findDataFile("detectors_descriptors_evaluation/matching/LOGOS/vP1.txt");
  101. const std::string oP1_path = cvtest::findDataFile("detectors_descriptors_evaluation/matching/LOGOS/oP1.txt");
  102. const std::string sP1_path = cvtest::findDataFile("detectors_descriptors_evaluation/matching/LOGOS/sP1.txt");
  103. const std::string w1_path = cvtest::findDataFile("detectors_descriptors_evaluation/matching/LOGOS/w1.txt");
  104. const std::string vP2_path = cvtest::findDataFile("detectors_descriptors_evaluation/matching/LOGOS/vP2.txt");
  105. const std::string oP2_path = cvtest::findDataFile("detectors_descriptors_evaluation/matching/LOGOS/oP2.txt");
  106. const std::string sP2_path = cvtest::findDataFile("detectors_descriptors_evaluation/matching/LOGOS/sP2.txt");
  107. const std::string w2_path = cvtest::findDataFile("detectors_descriptors_evaluation/matching/LOGOS/w2.txt");
  108. std::vector<cv::KeyPoint> keypoints1, keypoints2;
  109. std::vector<int> nn1, nn2;
  110. loadKeypoints(vP1_path, oP1_path, sP1_path, w1_path, keypoints1, nn1);
  111. loadKeypoints(vP2_path, oP2_path, sP2_path, w2_path, keypoints2, nn2);
  112. std::vector<cv::DMatch> matchesLogos;
  113. matchLOGOS(keypoints1, keypoints2, nn1, nn2, matchesLogos);
  114. std::vector<cv::DMatch> groundTruth;
  115. const std::string d1_path = cvtest::findDataFile("detectors_descriptors_evaluation/matching/LOGOS/d1.txt");
  116. const std::string b1_path = cvtest::findDataFile("detectors_descriptors_evaluation/matching/LOGOS/b1.txt");
  117. loadGroundTruth(d1_path, b1_path, groundTruth);
  118. int correctMatches = 0;
  119. for (size_t i = 0; i < matchesLogos.size(); i++)
  120. {
  121. for (size_t j = 0; j < groundTruth.size(); j++)
  122. {
  123. if (groundTruth[j].queryIdx == matchesLogos[i].queryIdx &&
  124. groundTruth[j].trainIdx == matchesLogos[j].trainIdx)
  125. {
  126. correctMatches++;
  127. break;
  128. }
  129. }
  130. }
  131. ASSERT_EQ(static_cast<int>(groundTruth.size()), correctMatches)
  132. << "groundTruth: " << groundTruth.size()
  133. << " ; matchesLogos: " << matchesLogos.size()
  134. << " ; correctMatches: " << correctMatches;
  135. }
  136. }} // namespace