test_matchers.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // Intel License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000, Intel Corporation, all rights reserved.
  14. // Third party copyrights are property of their respective owners.
  15. //
  16. // Redistribution and use in source and binary forms, with or without modification,
  17. // are permitted provided that the following conditions are met:
  18. //
  19. // * Redistribution's of source code must retain the above copyright notice,
  20. // this list of conditions and the following disclaimer.
  21. //
  22. // * Redistribution's in binary form must reproduce the above copyright notice,
  23. // this list of conditions and the following disclaimer in the documentation
  24. // and/or other materials provided with the distribution.
  25. //
  26. // * The name of Intel Corporation may not be used to endorse or promote products
  27. // derived from this software without specific prior written permission.
  28. //
  29. // This software is provided by the copyright holders and contributors "as is" and
  30. // any express or implied warranties, including, but not limited to, the implied
  31. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  32. // In no event shall the Intel Corporation or contributors be liable for any direct,
  33. // indirect, incidental, special, exemplary, or consequential damages
  34. // (including, but not limited to, procurement of substitute goods or services;
  35. // loss of use, data, or profits; or business interruption) however caused
  36. // and on any theory of liability, whether in contract, strict liability,
  37. // or tort (including negligence or otherwise) arising in any way out of
  38. // the use of this software, even if advised of the possibility of such damage.
  39. //
  40. //M*/
  41. #include "test_precomp.hpp"
  42. namespace opencv_test { namespace {
  43. #if defined(HAVE_OPENCV_XFEATURES2D) && defined(OPENCV_ENABLE_NONFREE)
  44. TEST(SurfFeaturesFinder, CanFindInROIs)
  45. {
  46. Ptr<Feature2D> finder = xfeatures2d::SURF::create();
  47. Mat img = imread(string(cvtest::TS::ptr()->get_data_path()) + "cv/shared/lena.png");
  48. vector<Rect> rois;
  49. rois.push_back(Rect(0, 0, img.cols / 2, img.rows / 2));
  50. rois.push_back(Rect(img.cols / 2, img.rows / 2, img.cols - img.cols / 2, img.rows - img.rows / 2));
  51. // construct mask
  52. Mat mask = Mat::zeros(img.size(), CV_8U);
  53. for (const Rect &roi : rois)
  54. {
  55. Mat(mask, roi) = 1;
  56. }
  57. detail::ImageFeatures roi_features;
  58. detail::computeImageFeatures(finder, img, roi_features, mask);
  59. int tl_rect_count = 0, br_rect_count = 0, bad_count = 0;
  60. for (const auto &keypoint : roi_features.keypoints)
  61. {
  62. // Workaround for https://github.com/opencv/opencv/issues/26016
  63. // To keep its behaviour, keypoint.pt casts to Point_<int>.
  64. if (rois[0].contains(Point_<int>(keypoint.pt)))
  65. tl_rect_count++;
  66. else if (rois[1].contains(Point_<int>(keypoint.pt)))
  67. br_rect_count++;
  68. else
  69. bad_count++;
  70. }
  71. EXPECT_GT(tl_rect_count, 0);
  72. EXPECT_GT(br_rect_count, 0);
  73. EXPECT_EQ(bad_count, 0);
  74. }
  75. #endif // HAVE_OPENCV_XFEATURES2D && OPENCV_ENABLE_NONFREE
  76. TEST(ParallelFeaturesFinder, IsSameWithSerial)
  77. {
  78. Ptr<Feature2D> para_finder = ORB::create();
  79. Ptr<Feature2D> serial_finder = ORB::create();
  80. Mat img = imread(string(cvtest::TS::ptr()->get_data_path()) + "stitching/a3.png", IMREAD_GRAYSCALE);
  81. detail::ImageFeatures serial_features;
  82. detail::computeImageFeatures(serial_finder, img, serial_features);
  83. vector<Mat> imgs(50, img);
  84. vector<detail::ImageFeatures> para_features(imgs.size());
  85. detail::computeImageFeatures(para_finder, imgs, para_features); // FIXIT This call doesn't use parallel_for_()
  86. // results must be the same
  87. Mat serial_descriptors;
  88. serial_features.descriptors.copyTo(serial_descriptors);
  89. for(size_t i = 0; i < para_features.size(); ++i)
  90. {
  91. SCOPED_TRACE(cv::format("i=%zu", i));
  92. EXPECT_EQ(serial_descriptors.size(), para_features[i].descriptors.size());
  93. #if 0 // FIXIT ORB descriptors are not bit-exact (perhaps due internal parallel_for usage)
  94. ASSERT_EQ(0, cv::norm(u_serial_descriptors, para_features[i].descriptors, NORM_L1))
  95. << "serial_size=" << u_serial_descriptors.size()
  96. << " par_size=" << para_features[i].descriptors.size()
  97. << endl << u_serial_descriptors.getMat(ACCESS_READ)
  98. << endl << endl << para_features[i].descriptors.getMat(ACCESS_READ);
  99. #endif
  100. EXPECT_EQ(serial_features.img_size, para_features[i].img_size);
  101. EXPECT_EQ(serial_features.keypoints.size(), para_features[i].keypoints.size());
  102. }
  103. }
  104. TEST(RangeMatcher, MatchesRangeOnly)
  105. {
  106. Ptr<Feature2D> finder = ORB::create();
  107. Mat img0 = imread(string(cvtest::TS::ptr()->get_data_path()) + "stitching/a1.png", IMREAD_GRAYSCALE);
  108. Mat img1 = imread(string(cvtest::TS::ptr()->get_data_path()) + "stitching/a2.png", IMREAD_GRAYSCALE);
  109. Mat img2 = imread(string(cvtest::TS::ptr()->get_data_path()) + "stitching/a3.png", IMREAD_GRAYSCALE);
  110. vector<detail::ImageFeatures> features(3);
  111. computeImageFeatures(finder, img0, features[0]);
  112. computeImageFeatures(finder, img1, features[1]);
  113. computeImageFeatures(finder, img2, features[2]);
  114. vector<detail::MatchesInfo> pairwise_matches;
  115. Ptr<detail::FeaturesMatcher> matcher = makePtr<detail::BestOf2NearestRangeMatcher>(1);
  116. (*matcher)(features, pairwise_matches);
  117. // matches[1] will be image 0 and image 1, should have non-zero confidence
  118. EXPECT_NE(pairwise_matches[1].confidence, .0);
  119. // matches[2] will be image 0 and image 2, should have zero confidence due to range_width=1
  120. EXPECT_DOUBLE_EQ(pairwise_matches[2].confidence, .0);
  121. }
  122. }} // namespace