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
Matrix< double, 3, 4 > Matrix3x4d
Definition: types.h:39
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
bool HasPointPositiveDepth(const Eigen::Matrix3x4d &proj_matrix, const Eigen::Vector3d &point3D)
Definition: projection.cc:191
Eigen::Matrix3x4d InvertProjectionMatrix(const Eigen::Matrix3x4d &proj_matrix)
Definition: projection.cc:55
double CalculateDepth(const Eigen::Matrix3x4d &proj_matrix, const Eigen::Vector3d &point3D)
Definition: projection.cc:185
bool DecomposeProjectionMatrix(const Eigen::Matrix3x4d &P, Eigen::Matrix3d *K, Eigen::Matrix3d *R, Eigen::Vector3d *T)
Definition: projection.cc:72
Eigen::Matrix3d ComputeClosestRotationMatrix(const Eigen::Matrix3d &matrix)
Definition: projection.cc:62
double CalculateNormalizedAngularError(const Eigen::Vector2d &point2D, const Eigen::Vector3d &point3D, const Eigen::Vector4d &qvec, const Eigen::Vector3d &tvec)
Definition: projection.cc:168
Eigen::Matrix3x4d ComposeProjectionMatrix(const Eigen::Vector4d &qvec, const Eigen::Vector3d &tvec)
Definition: projection.cc:39
Eigen::Vector2d ProjectPointToImage(const Eigen::Vector3d &point3D, const Eigen::Matrix3x4d &proj_matrix, const Camera &camera)
Definition: projection.cc:104
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