Blender V4.3
libmv/libmv/multiview/projection.h
Go to the documentation of this file.
1// Copyright (c) 2007, 2008 libmv authors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to
5// deal in the Software without restriction, including without limitation the
6// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7// sell copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19// IN THE SOFTWARE.
20
21#ifndef LIBMV_MULTIVIEW_PROJECTION_H_
22#define LIBMV_MULTIVIEW_PROJECTION_H_
23
25
26namespace libmv {
27
28void P_From_KRt(const Mat3& K, const Mat3& R, const Vec3& t, Mat34* P);
29void KRt_From_P(const Mat34& P, Mat3* K, Mat3* R, Vec3* t);
30
31// Applies a change of basis to the image coordinates of the projection matrix
32// so that the principal point becomes principal_point_new.
34 const Vec2& principal_point,
35 const Vec2& principal_point_new,
36 Mat34* P_new);
37
38// Applies a change of basis to the image coordinates of the projection matrix
39// so that the aspect ratio becomes aspect_ratio_new. This is done by
40// stretching the y axis. The aspect ratio is defined as the quotient between
41// the focal length of the y and the x axis.
43 const Vec2& principal_point,
44 double aspect_ratio,
45 double aspect_ratio_new,
46 Mat34* P_new);
47
48void HomogeneousToEuclidean(const Mat& H, Mat* X);
49void HomogeneousToEuclidean(const Mat3X& h, Mat2X* e);
50void HomogeneousToEuclidean(const Mat4X& h, Mat3X* e);
51void HomogeneousToEuclidean(const Vec3& H, Vec2* X);
52void HomogeneousToEuclidean(const Vec4& H, Vec3* X);
54 return h.head<2>() / h(2);
55}
57 return h.head<3>() / h(3);
58}
60 Mat2X e(2, h.cols());
61 e.row(0) = h.row(0).array() / h.row(2).array();
62 e.row(1) = h.row(1).array() / h.row(2).array();
63 return e;
64}
65
66void EuclideanToHomogeneous(const Mat& X, Mat* H);
68 Mat3X h(3, x.cols());
69 h.block(0, 0, 2, x.cols()) = x;
70 h.row(2).setOnes();
71 return h;
72}
73inline void EuclideanToHomogeneous(const Mat2X& x, Mat3X* h) {
74 h->resize(3, x.cols());
75 h->block(0, 0, 2, x.cols()) = x;
76 h->row(2).setOnes();
77}
79 Mat4X h(4, x.cols());
80 h.block(0, 0, 3, x.cols()) = x;
81 h.row(3).setOnes();
82 return h;
83}
84inline void EuclideanToHomogeneous(const Mat3X& x, Mat4X* h) {
85 h->resize(4, x.cols());
86 h->block(0, 0, 3, x.cols()) = x;
87 h->row(3).setOnes();
88}
89void EuclideanToHomogeneous(const Vec2& X, Vec3* H);
90void EuclideanToHomogeneous(const Vec3& X, Vec4* H);
92 return Vec3(x(0), x(1), 1);
93}
95 return Vec4(x(0), x(1), x(2), 1);
96}
97// Conversion from image coordinates to normalized camera coordinates
98void EuclideanToNormalizedCamera(const Mat2X& x, const Mat3& K, Mat2X* n);
99void HomogeneousToNormalizedCamera(const Mat3X& x, const Mat3& K, Mat2X* n);
100
101inline Vec2 Project(const Mat34& P, const Vec3& X) {
102 Vec4 HX;
103 HX << X, 1.0;
104 Vec3 hx = P * HX;
105 return hx.head<2>() / hx(2);
106}
107
108inline void Project(const Mat34& P, const Vec4& X, Vec3* x) {
109 *x = P * X;
110}
111
112inline void Project(const Mat34& P, const Vec4& X, Vec2* x) {
113 Vec3 hx = P * X;
114 *x = hx.head<2>() / hx(2);
115}
116
117inline void Project(const Mat34& P, const Vec3& X, Vec3* x) {
118 Vec4 HX;
119 HX << X, 1.0;
120 Project(P, HX, x);
121}
122
123inline void Project(const Mat34& P, const Vec3& X, Vec2* x) {
124 Vec3 hx;
125 Project(P, X, &hx);
126 *x = hx.head<2>() / hx(2);
127}
128
129inline void Project(const Mat34& P, const Mat4X& X, Mat2X* x) {
130 x->resize(2, X.cols());
131 for (int c = 0; c < X.cols(); ++c) {
132 Vec3 hx = P * X.col(c);
133 x->col(c) = hx.head<2>() / hx(2);
134 }
135}
136
137inline Mat2X Project(const Mat34& P, const Mat4X& X) {
138 Mat2X x;
139 Project(P, X, &x);
140 return x;
141}
142
143inline void Project(const Mat34& P, const Mat3X& X, Mat2X* x) {
144 x->resize(2, X.cols());
145 for (int c = 0; c < X.cols(); ++c) {
146 Vec4 HX;
147 HX << X.col(c), 1.0;
148 Vec3 hx = P * HX;
149 x->col(c) = hx.head<2>() / hx(2);
150 }
151}
152
153inline void Project(const Mat34& P, const Mat3X& X, const Vecu& ids, Mat2X* x) {
154 x->resize(2, ids.size());
155 Vec4 HX;
156 Vec3 hx;
157 for (int c = 0; c < ids.size(); ++c) {
158 HX << X.col(ids[c]), 1.0;
159 hx = P * HX;
160 x->col(c) = hx.head<2>() / hx(2);
161 }
162}
163
164inline Mat2X Project(const Mat34& P, const Mat3X& X) {
165 Mat2X x(2, X.cols());
166 Project(P, X, &x);
167 return x;
168}
169
170inline Mat2X Project(const Mat34& P, const Mat3X& X, const Vecu& ids) {
171 Mat2X x(2, ids.size());
172 Project(P, X, ids, &x);
173 return x;
174}
175
176double Depth(const Mat3& R, const Vec3& t, const Vec3& X);
177double Depth(const Mat3& R, const Vec3& t, const Vec4& X);
178
183inline bool isInFrontOfCamera(const Mat34& P, const Vec4& X) {
184 double condition_1 = P.row(2).dot(X) * X[3];
185 double condition_2 = X[2] * X[3];
186 if (condition_1 > 0 && condition_2 > 0) {
187 return true;
188 } else {
189 return false;
190 }
191}
192
193inline bool isInFrontOfCamera(const Mat34& P, const Vec3& X) {
194 Vec4 X_homo;
195 X_homo.segment<3>(0) = X;
196 X_homo(3) = 1;
197 return isInFrontOfCamera(P, X_homo);
198}
199
205 Vec3 x_h = Kinverse * EuclideanToHomogeneous(x);
206 return HomogeneousToEuclidean(x_h);
207}
208
210inline double RootMeanSquareError(const Mat2X& x_image,
211 const Mat4X& X_world,
212 const Mat34& P) {
213 size_t num_points = x_image.cols();
214 Mat2X dx = Project(P, X_world) - x_image;
215 return dx.norm() / num_points;
216}
217
219inline double RootMeanSquareError(const Mat2X& x_image,
220 const Mat3X& X_world,
221 const Mat3& K,
222 const Mat3& R,
223 const Vec3& t) {
224 Mat34 P;
225 P_From_KRt(K, R, t, &P);
226 size_t num_points = x_image.cols();
227 Mat2X dx = Project(P, X_world) - x_image;
228 return dx.norm() / num_points;
229}
230} // namespace libmv
231
232#endif // LIBMV_MULTIVIEW_PROJECTION_H_
#define K(key)
#define X
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
float[3][3] Mat3
Definition gpu_matrix.cc:28
#define R
#define H(x, y, z)
Eigen::Vector4d Vec4
Definition numeric.h:107
void EuclideanToHomogeneous(const Mat &X, Mat *H)
Eigen::Matrix< double, 3, 3 > Mat3
Definition numeric.h:72
Eigen::Vector2d Vec2
Definition numeric.h:105
void HomogeneousToEuclidean(const Mat &H, Mat *X)
void HomogeneousToNormalizedCamera(const Mat3X &x, const Mat3 &K, Mat2X *n)
Eigen::MatrixXd Mat
Definition numeric.h:60
void ProjectionShiftPrincipalPoint(const Mat34 &P, const Vec2 &principal_point, const Vec2 &principal_point_new, Mat34 *P_new)
double Depth(const Mat3 &R, const Vec3 &t, const Vec3 &X)
double RootMeanSquareError(const Mat2X &x_image, const Mat4X &X_world, const Mat34 &P)
Estimates the root mean square error (2D)
Vec2 Project(const Mat34 &P, const Vec3 &X)
void KRt_From_P(const Mat34 &P, Mat3 *Kp, Mat3 *Rp, Vec3 *tp)
Definition projection.cc:32
Eigen::Matrix< double, 3, 4 > Mat34
Definition numeric.h:73
Eigen::Matrix< unsigned int, Eigen::Dynamic, 1 > Vecu
Definition numeric.h:67
Eigen::Vector3d Vec3
Definition numeric.h:106
Eigen::Matrix< double, 4, Eigen::Dynamic > Mat4X
Definition numeric.h:93
Eigen::Matrix< double, 3, Eigen::Dynamic > Mat3X
Definition numeric.h:92
void ProjectionChangeAspectRatio(const Mat34 &P, const Vec2 &principal_point, double aspect_ratio, double aspect_ratio_new, Mat34 *P_new)
Vec2 ImageToNormImageCoordinates(Mat3 &Kinverse, Vec2 &x)
Eigen::Matrix< double, 2, Eigen::Dynamic > Mat2X
Definition numeric.h:91
void P_From_KRt(const Mat3 &K, const Mat3 &R, const Vec3 &t, Mat34 *P)
Definition projection.cc:26
bool isInFrontOfCamera(const Mat34 &P, const Vec4 &X)
void EuclideanToNormalizedCamera(const Mat2X &x, const Mat3 &K, Mat2X *n)