ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
ransac_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 "optim/ransac"
33 #include "util/testing.h"
34 
35 #include <Eigen/Core>
36 #include <Eigen/Geometry>
37 
38 #include "base/pose.h"
41 #include "optim/ransac.h"
42 #include "util/random.h"
43 
44 using namespace colmap;
45 
46 BOOST_AUTO_TEST_CASE(TestOptions) {
47  RANSACOptions options;
48  BOOST_CHECK_EQUAL(options.max_error, 0);
49  BOOST_CHECK_EQUAL(options.min_inlier_ratio, 0.1);
50  BOOST_CHECK_EQUAL(options.confidence, 0.99);
51  BOOST_CHECK_EQUAL(options.min_num_trials, 0);
52  BOOST_CHECK_EQUAL(options.max_num_trials, std::numeric_limits<size_t>::max());
53 }
54 
55 BOOST_AUTO_TEST_CASE(TestReport) {
57  BOOST_CHECK_EQUAL(report.success, false);
58  BOOST_CHECK_EQUAL(report.num_trials, 0);
59  BOOST_CHECK_EQUAL(report.support.num_inliers, 0);
60  BOOST_CHECK_EQUAL(report.support.residual_sum,
61  std::numeric_limits<double>::max());
62  BOOST_CHECK_EQUAL(report.inlier_mask.size(), 0);
63 }
64 
65 BOOST_AUTO_TEST_CASE(TestNumTrials) {
66  BOOST_CHECK_EQUAL(RANSAC<SimilarityTransformEstimator<3>>::ComputeNumTrials(
67  1, 100, 0.99, 1.0),
68  4605168);
69  BOOST_CHECK_EQUAL(RANSAC<SimilarityTransformEstimator<3>>::ComputeNumTrials(
70  10, 100, 0.99, 1.0),
71  4603);
72  BOOST_CHECK_EQUAL(RANSAC<SimilarityTransformEstimator<3>>::ComputeNumTrials(
73  10, 100, 0.999, 1.0),
74  6905);
75  BOOST_CHECK_EQUAL(RANSAC<SimilarityTransformEstimator<3>>::ComputeNumTrials(
76  10, 100, 0.999, 2.0),
77  13809);
78  BOOST_CHECK_EQUAL(RANSAC<SimilarityTransformEstimator<3>>::ComputeNumTrials(
79  100, 100, 0.99, 1.0),
80  1);
81  BOOST_CHECK_EQUAL(RANSAC<SimilarityTransformEstimator<3>>::ComputeNumTrials(
82  100, 100, 0.999, 1.0),
83  1);
84  BOOST_CHECK_EQUAL(RANSAC<SimilarityTransformEstimator<3>>::ComputeNumTrials(
85  100, 100, 0, 1.0),
86  1);
87 }
88 
89 BOOST_AUTO_TEST_CASE(TestSimilarityTransform) {
90  SetPRNGSeed(0);
91 
92  const size_t num_samples = 1000;
93  const size_t num_outliers = 400;
94 
95  // Create some arbitrary transformation.
96  const SimilarityTransform3 orig_tform(2, ComposeIdentityQuaternion(),
97  Eigen::Vector3d(100, 10, 10));
98 
99  // Generate exact data.
100  std::vector<Eigen::Vector3d> src;
101  std::vector<Eigen::Vector3d> dst;
102  for (size_t i = 0; i < num_samples; ++i) {
103  src.emplace_back(i, std::sqrt(i) + 2, std::sqrt(2 * i + 2));
104  dst.push_back(src.back());
105  orig_tform.TransformPoint(&dst.back());
106  }
107 
108  // Add some faulty data.
109  for (size_t i = 0; i < num_outliers; ++i) {
110  dst[i] = Eigen::Vector3d(RandomReal(-3000.0, -2000.0),
111  RandomReal(-4000.0, -3000.0),
112  RandomReal(-5000.0, -4000.0));
113  }
114 
115  // Robustly estimate transformation using RANSAC.
116  RANSACOptions options;
117  options.max_error = 10;
119  const auto report = ransac.Estimate(src, dst);
120 
121  BOOST_CHECK_EQUAL(report.success, true);
122  BOOST_CHECK_GT(report.num_trials, 0);
123 
124  // Make sure outliers were detected correctly.
125  BOOST_CHECK_EQUAL(report.support.num_inliers, num_samples - num_outliers);
126  for (size_t i = 0; i < num_samples; ++i) {
127  if (i < num_outliers) {
128  BOOST_CHECK(!report.inlier_mask[i]);
129  } else {
130  BOOST_CHECK(report.inlier_mask[i]);
131  }
132  }
133 
134  // Make sure original transformation is estimated correctly.
135  const double matrix_diff =
136  (orig_tform.Matrix().topLeftCorner<3, 4>() - report.model).norm();
137  BOOST_CHECK(std::abs(matrix_diff) < 1e-6);
138 }
Report Estimate(const std::vector< typename Estimator::X_t > &X, const std::vector< typename Estimator::Y_t > &Y)
Definition: ransac.h:159
Eigen::Matrix4d Matrix() const
void TransformPoint(Eigen::Vector3d *xyz) const
const double * e
void SetPRNGSeed(unsigned seed)
Definition: random.cc:40
Eigen::Vector4d ComposeIdentityQuaternion()
Definition: pose.h:209
T RandomReal(const T min, const T max)
Definition: random.h:75
BOOST_AUTO_TEST_CASE(TestOptions)
Definition: ransac_test.cc:46
size_t min_num_trials
Definition: ransac.h:40
double min_inlier_ratio
Definition: ransac.h:29
size_t max_num_trials
Definition: ransac.h:41