ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
pose.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <ceres/ceres.h>
11 
12 #include <Eigen/Core>
13 #include <vector>
14 
15 #include "base/camera.h"
16 #include "base/camera_models.h"
17 #include "optim/loransac.h"
18 #include "util/alignment.h"
19 #include "util/logging.h"
20 #include "util/threading.h"
21 #include "util/types.h"
22 
23 namespace colmap {
24 
26  // Whether to estimate the focal length.
27  bool estimate_focal_length = false;
28 
29  // Number of discrete samples for focal length estimation.
31 
32  // Minimum focal length ratio for discrete focal length sampling
33  // around focal length of given camera.
34  double min_focal_length_ratio = 0.2;
35 
36  // Maximum focal length ratio for discrete focal length sampling
37  // around focal length of given camera.
39 
40  // Number of threads for parallel estimation of focal length.
42 
43  // Options used for P3P RANSAC.
45 
46  void Check() const {
47  CHECK_GT(num_focal_length_samples, 0);
48  CHECK_GT(min_focal_length_ratio, 0);
49  CHECK_GT(max_focal_length_ratio, 0);
52  }
53 };
54 
56  // Convergence criterion.
57  double gradient_tolerance = 1.0;
58 
59  // Maximum number of solver iterations.
60  int max_num_iterations = 100;
61 
62  // Scaling factor determines at which residual robustification takes place.
63  double loss_function_scale = 1.0;
64 
65  // Whether to refine the focal length parameter group.
66  bool refine_focal_length = true;
67 
68  // Whether to refine the extra parameter group.
69  bool refine_extra_params = true;
70 
71  // Whether to print final summary.
72  bool print_summary = true;
73 
74  void Check() const {
75  CHECK_GE(gradient_tolerance, 0.0);
76  CHECK_GE(max_num_iterations, 0);
77  CHECK_GE(loss_function_scale, 0.0);
78  }
79 };
80 
81 // Estimate absolute pose (optionally focal length) from 2D-3D correspondences.
82 //
83 // Focal length estimation is performed using discrete sampling around the
84 // focal length of the given camera. The focal length that results in the
85 // maximal number of inliers is assigned to the given camera.
86 //
87 // @param options Absolute pose estimation options.
88 // @param points2D Corresponding 2D points.
89 // @param points3D Corresponding 3D points.
90 // @param qvec Estimated rotation component as
91 // unit Quaternion coefficients (w, x, y, z).
92 // @param tvec Estimated translation component.
93 // @param camera Camera for which to estimate pose. Modified
94 // in-place to store the estimated focal length.
95 // @param num_inliers Number of inliers in RANSAC.
96 // @param inlier_mask Inlier mask for 2D-3D correspondences.
97 //
98 // @return Whether pose is estimated successfully.
99 bool EstimateAbsolutePose(const AbsolutePoseEstimationOptions& options,
100  const std::vector<Eigen::Vector2d>& points2D,
101  const std::vector<Eigen::Vector3d>& points3D,
102  Eigen::Vector4d* qvec,
103  Eigen::Vector3d* tvec,
104  Camera* camera,
105  size_t* num_inliers,
106  std::vector<char>* inlier_mask);
107 
108 // Estimate relative from 2D-2D correspondences.
109 //
110 // Pose of first camera is assumed to be at the origin without rotation. Pose
111 // of second camera is given as world-to-image transformation,
112 // i.e. `x2 = [R | t] * X2`.
113 //
114 // @param ransac_options RANSAC options.
115 // @param points1 Corresponding 2D points.
116 // @param points2 Corresponding 2D points.
117 // @param qvec Estimated rotation component as
118 // unit Quaternion coefficients (w, x, y, z).
119 // @param tvec Estimated translation component.
120 //
121 // @return Number of RANSAC inliers.
122 size_t EstimateRelativePose(const RANSACOptions& ransac_options,
123  const std::vector<Eigen::Vector2d>& points1,
124  const std::vector<Eigen::Vector2d>& points2,
125  Eigen::Vector4d* qvec,
126  Eigen::Vector3d* tvec);
127 
128 // Refine absolute pose (optionally focal length) from 2D-3D correspondences.
129 //
130 // @param options Refinement options.
131 // @param inlier_mask Inlier mask for 2D-3D correspondences.
132 // @param points2D Corresponding 2D points.
133 // @param points3D Corresponding 3D points.
134 // @param qvec Estimated rotation component as
135 // unit Quaternion coefficients (w, x, y, z).
136 // @param tvec Estimated translation component.
137 // @param camera Camera for which to estimate pose. Modified
138 // in-place to store the estimated focal length.
139 //
140 // @return Whether the solution is usable.
141 bool RefineAbsolutePose(const AbsolutePoseRefinementOptions& options,
142  const std::vector<char>& inlier_mask,
143  const std::vector<Eigen::Vector2d>& points2D,
144  const std::vector<Eigen::Vector3d>& points3D,
145  Eigen::Vector4d* qvec,
146  Eigen::Vector3d* tvec,
147  Camera* camera);
148 
149 // Refine relative pose of two cameras.
150 //
151 // Minimizes the Sampson error between corresponding normalized points using
152 // a robust cost function, i.e. the corresponding points need not necessarily
153 // be inliers given a sufficient initial guess for the relative pose.
154 //
155 // Assumes that first camera pose has projection matrix P = [I | 0], and
156 // pose of second camera is given as transformation from world to camera system.
157 //
158 // Assumes that the given translation vector is normalized, and refines
159 // the translation up to an unknown scale (i.e. refined translation vector
160 // is a unit vector again).
161 //
162 // @param options Solver options.
163 // @param points1 First set of corresponding points.
164 // @param points2 Second set of corresponding points.
165 // @param qvec Unit Quaternion rotation coefficients (w, x, y, z).
166 // @param tvec 3x1 translation vector.
167 //
168 // @return Flag indicating if solution is usable.
169 bool RefineRelativePose(const ceres::Solver::Options& options,
170  const std::vector<Eigen::Vector2d>& points1,
171  const std::vector<Eigen::Vector2d>& points2,
172  Eigen::Vector4d* qvec,
173  Eigen::Vector3d* tvec);
174 
175 } // namespace colmap
static const int kMaxNumThreads
Definition: threading.h:173
size_t EstimateRelativePose(const RANSACOptions &ransac_options, const std::vector< Eigen::Vector2d > &points1, const std::vector< Eigen::Vector2d > &points2, Eigen::Vector4d *qvec, Eigen::Vector3d *tvec)
Definition: pose.cc:164
bool EstimateAbsolutePose(const AbsolutePoseEstimationOptions &options, const std::vector< Eigen::Vector2d > &points2D, const std::vector< Eigen::Vector3d > &points3D, Eigen::Vector4d *qvec, Eigen::Vector3d *tvec, Camera *camera, size_t *num_inliers, std::vector< char > *inlier_mask)
Definition: pose.cc:79
bool RefineRelativePose(const ceres::Solver::Options &options, const std::vector< Eigen::Vector2d > &points1, const std::vector< Eigen::Vector2d > &points2, Eigen::Vector4d *qvec, Eigen::Vector3d *tvec)
Definition: pose.cc:330
bool RefineAbsolutePose(const AbsolutePoseRefinementOptions &options, const std::vector< char > &inlier_mask, const std::vector< Eigen::Vector2d > &points2D, const std::vector< Eigen::Vector3d > &points3D, Eigen::Vector4d *qvec, Eigen::Vector3d *tvec, Camera *camera)
Definition: pose.cc:203
void Check() const
Definition: ransac.h:43