ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
projection.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 <Eigen/Core>
11 #include <Eigen/Geometry>
12 #include <limits>
13 #include <vector>
14 
15 #include "base/camera.h"
16 
17 namespace colmap {
18 
19 // Compose projection matrix from rotation and translation components.
20 //
21 // The projection matrix transforms 3D world to image points.
22 //
23 // @param qvec Unit Quaternion rotation coefficients (w, x, y, z).
24 // @param tvec 3x1 translation vector.
25 //
26 // @return 3x4 projection matrix.
27 Eigen::Matrix3x4d ComposeProjectionMatrix(const Eigen::Vector4d& qvec,
28  const Eigen::Vector3d& tvec);
29 
30 // Compose projection matrix from rotation matrix and translation components).
31 //
32 // The projection matrix transforms 3D world to image points.
33 //
34 // @param R 3x3 rotation matrix.
35 // @param t 3x1 translation vector.
36 //
37 // @return 3x4 projection matrix.
38 Eigen::Matrix3x4d ComposeProjectionMatrix(const Eigen::Matrix3d& R,
39  const Eigen::Vector3d& T);
40 
41 // Invert projection matrix, defined as:
42 //
43 // P = [R | t] with R \in SO(3) and t \in R^3
44 //
45 // and the inverse projection matrix as:
46 //
47 // P' = [R^T | -R^T t]
48 //
49 // @param proj_matrix 3x4 projection matrix.
50 //
51 // @return 3x4 inverse projection matrix.
53 
54 // Compute the closes rotation matrix with the closest Frobenius norm by setting
55 // the singular values of the given matrix to 1.
56 Eigen::Matrix3d ComputeClosestRotationMatrix(const Eigen::Matrix3d& matrix);
57 
58 // Decompose projection matrix into intrinsic camera matrix, rotation matrix and
59 // translation vector. Returns false if decomposition fails. This implementation
60 // is inspired by the OpenCV implementation with some additional checks.
61 bool DecomposeProjectionMatrix(const Eigen::Matrix3x4d& proj_matrix,
62  Eigen::Matrix3d* K,
63  Eigen::Matrix3d* R,
64  Eigen::Vector3d* T);
65 
66 // Project 3D point to image.
67 //
68 // @param points3D 3D world point as 3x1 vector.
69 // @param proj_matrix 3x4 projection matrix.
70 // @param camera Camera used to project to image plane.
71 //
72 // @return Projected image point.
73 Eigen::Vector2d ProjectPointToImage(const Eigen::Vector3d& point3D,
74  const Eigen::Matrix3x4d& proj_matrix,
75  const Camera& camera);
76 
77 // Calculate the reprojection error.
78 //
79 // The reprojection error is the Euclidean distance between the observation
80 // in the image and the projection of the 3D point into the image. If the
81 // 3D point is behind the camera, then this function returns DBL_MAX.
82 double CalculateSquaredReprojectionError(const Eigen::Vector2d& point2D,
83  const Eigen::Vector3d& point3D,
84  const Eigen::Vector4d& qvec,
85  const Eigen::Vector3d& tvec,
86  const Camera& camera);
87 double CalculateSquaredReprojectionError(const Eigen::Vector2d& point2D,
88  const Eigen::Vector3d& point3D,
89  const Eigen::Matrix3x4d& proj_matrix,
90  const Camera& camera);
91 
92 // Calculate the angular error.
93 //
94 // The angular error is the angle between the observed viewing ray and the
95 // actual viewing ray from the camera center to the 3D point.
96 double CalculateAngularError(const Eigen::Vector2d& point2D,
97  const Eigen::Vector3d& point3D,
98  const Eigen::Vector4d& qvec,
99  const Eigen::Vector3d& tvec,
100  const Camera& camera);
101 double CalculateAngularError(const Eigen::Vector2d& point2D,
102  const Eigen::Vector3d& point3D,
103  const Eigen::Matrix3x4d& proj_matrix,
104  const Camera& camera);
105 
106 // Calculate angulate error using normalized image points.
107 //
108 // The angular error is the angle between the observed viewing ray and the
109 // actual viewing ray from the camera center to the 3D point.
110 double CalculateNormalizedAngularError(const Eigen::Vector2d& point2D,
111  const Eigen::Vector3d& point3D,
112  const Eigen::Vector4d& qvec,
113  const Eigen::Vector3d& tvec);
114 double CalculateNormalizedAngularError(const Eigen::Vector2d& point2D,
115  const Eigen::Vector3d& point3D,
116  const Eigen::Matrix3x4d& proj_matrix);
117 
118 // Calculate depth of 3D point with respect to camera.
119 //
120 // The depth is defined as the Euclidean distance of a 3D point from the
121 // camera and is positive if the 3D point is in front and negative if
122 // behind of the camera.
123 //
124 // @param proj_matrix 3x4 projection matrix.
125 // @param point3D 3D point as 3x1 vector.
126 //
127 // @return Depth of 3D point.
128 double CalculateDepth(const Eigen::Matrix3x4d& proj_matrix,
129  const Eigen::Vector3d& point3D);
130 
131 // Check if 3D point passes cheirality constraint,
132 // i.e. it lies in front of the camera and not in the image plane.
133 //
134 // @param proj_matrix 3x4 projection matrix.
135 // @param point3D 3D point as 3x1 vector.
136 //
137 // @return True if point lies in front of camera.
138 bool HasPointPositiveDepth(const Eigen::Matrix3x4d& proj_matrix,
139  const Eigen::Vector3d& point3D);
140 
141 } // namespace colmap
colmap::ProjectionCenterFromMatrix
Eigen::Vector3d ProjectionCenterFromMatrix(const Eigen::Matrix3x4d &proj_matrix)
Definition: pose.cc:159
M_PI
constexpr double M_PI
Pi.
Definition: CVConst.h:19
colmap::RotationMatrixToEulerAngles
void RotationMatrixToEulerAngles(const Eigen::Matrix3d &R, double *rx, double *ry, double *rz)
Definition: pose.cc:48
Eigen::Matrix3x4d
Matrix< double, 3, 4 > Matrix3x4d
Definition: types.h:39
colmap::InvertPose
void InvertPose(const Eigen::Vector4d &qvec, const Eigen::Vector3d &tvec, Eigen::Vector4d *inv_qvec, Eigen::Vector3d *inv_tvec)
Definition: pose.cc:192
colmap::ComposeProjectionMatrix
Eigen::Matrix3x4d ComposeProjectionMatrix(const Eigen::Vector4d &qvec, const Eigen::Vector3d &tvec)
Definition: projection.cc:39
colmap::Camera::WorldToImage
Eigen::Vector2d WorldToImage(const Eigen::Vector2d &world_point) const
Definition: camera.cc:230
colmap::CalculateAngularError
double CalculateAngularError(const Eigen::Vector2d &point2D, const Eigen::Vector3d &point3D, const Eigen::Vector4d &qvec, const Eigen::Vector3d &tvec, const Camera &camera)
Definition: projection.cc:151
colmap::ComposeIdentityQuaternion
Eigen::Vector4d ComposeIdentityQuaternion()
Definition: pose.h:209
colmap::InterpolatePose
void InterpolatePose(const Eigen::Vector4d &qvec1, const Eigen::Vector3d &tvec1, const Eigen::Vector4d &qvec2, const Eigen::Vector3d &tvec2, const double t, Eigen::Vector4d *qveci, Eigen::Vector3d *tveci)
Definition: pose.cc:198
colmap::ProjectionCenterFromPose
Eigen::Vector3d ProjectionCenterFromPose(const Eigen::Vector4d &qvec, const Eigen::Vector3d &tvec)
Definition: pose.cc:164
colmap::InvertQuaternion
Eigen::Vector4d InvertQuaternion(const Eigen::Vector4d &qvec)
Definition: pose.cc:93
testing.h
colmap::CalculateNormalizedAngularError
double CalculateNormalizedAngularError(const Eigen::Vector2d &point2D, const Eigen::Vector3d &point3D, const Eigen::Vector4d &qvec, const Eigen::Vector3d &tvec)
Definition: projection.cc:168
colmap::EulerAnglesToRotationMatrix
Eigen::Matrix3d EulerAnglesToRotationMatrix(const double rx, const double ry, const double rz)
Definition: pose.cc:59
colmap::DecomposeProjectionMatrix
bool DecomposeProjectionMatrix(const Eigen::Matrix3x4d &P, Eigen::Matrix3d *K, Eigen::Matrix3d *R, Eigen::Vector3d *T)
Definition: projection.cc:72
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(TestCrossProductMatrix)
Definition: pose_test.cc:43
colmap::ConcatenateQuaternions
Eigen::Vector4d ConcatenateQuaternions(const Eigen::Vector4d &qvec1, const Eigen::Vector4d &qvec2)
Definition: pose.cc:97
colmap::Camera::ImageToWorld
Eigen::Vector2d ImageToWorld(const Eigen::Vector2d &image_point) const
Definition: camera.cc:219
projection.h
math.h
colmap::InvertProjectionMatrix
Eigen::Matrix3x4d InvertProjectionMatrix(const Eigen::Matrix3x4d &proj_matrix)
Definition: projection.cc:55
matrix.h
colmap::CrossProductMatrix
Eigen::Matrix3d CrossProductMatrix(const Eigen::Vector3d &vector)
Definition: pose.cc:41
colmap::CalculateSquaredReprojectionError
double CalculateSquaredReprojectionError(const Eigen::Vector2d &point2D, const Eigen::Vector3d &point3D, const Eigen::Vector4d &qvec, const Eigen::Vector3d &tvec, const Camera &camera)
Definition: projection.cc:111
colmap::HasPointPositiveDepth
bool HasPointPositiveDepth(const Eigen::Matrix3x4d &proj_matrix, const Eigen::Vector3d &point3D)
Definition: projection.cc:191
e
const double * e
Definition: essential_matrix_poly.h:10
colmap::CheckCheirality
bool CheckCheirality(const Eigen::Matrix3d &R, const Eigen::Vector3d &t, const std::vector< Eigen::Vector2d > &points1, const std::vector< Eigen::Vector2d > &points2, std::vector< Eigen::Vector3d > *points3D)
Definition: pose.cc:225
colmap::AverageQuaternions
Eigen::Vector4d AverageQuaternions(const std::vector< Eigen::Vector4d > &qvecs, const std::vector< double > &weights)
Definition: pose.cc:118
colmap::ConcatenatePoses
void ConcatenatePoses(const Eigen::Vector4d &qvec1, const Eigen::Vector3d &tvec1, const Eigen::Vector4d &qvec2, const Eigen::Vector3d &tvec2, Eigen::Vector4d *qvec12, Eigen::Vector3d *tvec12)
Definition: pose.cc:183
colmap::DecomposeMatrixRQ
void DecomposeMatrixRQ(const MatrixType &A, MatrixType *R, MatrixType *Q)
Definition: matrix.h:45
colmap::ComputeClosestRotationMatrix
Eigen::Matrix3d ComputeClosestRotationMatrix(const Eigen::Matrix3d &matrix)
Definition: projection.cc:62
colmap::ComputeRelativePose
void ComputeRelativePose(const Eigen::Vector4d &qvec1, const Eigen::Vector3d &tvec1, const Eigen::Vector4d &qvec2, const Eigen::Vector3d &tvec2, Eigen::Vector4d *qvec12, Eigen::Vector3d *tvec12)
Definition: pose.cc:173
camera.h
colmap::RotationMatrixToQuaternion
Eigen::Vector4d RotationMatrixToQuaternion(const Eigen::Matrix3d &rot_mat)
Definition: pose.cc:70
colmap::QuaternionToRotationMatrix
Eigen::Matrix3d QuaternionToRotationMatrix(const Eigen::Vector4d &qvec)
Definition: pose.cc:75
pose.h
colmap
Definition: AutomaticReconstructionController.h:17
colmap::CalculateDepth
double CalculateDepth(const Eigen::Matrix3x4d &proj_matrix, const Eigen::Vector3d &point3D)
Definition: projection.cc:185
colmap::CalculateBaseline
Eigen::Vector3d CalculateBaseline(const Eigen::Vector4d &qvec1, const Eigen::Vector3d &tvec1, const Eigen::Vector4d &qvec2, const Eigen::Vector3d &tvec2)
Definition: pose.cc:216
colmap::QuaternionRotatePoint
Eigen::Vector3d QuaternionRotatePoint(const Eigen::Vector4d &qvec, const Eigen::Vector3d &point)
Definition: pose.cc:110
colmap::ProjectPointToImage
Eigen::Vector2d ProjectPointToImage(const Eigen::Vector3d &point3D, const Eigen::Matrix3x4d &proj_matrix, const Camera &camera)
Definition: projection.cc:104
colmap::RotationFromUnitVectors
Eigen::Matrix3d RotationFromUnitVectors(const Eigen::Vector3d &vector1, const Eigen::Vector3d &vector2)
Definition: pose.cc:145
colmap::NormalizeQuaternion
Eigen::Vector4d NormalizeQuaternion(const Eigen::Vector4d &qvec)
Definition: pose.cc:82
colmap::Camera
Definition: camera.h:20