ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
essential_matrix_test.cc
Go to the documentation of this file.
1 // Copyright (c) 2018, ETH Zurich and UNC Chapel Hill.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 //
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 //
14 // * Neither the name of ETH Zurich and UNC Chapel Hill nor the names of
15 // its contributors may be used to endorse or promote products derived
16 // from this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 // POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Johannes L. Schoenberger (jsch-at-demuc-dot-de)
31 
32 #define TEST_NAME "estimators/essential_matrix"
33 #include "util/testing.h"
34 
35 #include <Eigen/Core>
36 
37 #include "base/camera_models.h"
38 #include "base/essential_matrix.h"
39 #include "base/pose.h"
40 #include "base/projection.h"
42 #include "optim/ransac.h"
43 #include "util/random.h"
44 
45 using namespace colmap;
46 
47 BOOST_AUTO_TEST_CASE(TestFivePoint) {
48  const double points1_raw[] = {
49  0.4964, 1.0577, 0.3650, -0.0919, -0.5412, 0.0159, -0.5239, 0.9467,
50  0.3467, 0.5301, 0.2797, 0.0012, -0.1986, 0.0460, -0.1622, 0.5347,
51  0.0796, 0.2379, -0.3946, 0.7969, 0.2, 0.7, 0.6, 0.3};
52 
53  const double points2_raw[] = {
54  0.7570, 2.7340, 0.3961, 0.6981, -0.6014, 0.7110, -0.7385, 2.2712,
55  0.4177, 1.2132, 0.3052, 0.4835, -0.2171, 0.5057, -0.2059, 1.1583,
56  0.0946, 0.7013, -0.6236, 3.0253, 0.5, 0.9, 0.9, 0.2};
57 
58  const size_t num_points = 12;
59 
60  std::vector<Eigen::Vector2d> points1(num_points);
61  std::vector<Eigen::Vector2d> points2(num_points);
62  for (size_t i = 0; i < num_points; ++i) {
63  points1[i] = Eigen::Vector2d(points1_raw[2 * i], points1_raw[2 * i + 1]);
64  points2[i] = Eigen::Vector2d(points2_raw[2 * i], points2_raw[2 * i + 1]);
65  }
66 
67  // Enforce repeatable tests
68  SetPRNGSeed(0);
69 
70  RANSACOptions options;
71  options.max_error = 0.02;
72  options.confidence = 0.9999;
73  options.min_inlier_ratio = 0.1;
74 
76 
77  const auto report = ransac.Estimate(points1, points2);
78 
79  std::vector<double> residuals;
80  EssentialMatrixFivePointEstimator::Residuals(points1, points2, report.model,
81  &residuals);
82 
83  for (size_t i = 0; i < 10; ++i) {
84  BOOST_CHECK_LE(residuals[i], options.max_error * options.max_error);
85  }
86 
87  BOOST_CHECK(!report.inlier_mask[10]);
88  BOOST_CHECK(!report.inlier_mask[11]);
89 }
90 
91 BOOST_AUTO_TEST_CASE(TestEightPoint) {
92  const double points1_raw[] = {1.839035, 1.924743, 0.543582, 0.375221,
93  0.473240, 0.142522, 0.964910, 0.598376,
94  0.102388, 0.140092, 15.994343, 9.622164,
95  0.285901, 0.430055, 0.091150, 0.254594};
96 
97  const double points2_raw[] = {
98  1.002114, 1.129644, 1.521742, 1.846002, 1.084332, 0.275134,
99  0.293328, 0.588992, 0.839509, 0.087290, 1.779735, 1.116857,
100  0.878616, 0.602447, 0.642616, 1.028681,
101  };
102 
103  const size_t kNumPoints = 8;
104  std::vector<Eigen::Vector2d> points1(kNumPoints);
105  std::vector<Eigen::Vector2d> points2(kNumPoints);
106  for (size_t i = 0; i < kNumPoints; ++i) {
107  points1[i] = Eigen::Vector2d(points1_raw[2 * i], points1_raw[2 * i + 1]);
108  points2[i] = Eigen::Vector2d(points2_raw[2 * i], points2_raw[2 * i + 1]);
109  }
110 
112  const auto E = estimator.Estimate(points1, points2)[0];
113 
114  // Reference values.
115  BOOST_CHECK(std::abs(E(0, 0) - -0.0368602) < 1e-5);
116  BOOST_CHECK(std::abs(E(0, 1) - 0.265019) < 1e-5);
117  BOOST_CHECK(std::abs(E(0, 2) - -0.0625948) < 1e-5);
118  BOOST_CHECK(std::abs(E(1, 0) - -0.299679) < 1e-5);
119  BOOST_CHECK(std::abs(E(1, 1) - -0.110667) < 1e-5);
120  BOOST_CHECK(std::abs(E(1, 2) - 0.147114) < 1e-5);
121  BOOST_CHECK(std::abs(E(2, 0) - 0.169381) < 1e-5);
122  BOOST_CHECK(std::abs(E(2, 1) - -0.21072) < 1e-5);
123  BOOST_CHECK(std::abs(E(2, 2) - -0.00401306) < 1e-5);
124 
125  // Check that the internal constraint is satisfied (two singular values equal
126  // and one zero).
127  Eigen::JacobiSVD<Eigen::Matrix3d> svd(E);
128  Eigen::Vector3d s = svd.singularValues();
129  BOOST_CHECK(std::abs(s(0) - s(1)) < 1e-5);
130  BOOST_CHECK(std::abs(s(2)) < 1e-5);
131 }
BOOST_AUTO_TEST_CASE(TestDecomposeEssentialMatrix)
static std::vector< M_t > Estimate(const std::vector< X_t > &points1, const std::vector< Y_t > &points2)
static void Residuals(const std::vector< X_t > &points1, const std::vector< Y_t > &points2, const M_t &E, std::vector< double > *residuals)
Report Estimate(const std::vector< typename Estimator::X_t > &X, const std::vector< typename Estimator::Y_t > &Y)
Definition: ransac.h:159
const double * e
void SetPRNGSeed(unsigned seed)
Definition: random.cc:40
double min_inlier_ratio
Definition: ransac.h:29