ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
homography_matrix.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/Geometry>
35 #include <Eigen/LU>
36 #include <Eigen/SVD>
37 
38 #include "base/projection.h"
39 #include "estimators/utils.h"
40 #include "util/logging.h"
41 
42 namespace colmap {
43 
44 std::vector<HomographyMatrixEstimator::M_t> HomographyMatrixEstimator::Estimate(
45  const std::vector<X_t>& points1, const std::vector<Y_t>& points2) {
46  CHECK_EQ(points1.size(), points2.size());
47 
48  const size_t N = points1.size();
49 
50  // Center and normalize image points for better numerical stability.
51  std::vector<X_t> normed_points1;
52  std::vector<Y_t> normed_points2;
53  Eigen::Matrix3d points1_norm_matrix;
54  Eigen::Matrix3d points2_norm_matrix;
55  CenterAndNormalizeImagePoints(points1, &normed_points1, &points1_norm_matrix);
56  CenterAndNormalizeImagePoints(points2, &normed_points2, &points2_norm_matrix);
57 
58  // Setup constraint matrix.
59  Eigen::Matrix<double, Eigen::Dynamic, 9> A = Eigen::MatrixXd::Zero(2 * N, 9);
60 
61  for (size_t i = 0, j = N; i < points1.size(); ++i, ++j) {
62  const double s_0 = normed_points1[i](0);
63  const double s_1 = normed_points1[i](1);
64  const double d_0 = normed_points2[i](0);
65  const double d_1 = normed_points2[i](1);
66 
67  A(i, 0) = -s_0;
68  A(i, 1) = -s_1;
69  A(i, 2) = -1;
70  A(i, 6) = s_0 * d_0;
71  A(i, 7) = s_1 * d_0;
72  A(i, 8) = d_0;
73 
74  A(j, 3) = -s_0;
75  A(j, 4) = -s_1;
76  A(j, 5) = -1;
77  A(j, 6) = s_0 * d_1;
78  A(j, 7) = s_1 * d_1;
79  A(j, 8) = d_1;
80  }
81 
82  // Solve for the nullspace of the constraint matrix.
83  Eigen::JacobiSVD<Eigen::Matrix<double, Eigen::Dynamic, 9>> svd(
84  A, Eigen::ComputeFullV);
85 
86  const Eigen::VectorXd nullspace = svd.matrixV().col(8);
87  Eigen::Map<const Eigen::Matrix3d> H_t(nullspace.data());
88 
89  const std::vector<M_t> models = {points2_norm_matrix.inverse() *
90  H_t.transpose() * points1_norm_matrix};
91  return models;
92 }
93 
94 void HomographyMatrixEstimator::Residuals(const std::vector<X_t>& points1,
95  const std::vector<Y_t>& points2,
96  const M_t& H,
97  std::vector<double>* residuals) {
98  CHECK_EQ(points1.size(), points2.size());
99 
100  residuals->resize(points1.size());
101 
102  // Note that this code might not be as nice as Eigen expressions,
103  // but it is significantly faster in various tests.
104 
105  const double H_00 = H(0, 0);
106  const double H_01 = H(0, 1);
107  const double H_02 = H(0, 2);
108  const double H_10 = H(1, 0);
109  const double H_11 = H(1, 1);
110  const double H_12 = H(1, 2);
111  const double H_20 = H(2, 0);
112  const double H_21 = H(2, 1);
113  const double H_22 = H(2, 2);
114 
115  for (size_t i = 0; i < points1.size(); ++i) {
116  const double s_0 = points1[i](0);
117  const double s_1 = points1[i](1);
118  const double d_0 = points2[i](0);
119  const double d_1 = points2[i](1);
120 
121  const double pd_0 = H_00 * s_0 + H_01 * s_1 + H_02;
122  const double pd_1 = H_10 * s_0 + H_11 * s_1 + H_12;
123  const double pd_2 = H_20 * s_0 + H_21 * s_1 + H_22;
124 
125  const double inv_pd_2 = 1.0 / pd_2;
126  const double dd_0 = d_0 - pd_0 * inv_pd_2;
127  const double dd_1 = d_1 - pd_1 * inv_pd_2;
128 
129  (*residuals)[i] = dd_0 * dd_0 + dd_1 * dd_1;
130  }
131 }
132 
133 } // namespace colmap
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 &H, std::vector< double > *residuals)
void CenterAndNormalizeImagePoints(const std::vector< Eigen::Vector2d > &points, std::vector< Eigen::Vector2d > *normed_points, Eigen::Matrix3d *matrix)
Definition: utils.cc:38