ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
affine_transform.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 
33 
34 #include <Eigen/SVD>
35 
36 #include "util/logging.h"
37 
38 namespace colmap {
39 
40 std::vector<AffineTransformEstimator::M_t> AffineTransformEstimator::Estimate(
41  const std::vector<X_t>& points1, const std::vector<Y_t>& points2) {
42  CHECK_EQ(points1.size(), points2.size());
43  CHECK_GE(points1.size(), 3);
44 
45  // Sets up the linear system that we solve to obtain a least squared solution
46  // for the affine transformation.
47  Eigen::MatrixXd C(2 * points1.size(), 6);
48  C.setZero();
49  Eigen::VectorXd b(2 * points1.size(), 1);
50 
51  for (size_t i = 0; i < points1.size(); ++i) {
52  const Eigen::Vector2d& x1 = points1[i];
53  const Eigen::Vector2d& x2 = points2[i];
54 
55  C(2 * i, 0) = x1(0);
56  C(2 * i, 1) = x1(1);
57  C(2 * i, 2) = 1.0f;
58  b(2 * i) = x2(0);
59 
60  C(2 * i + 1, 3) = x1(0);
61  C(2 * i + 1, 4) = x1(1);
62  C(2 * i + 1, 5) = 1.0f;
63  b(2 * i + 1) = x2(1);
64  }
65 
66  const Eigen::VectorXd nullspace =
67  C.jacobiSvd(Eigen::ComputeThinU | Eigen::ComputeThinV).solve(b);
68 
69  Eigen::Map<const Eigen::Matrix<double, 3, 2>> A_t(nullspace.data());
70 
71  const std::vector<M_t> models = {A_t.transpose()};
72  return models;
73 }
74 
75 void AffineTransformEstimator::Residuals(const std::vector<X_t>& points1,
76  const std::vector<Y_t>& points2,
77  const M_t& A,
78  std::vector<double>* residuals) {
79  CHECK_EQ(points1.size(), points2.size());
80 
81  residuals->resize(points1.size());
82 
83  // Note that this code might not be as nice as Eigen expressions,
84  // but it is significantly faster in various tests.
85 
86  const double A_00 = A(0, 0);
87  const double A_01 = A(0, 1);
88  const double A_02 = A(0, 2);
89  const double A_10 = A(1, 0);
90  const double A_11 = A(1, 1);
91  const double A_12 = A(1, 2);
92 
93  for (size_t i = 0; i < points1.size(); ++i) {
94  const double s_0 = points1[i](0);
95  const double s_1 = points1[i](1);
96  const double d_0 = points2[i](0);
97  const double d_1 = points2[i](1);
98 
99  const double pd_0 = A_00 * s_0 + A_01 * s_1 + A_02;
100  const double pd_1 = A_10 * s_0 + A_11 * s_1 + A_12;
101 
102  const double dd_0 = d_0 - pd_0;
103  const double dd_1 = d_1 - pd_1;
104 
105  (*residuals)[i] = dd_0 * dd_0 + dd_1 * dd_1;
106  }
107 }
108 
109 } // namespace colmap
static std::vector< M_t > Estimate(const std::vector< X_t > &points1, const std::vector< Y_t > &points2)
Eigen::Matrix< double, 2, 3 > M_t
static void Residuals(const std::vector< X_t > &points1, const std::vector< Y_t > &points2, const M_t &E, std::vector< double > *residuals)