perf_estimators.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include "perf_precomp.hpp"
  2. #include "opencv2/imgcodecs.hpp"
  3. #include "opencv2/opencv_modules.hpp"
  4. namespace opencv_test
  5. {
  6. using namespace perf;
  7. typedef TestBaseWithParam<tuple<string, string> > bundleAdjuster;
  8. #if defined(HAVE_OPENCV_XFEATURES2D) && defined(OPENCV_ENABLE_NONFREE)
  9. #define TEST_DETECTORS testing::Values("surf", "orb")
  10. #else
  11. #define TEST_DETECTORS testing::Values<string>("orb")
  12. #endif
  13. #define WORK_MEGAPIX 0.6
  14. #define AFFINE_FUNCTIONS testing::Values("affinePartial", "affine")
  15. PERF_TEST_P(bundleAdjuster, affine, testing::Combine(TEST_DETECTORS, AFFINE_FUNCTIONS))
  16. {
  17. Mat img1, img1_full = imread(getDataPath("stitching/s1.jpg"));
  18. Mat img2, img2_full = imread(getDataPath("stitching/s2.jpg"));
  19. float scale1 = (float)std::min(1.0, sqrt(WORK_MEGAPIX * 1e6 / img1_full.total()));
  20. float scale2 = (float)std::min(1.0, sqrt(WORK_MEGAPIX * 1e6 / img2_full.total()));
  21. resize(img1_full, img1, Size(), scale1, scale1, INTER_LINEAR_EXACT);
  22. resize(img2_full, img2, Size(), scale2, scale2, INTER_LINEAR_EXACT);
  23. string detector = get<0>(GetParam());
  24. string affine_fun = get<1>(GetParam());
  25. Ptr<Feature2D> finder = getFeatureFinder(detector);
  26. Ptr<detail::FeaturesMatcher> matcher;
  27. Ptr<detail::BundleAdjusterBase> bundle_adjuster;
  28. if (affine_fun == "affinePartial")
  29. {
  30. matcher = makePtr<detail::AffineBestOf2NearestMatcher>(false);
  31. bundle_adjuster = makePtr<detail::BundleAdjusterAffinePartial>();
  32. }
  33. else if (affine_fun == "affine")
  34. {
  35. matcher = makePtr<detail::AffineBestOf2NearestMatcher>(true);
  36. bundle_adjuster = makePtr<detail::BundleAdjusterAffine>();
  37. }
  38. Ptr<detail::Estimator> estimator = makePtr<detail::AffineBasedEstimator>();
  39. std::vector<Mat> images;
  40. images.push_back(img1), images.push_back(img2);
  41. std::vector<detail::ImageFeatures> features;
  42. std::vector<detail::MatchesInfo> pairwise_matches;
  43. std::vector<detail::CameraParams> cameras;
  44. std::vector<detail::CameraParams> cameras2;
  45. computeImageFeatures(finder, images, features);
  46. (*matcher)(features, pairwise_matches);
  47. if (!(*estimator)(features, pairwise_matches, cameras))
  48. FAIL() << "estimation failed. this should never happen.";
  49. // this is currently required
  50. for (size_t i = 0; i < cameras.size(); ++i)
  51. {
  52. Mat R;
  53. cameras[i].R.convertTo(R, CV_32F);
  54. cameras[i].R = R;
  55. }
  56. cameras2 = cameras;
  57. bool success = true;
  58. while(next())
  59. {
  60. cameras = cameras2; // revert cameras back to original initial guess
  61. startTimer();
  62. success = (*bundle_adjuster)(features, pairwise_matches, cameras);
  63. stopTimer();
  64. }
  65. EXPECT_TRUE(success);
  66. EXPECT_TRUE(cameras.size() == 2);
  67. // fist camera should be just identity
  68. Mat &first = cameras[0].R;
  69. SANITY_CHECK(first, 1e-3, ERROR_ABSOLUTE);
  70. // second camera should be the estimated transform between images
  71. // separate rotation and translation in transform matrix
  72. Mat T_second (cameras[1].R, Range(0, 2), Range(2, 3));
  73. Mat R_second (cameras[1].R, Range(0, 2), Range(0, 2));
  74. Mat h (cameras[1].R, Range(2, 3), Range::all());
  75. SANITY_CHECK(T_second, 5, ERROR_ABSOLUTE); // allow 5 pixels diff in translations
  76. SANITY_CHECK(R_second, .01, ERROR_ABSOLUTE); // rotations must be more precise
  77. // last row should be precisely (0, 0, 1) as it is just added for representation in homogeneous
  78. // coordinates
  79. EXPECT_TRUE(h.type() == CV_32F);
  80. EXPECT_FLOAT_EQ(h.at<float>(0), 0.f);
  81. EXPECT_FLOAT_EQ(h.at<float>(1), 0.f);
  82. EXPECT_FLOAT_EQ(h.at<float>(2), 1.f);
  83. }
  84. } // namespace