Main MRPT website > C++ reference for MRPT 1.9.9
CCamModel.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "vision-precomp.h" // Precompiled headers
11 #include <mrpt/vision/CCamModel.h>
12 #include <mrpt/vision/pinhole.h>
14 #include <mrpt/utils/types_math.h>
15 
16 using namespace mrpt;
17 using namespace mrpt::vision;
18 using namespace mrpt::poses;
19 using namespace mrpt::math;
20 using namespace mrpt::utils;
21 
22 /** Constructor */
24 /**********************************************************************************************************************/
27 {
28  // JL: CHECK!!!!
29  const double Cx = cam.cx();
30  const double Cy = cam.cy();
31  const double k1 = cam.k1();
32  const double k2 = cam.k2();
33  const double dx =
34  1.0 / cam.fx(); // JL: Check if formulas hold with this change!!!
35  const double dy = 1.0 / cam.fy();
36 
37  double xd = (p.x - Cx) * dx;
38  double yd = (p.y - Cy) * dy;
39  double rd2 = xd * xd + yd * yd;
40  double rd4 = rd2 * rd2;
41 
42  J_undist.setSize(2, 2);
43  J_undist(0, 0) =
44  (1 + k1 * rd2 + k2 * rd4) +
45  (p.x - Cx) * (k1 + 2 * k2 * rd2) * (2 * (p.x - Cx) * dx * dx);
46  J_undist(1, 1) =
47  (1 + k1 * rd2 + k2 * rd4) +
48  (p.y - Cy) * (k1 + 2 * k2 * rd2) * (2 * (p.y - Cy) * dy * dy);
49  J_undist(0, 1) =
50  (p.x - Cx) * (k1 + 2 * k2 * rd2) * (2 * (p.y - Cy) * dy * dy);
51  J_undist(1, 0) =
52  (p.y - Cy) * (k1 + 2 * k2 * rd2) * (2 * (p.x - Cx) * dx * dx);
53 }
54 /******************************************************************************************************************************/
55 
58 {
59  // J_undistor.setSize(2,2);
60  const double dx = p.x - cam.cx();
61  const double dy = p.y - cam.cy();
62  const double f = 1 - 2 * cam.k1() * square(dx) * square(dy);
63  const double inv_f_15 = 1.0 / pow(f, 1.5);
64 
65  //// dy/du
66  // CMatrixDouble dy_du(2,2); // Default is all zeroes
67  // dy_du(0,0) = 1.0/cam.fx;
68  // dy_du(1,1) = 1.0/cam.fy;
69 
70  // du/dh
71  // CMatrixDouble du_dh(2,2);
72 
73  J_undistor(0, 0) = (1 - 2 * cam.k1() * square(dy)) * inv_f_15;
74  J_undistor(0, 1) = J_undistor(1, 0) = (2 * cam.k1() * dx * dy) * inv_f_15;
75  J_undistor(1, 1) = (1 - 2 * cam.k1() * square(dx)) * inv_f_15;
76 
77  //// JL: TODO: CHECK!
78  // const double Cx = cam.cx;
79  // const double Cy = cam.cy;
80  // const double k1 = cam.k1();
81  // const double k2 = cam.k2();
82  // const double dx = 1.0 / cam.fx; // JL: Check if formulas hold with this
83  // change!!!
84  // const double dy = 1.0 / cam.fy;
85 
86  // double xd = (p.x-Cx)*dx;
87  // double yd = (p.y-Cy)*dy;
88 
89  // double rd2 = xd*xd + yd*yd;
90  // double rd4 = rd2 * rd2;
91 
92  // J_undistor.setSize(2,2);
93 
94  // J_undistor(0,0) = (1+k1*rd2+k2*rd4) + (p.x-Cx) * (k1+2*k2*rd2) *
95  // (2*(p.x-Cx)*dx*dx);
96  // J_undistor(1,1) = (1+k1*rd2+k2*rd4) + (p.y-Cy) * (k1+2*k2*rd2) *
97  // (2*(p.y-Cy)*dy*dy);
98  // J_undistor(0,1) = (p.x-Cx) * (k1+2*k2*rd2) * (2*(p.y-Cy)*dy*dy);
99  // J_undistor(1,0) = (p.y-Cy) * (k1+2*k2*rd2) * (2*(p.x-Cx)*dx*dx);
100 }
101 /**********************************************************************************************************************/
102 
105 {
106  // JLBC: Added from Davison's SceneLib:
107  //
108  // 1 distortion coefficient model
109  //
110  const double dx = (p.x - cam.cx()); // / cam.fx; //JL: commented out
111  // cam.fxy... (dx,dy) units must be
112  // pixels
113  const double dy = (p.y - cam.cy()); // / cam.fy;
114 
115  const double r2 = square(dx) + square(dy);
116 
117  const double fact = 1.0 / sqrt(1 + 2 * cam.k1() * r2);
118 
119  distorted_p.x = cam.cx() + dx * fact;
120  distorted_p.y = cam.cy() + dy * fact;
121  return;
122 }
123 /*************************************************************************************************************************/
124 // Removes distortion of a pair of pixel coordinates x,y.
127  mrpt::utils::TPixelCoordf& undistorted_p)
128 {
129  std::vector<TPixelCoordf> in_p(1), out_p;
130  in_p[0] = p;
131 
134 
135  ASSERT_(out_p.size() == 1);
136  undistorted_p = out_p[0];
137 
138  // It's explained fine in page 3, "A visual compass based on SLAM"
139 }
140 
141 /*************************************************************************************************************************/
142 /*************************************************************************************************************************/
143 /**************************************************Davison
144  * Style**********************************************************/
145 /*************************************************************************************************************************/
146 
147 /** Return the (distorted) pixel position of a 3D point given in coordinates
148  * relative to the camera (+Z pointing forward, +X to the right)
149  */
151  const mrpt::math::TPoint3D& p3D,
152  mrpt::utils::TPixelCoordf& distorted_p) const
153 {
154  // JLBC: From Davison's SceneLib:
155  //
156  // 1 distortion coefficient model (+ projection)
157  //
158 
159  // Offsets from the image center for the undistorted projection, in units of
160  // pixels:
161 
162  ASSERT_(p3D.z != 0)
163  const double dx = (p3D.x / p3D.z) * cam.fx();
164  const double dy = (p3D.y / p3D.z) * cam.fy();
165 
166  // 1 distortion coeff. model:
167  const double r2 = square(dx) + square(dy);
168 
169  const double fact =
170  1.0 / sqrt(1 + 2 * cam.k1() * r2); // Note the "+2" sign
171 
172  distorted_p.x = cam.cx() + dx * fact;
173  distorted_p.y = cam.cy() + dy * fact;
174 }
175 
176 /** Return the 3D location of a point (at a fixed distance z=1), for the given
177  * (distorted) pixel position
178  */
180  const mrpt::utils::TPixelCoordf& distorted_p,
181  mrpt::math::TPoint3D& p3D) const
182 {
183  // JLBC: From Davison's SceneLib:
184  //
185  // 1 distortion coefficient model (+ projection)
186  //
187  const double dx = distorted_p.x - cam.cx();
188  const double dy = distorted_p.y - cam.cy();
189  const double r2 = square(dx) + square(dy);
190  double factor = 1.0 / sqrt(1 - 2 * cam.k1() * r2); // Note the "-2" sign
191 
192  p3D.x = dx * factor / cam.fx();
193  p3D.y = dy * factor / cam.fy();
194  p3D.z = 1.0;
195 }
196 
197 // Jacobian of the projection of 3D points (with distortion), as done in
198 // project_3D_point \f$ \frac{\partial \vct{h}}{\partial \vct{y}} \f$
199 // JL: See .h file for all the formulas
201  const mrpt::math::TPoint3D& p3D, math::CMatrixDouble& dh_dy) const
202 {
203  /*
204  \frac{\partial \vct{u}}{\partial \vct{y}} =
205  \left( \begin{array}{ccc}
206  \frac{f_x}{y_z} & 0 & - y \frac f_x}{y_z^2} \\
207  0 & \frac{f_y}{y_z} & - y \frac f_y}{y_z^2} \\
208  \end{array} \right)
209  */
210  ASSERT_(p3D.z != 0)
211 
212  CMatrixDouble du_dy(2, 3); // Default all to zeroes.
213 
214  du_dy(0, 0) = cam.fx() / p3D.z;
215  du_dy(0, 2) = -p3D.x * cam.fx() / square(p3D.z);
216  du_dy(1, 1) = cam.fy() / p3D.z;
217  du_dy(1, 2) = -p3D.y * cam.fy() / square(p3D.z);
218 
219  /*
220  f = 1+ 2 k_1 (u_x^2+u_y^2), then:
221 
222  \frac{\partial \vct{h}}{\partial \vct{u}} =
223  \left( \begin{array}{cc}
224  \frac{ 1+2 k_1 u_y^2 }{f^{3/2}} & -\frac{2 u_x u_y k_1 }{f^{3/2}} \\
225  -\frac{2 u_x u_y k_1 }{f^{3/2}} & \frac{ 1+2 k_1 u_x^2 }{f^{3/2}}
226  \end{array} \right)
227  */
228  const double ux = (p3D.x / p3D.z) *
229  cam.fx(); // coordinates with (0,0) at the image center
230  const double uy = (p3D.y / p3D.z) * cam.fy();
231 
232  const double ux_sqr = square(ux);
233  const double uy_sqr = square(uy);
234 
235  const double r2 = ux_sqr + uy_sqr;
236 
237  const double f = 1 + 2 * cam.k1() * r2;
238  const double f1_2 = sqrt(f);
239  const double f3_2 = f1_2 * f;
240 
241  // Now form the proper dh_by_du by manipulating the outer product matrix
242 
243  CMatrixDouble dh_du(2, 2);
244  dh_du *= -2 * cam.k1() / f3_2;
245  dh_du(0, 0) += (1 / f1_2);
246  dh_du(1, 1) += (1 / f1_2);
247 
248  // dh_du(0,0) = 1+2*cam.k1()*uy_sqr;
249  // dh_du(1,1) = 1+2*cam.k1()*ux_sqr;
250 
251  // dh_du(0,1) =
252  // dh_du(1,0) = -2*ux*uy*cam.k1()/pow(f,1.5);
253 
254  // Jacobian dh_dy = dh_du * du_dy (Result is 2x3)
255  dh_dy.multiply(dh_du, du_dy);
256 }
257 
258 /* Jacobian of the unprojection of a pixel (with distortion) back into a 3D
259 point, as done in unproject_3D_point \f$ \frac{\partial \vct{y}}{\partial
260 \vct{h}} \f$, evaluated at the pixel p
261 \note JLBC: Added in March, 2009. Should be equivalent to Davison's
262 WideCamera::ProjectionJacobian
263 \sa unproject_3D_point
264 */
266  const mrpt::utils::TPixelCoordf& p, math::CMatrixDouble& dy_dh) const
267 {
268  // dy/du
269  CMatrixDouble dy_du(3, 2); // Default is all zeroes
270  dy_du(0, 0) = 1.0 / cam.fx();
271  dy_du(1, 1) = 1.0 / cam.fy();
272 
273  // MAAA:
274  //// du/dh
275  const double dx = p.x - cam.cx();
276  const double dy = p.y - cam.cy();
277  const double radi2 = square(dx) + square(dy);
278 
279  double f = 1 - 2 * cam.k1() * radi2;
280  double f1_2 = sqrt(f);
281  double f3_2 = f1_2 * f;
282 
283  CMatrixDouble du_dh(2, 3);
284 
285  // const double f = 1 - 2*cam.k1()*radi2;
286  // const double inv_f_15 = 1.0f/powf(f,1.5f);
287  // du_dh(0,0) = ( 1 - 2*cam.k1()*square(dy) ) * inv_f_15;
288  // du_dh(0,1) =
289  // du_dh(1,0) = ( 2*cam.k1()*dx*dy ) * inv_f_15;
290  // du_dh(1,1) = ( 1 - 2*cam.k1()*square(dx) ) * inv_f_15;
291 
292  du_dh *= 2 * cam.k1() / f3_2;
293  du_dh(0, 0) += (1 / f1_2);
294  du_dh(1, 1) += (1 / f1_2);
295 
296  // Jacobian dy_dh = dy_du * du_dh (Result is 3x2)
297  dy_dh.multiply(dy_du, du_dh);
298 }
299 
301  const mrpt::utils::CConfigFileBase& source, const std::string& section)
302 {
303  MRPT_START
304 
305  // Read camera parameters: They are mandatory, we'll raise an exception if
306  // not found:
307  double cx = 0.0f, cy = 0.0f, fx = 0.0f, fy = 0.0f;
308 
309  // MRPT_LOAD_HERE_CONFIG_VAR_NO_DEFAULT(nrows,int,cam.nrows, source,
310  // section)
311  // MRPT_LOAD_HERE_CONFIG_VAR_NO_DEFAULT(ncols,int,cam.ncols, source,
312  // section)
313 
314  MRPT_LOAD_HERE_CONFIG_VAR_NO_DEFAULT(cx, double, cx, source, section)
315  MRPT_LOAD_HERE_CONFIG_VAR_NO_DEFAULT(cy, double, cy, source, section)
316  MRPT_LOAD_HERE_CONFIG_VAR_NO_DEFAULT(fx, double, fx, source, section)
317  MRPT_LOAD_HERE_CONFIG_VAR_NO_DEFAULT(fy, double, fy, source, section)
318 
319  cam.setIntrinsicParamsFromValues(fx, fy, cx, cy);
320 
321  CVectorDouble DD;
322  source.read_vector(section, "dist_params", CVectorDouble(), DD, true);
323  ASSERT_(DD.size() == 4 || DD.size() == 5)
324 
325  this->cam.setDistortionParamsVector(DD);
326 
327  MRPT_END
328 }
329 
330 /** This method displays clearly all the contents of the structure in textual
331  * form, sending it to a CStream. */
mrpt::utils::TCamera cam
The parameters of a camera.
Definition: CCamModel.h:40
A pair (x,y) of pixel coordinates (subpixel resolution).
Definition: TPixelCoord.h:21
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
double cy() const
Get the value of the principal point y-coordinate (in pixels).
Definition: TCamera.h:176
void jacobian_project_with_distortion(const mrpt::math::TPoint3D &p3D, math::CMatrixDouble &dh_dy) const
Jacobian of the projection of 3D points (with distortion), as done in project_3D_point ...
Definition: CCamModel.cpp:200
Column vector, like Eigen::MatrixX*, but automatically initialized to zeros since construction...
Definition: eigen_frwds.h:42
void loadFromConfigFile(const mrpt::utils::CConfigFileBase &source, const std::string &section) override
This method load the options from a ".ini"-like file or memory-stored string list.
Definition: CCamModel.cpp:300
This class allows loading and storing values and vectors of different types from a configuration text...
T square(const T x)
Inline function for the square of a number.
Definition: bits.h:55
double fx() const
Get the value of the focal length x-value (in pixels).
Definition: TCamera.h:178
double fy() const
Get the value of the focal length y-value (in pixels).
Definition: TCamera.h:180
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:41
This base provides a set of functions for maths stuff.
Definition: CArrayNumeric.h:19
double k1() const
Get the value of the k1 distortion parameter.
Definition: TCamera.h:190
void jacob_undistor_fm(const mrpt::utils::TPixelCoordf &uvd, math::CMatrixDouble &J_undist)
Jacobian for undistortion the image coordinates.
Definition: CCamModel.cpp:25
#define MRPT_END
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
void jacobian_unproject_with_distortion(const mrpt::utils::TPixelCoordf &p, math::CMatrixDouble &dy_dh) const
Jacobian of the unprojection of a pixel (with distortion) back into a 3D point, as done in unproject_...
Definition: CCamModel.cpp:265
void unproject_3D_point(const mrpt::utils::TPixelCoordf &distorted_p, mrpt::math::TPoint3D &p3D) const
Return the 3D location of a point (at a fixed distance z=1), for the given (distorted) pixel position...
Definition: CCamModel.cpp:179
void setDistortionParamsVector(const mrpt::math::CMatrixDouble15 &distParVector)
Set the whole vector of distortion params of the camera.
Definition: TCamera.h:143
Classes for computer vision, detectors, features, etc.
void project_3D_point(const mrpt::math::TPoint3D &p3D, mrpt::utils::TPixelCoordf &distorted_p) const
Return the (distorted) pixel position of a 3D point given in coordinates relative to the camera (+Z p...
Definition: CCamModel.cpp:150
double x
X,Y,Z coordinates.
void jacob_undistor(const mrpt::utils::TPixelCoordf &p, mrpt::math::CMatrixDouble &J_undist)
Calculate the image coordinates undistorted.
Definition: CCamModel.cpp:56
GLsizei const GLchar ** string
Definition: glext.h:4101
double cx() const
Get the value of the principal point x-coordinate (in pixels).
Definition: TCamera.h:174
void distort_a_point(const mrpt::utils::TPixelCoordf &p, mrpt::utils::TPixelCoordf &distorted_p)
Return the pixel position distorted by the camera.
Definition: CCamModel.cpp:103
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
Definition: CPoint.h:17
mrpt::math::CMatrixDouble33 intrinsicParams
Matrix of intrinsic parameters (containing the focal length and principal point coordinates) ...
Definition: TCamera.h:57
std::vector< double > getDistortionParamsAsVector() const
Get a vector with the distortion params of the camera.
Definition: TCamera.h:135
#define MRPT_START
#define MRPT_LOAD_HERE_CONFIG_VAR_NO_DEFAULT( variableName, variableType, targetVariable, configFileObject, sectionNameStr)
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void dumpToTextStream(mrpt::utils::CStream &out) const override
This method displays clearly all the contents of the structure in textual form, sending it to a CStre...
Definition: CCamModel.cpp:332
GLsizei GLsizei GLchar * source
Definition: glext.h:4082
#define ASSERT_(f)
void undistort_points(const std::vector< mrpt::utils::TPixelCoordf > &srcDistortedPixels, std::vector< mrpt::utils::TPixelCoordf > &dstUndistortedPixels, const mrpt::math::CMatrixDouble33 &intrinsicParams, const std::vector< double > &distortionParams)
Undistort a list of points given by their pixel coordinates, provided the camera matrix and distortio...
Definition: pinhole.cpp:132
void undistort_point(const mrpt::utils::TPixelCoordf &p, mrpt::utils::TPixelCoordf &undistorted_p)
Return the pixel position undistorted by the camera The input values &#39;col&#39; and &#39;row&#39; will be replace ...
Definition: CCamModel.cpp:125
dynamic_vector< double > CVectorDouble
Column vector, like Eigen::MatrixXd, but automatically initialized to zeros since construction...
Definition: eigen_frwds.h:44
double k2() const
Get the value of the k2 distortion parameter.
Definition: TCamera.h:192
Lightweight 3D point.
CCamModel()
Default Constructor.
Definition: CCamModel.cpp:23
GLfloat GLfloat p
Definition: glext.h:6305
void setIntrinsicParamsFromValues(double fx, double fy, double cx, double cy)
Set the matrix of intrinsic params of the camera from the individual values of focal length and princ...
Definition: TCamera.h:118



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019