Blender  V3.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 
24 #include "libmv/numeric/numeric.h"
25 
26 namespace libmv {
27 
28 void P_From_KRt(const Mat3& K, const Mat3& R, const Vec3& t, Mat34* P);
29 void 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 
48 void HomogeneousToEuclidean(const Mat& H, Mat* X);
49 void HomogeneousToEuclidean(const Mat3X& h, Mat2X* e);
50 void HomogeneousToEuclidean(const Mat4X& h, Mat3X* e);
51 void HomogeneousToEuclidean(const Vec3& H, Vec2* X);
52 void HomogeneousToEuclidean(const Vec4& H, Vec3* X);
53 inline Vec2 HomogeneousToEuclidean(const Vec3& h) {
54  return h.head<2>() / h(2);
55 }
56 inline Vec3 HomogeneousToEuclidean(const Vec4& h) {
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 
66 void 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 }
73 inline 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 }
84 inline 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 }
89 void EuclideanToHomogeneous(const Vec2& X, Vec3* H);
90 void 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
98 void EuclideanToNormalizedCamera(const Mat2X& x, const Mat3& K, Mat2X* n);
99 void HomogeneousToNormalizedCamera(const Mat3X& x, const Mat3& K, Mat2X* n);
100 
101 inline 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 
108 inline void Project(const Mat34& P, const Vec4& X, Vec3* x) {
109  *x = P * X;
110 }
111 
112 inline void Project(const Mat34& P, const Vec4& X, Vec2* x) {
113  Vec3 hx = P * X;
114  *x = hx.head<2>() / hx(2);
115 }
116 
117 inline 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 
123 inline 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 
129 inline 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 
137 inline Mat2X Project(const Mat34& P, const Mat4X& X) {
138  Mat2X x;
139  Project(P, X, &x);
140  return x;
141 }
142 
143 inline 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 
153 inline 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 
164 inline Mat2X Project(const Mat34& P, const Mat3X& X) {
165  Mat2X x(2, X.cols());
166  Project(P, X, &x);
167  return x;
168 }
169 
170 inline 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 
176 double Depth(const Mat3& R, const Vec3& t, const Vec3& X);
177 double Depth(const Mat3& R, const Vec3& t, const Vec4& X);
178 
183 inline 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 inline bool isInFrontOfCamera(const Mat34& P, const Vec3& X) {
193  Vec4 X_homo;
194  X_homo.segment<3>(0) = X;
195  X_homo(3) = 1;
196  return isInFrontOfCamera(P, X_homo);
197 }
198 
204  Vec3 x_h = Kinverse * EuclideanToHomogeneous(x);
205  return HomogeneousToEuclidean(x_h);
206 }
207 
209 inline double RootMeanSquareError(const Mat2X& x_image,
210  const Mat4X& X_world,
211  const Mat34& P) {
212  size_t num_points = x_image.cols();
213  Mat2X dx = Project(P, X_world) - x_image;
214  return dx.norm() / num_points;
215 }
216 
218 inline double RootMeanSquareError(const Mat2X& x_image,
219  const Mat3X& X_world,
220  const Mat3& K,
221  const Mat3& R,
222  const Vec3& t) {
223  Mat34 P;
224  P_From_KRt(K, R, t, &P);
225  size_t num_points = x_image.cols();
226  Mat2X dx = Project(P, X_world) - x_image;
227  return dx.norm() / num_points;
228 }
229 } // namespace libmv
230 
231 #endif // LIBMV_MULTIVIEW_PROJECTION_H_
#define K(key)
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble t
#define X
Definition: GeomUtils.cpp:199
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
float[3][3] Mat3
Definition: gpu_matrix.cc:27
static float P(float k)
Definition: math_interp.c:25
#define R
#define H(x, y, z)
static unsigned c
Definition: RandGen.cpp:83
Eigen::Vector4d Vec4
Definition: numeric.h:107
void EuclideanToHomogeneous(const Mat &X, Mat *H)
Definition: projection.cc:200
Eigen::Matrix< double, 3, 3 > Mat3
Definition: numeric.h:72
Eigen::Vector2d Vec2
Definition: numeric.h:105
void HomogeneousToEuclidean(const Mat &H, Mat *X)
Definition: projection.cc:166
void HomogeneousToNormalizedCamera(const Mat3X &x, const Mat3 &K, Mat2X *n)
Definition: projection.cc:224
Eigen::MatrixXd Mat
Definition: numeric.h:60
void ProjectionShiftPrincipalPoint(const Mat34 &P, const Vec2 &principal_point, const Vec2 &principal_point_new, Mat34 *P_new)
Definition: projection.cc:135
double Depth(const Mat3 &R, const Vec3 &t, const Vec3 &X)
Definition: projection.cc:229
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)
Definition: projection.cc:148
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)
Definition: projection.cc:217