Berkeley SfM
pose_estimator_2d3d.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015, The Regents of the University of California (Regents).
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following
14  * disclaimer in the documentation and/or other materials provided
15  * with the distribution.
16  *
17  * 3. Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  * Please contact the author(s) of this library if you have any questions.
34  * Authors: Erik Nelson ( eanelson@eecs.berkeley.edu )
35  * David Fridovich-Keil ( dfk@eecs.berkeley.edu )
36  */
37 
38 ///////////////////////////////////////////////////////////////////////////////
39 //
40 // This class implements the Gold Standard Algorithm for computing the pose of
41 // a camera, given 3D points in the world and 2D image-space features
42 // corresponding to those 3D points. The algorithm requires at least 6
43 // non-degenerate (e.g. not colinear) point matches to compute a solution.
44 //
45 // Inputs are:
46 // - A set of 2D image-space features.
47 // - A set of corresponding 3D world-space points (same number and order).
48 // - A set of camera intrinsic parameters.
49 //
50 // Outputs are:
51 // - A boolean for success or failure.
52 // - The pose of the camera.
53 //
54 ///////////////////////////////////////////////////////////////////////////////
55 
56 #ifndef BSFM_GEOMETRY_POSE_ESTIMATOR_2D_3D_H
57 #define BSFM_GEOMETRY_POSE_ESTIMATOR_2D_3D_H
58 
59 #include <Eigen/Core>
60 #include <memory>
61 
62 #include "../camera/camera_intrinsics.h"
63 #include "../geometry/point_3d.h"
64 #include "../matching/feature.h"
65 #include "../pose/pose.h"
66 #include "../util/disallow_copy_and_assign.h"
67 #include "../util/types.h"
68 
69 namespace bsfm {
70 
71 using Eigen::Matrix3d;
72 using Eigen::Matrix4d;
73 using Eigen::MatrixXd;
74 using Eigen::VectorXd;
75 
77  public:
80 
81  // Initialize the solver with a list of 2D <--> 3D point correspondences and a
82  // set of camera intrinsic parameters.
83  bool Initialize(const FeatureList& points_2d, const Point3DList& points_3d,
84  const CameraIntrinsics& intrinsics);
85 
86  // Computes the camera pose, returning success or failure. If a solution was
87  // not found, the input pose remains unchanged.
88  bool Solve(Pose& camera_pose);
89 
90  private:
92 
93  // Computes an initial valid solution for the projection matrix P = K[R|t]
94  // using Eq. 7.2 from H&Z: Multiple-View Geometry.
95  bool ComputeInitialSolution(Matrix34d& initial_solution);
96 
97  // Refines the solution using the Levenberg-Marquardt iterative algorithm for
98  // non-linear least-squares.
99  bool OptimizeSolution(Matrix34d& solution);
100 
101  // Uses the camera intrinsics (stored locally) to extract the camera pose from
102  // the projection matrix P. This amounts to solving [R|t] = K^{-1} * P.
103  // Returns false if for some reason the computed pose has a rotation matrix
104  // with a determinant of 0, which is not a valid rotation matrix.
105  bool ExtractPose(const Matrix34d& P, Pose& pose);
106 
107  // Normalized versions of the inputs to the algorithm.
110 
111  // Input camera intrinsics matrix.
113 
114  // Normalization matrices for points_2d_ and points_3d_.
115  Matrix3d T_;
116  Matrix4d U_;
117 
118 }; //\class PoseEstimator2D3D
119 
120 } //\namespace bsfm
121 
122 #endif
std::vector< Point3D > Point3DList
Definition: point_3d.h:100
bool ExtractPose(const Matrix34d &P, Pose &pose)
bool OptimizeSolution(Matrix34d &solution)
bool Initialize(const FeatureList &points_2d, const Point3DList &points_3d, const CameraIntrinsics &intrinsics)
bool Solve(Pose &camera_pose)
std::vector< Feature > FeatureList
Definition: feature.h:65
Definition: camera.cpp:50
::Eigen::Matrix< double, 3, 4 > Matrix34d
Definition: types.h:87
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
bool ComputeInitialSolution(Matrix34d &initial_solution)