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