MRPT  1.9.9
CCamModel.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, 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 #ifndef CCamModel_H
10 #define CCamModel_H
11 
12 #include <mrpt/img/TCamera.h>
13 #include <mrpt/system/os.h>
14 #include <mrpt/vision/utils.h>
17 
18 namespace mrpt::vision
19 {
20 /** This class represent a pinhole camera model for Monocular SLAM and
21  * implements some associated Jacobians
22  *
23  * The camera parameters are accessible in the public member CCamModel::cam
24  *
25  * - Versions:
26  * - First version: By Antonio J. Ortiz de Galistea.
27  * - 2009-2010: Rewritten by various authors.
28  *
29  * \sa mrpt::img::TCamera, CMonoSlam, the application <a
30  * href="http://www.mrpt.org/Application:camera-calib-gui" >camera-calib-gui</a>
31  * for calibrating a camera
32  * \ingroup mrpt_vision_grp
33  */
35 {
36  public:
37  /** The parameters of a camera */
39 
40  /** Default Constructor */
41  CCamModel();
42 
43  void loadFromConfigFile(
45  const std::string& section) override; // See base docs
46  void dumpToTextStream(std::ostream& out) const override; // See base docs
47 
48  /** Constructor from a ini file
49  */
51 
52  /** Jacobian for undistortion the image coordinates */
53  void jacob_undistor_fm(
54  const mrpt::img::TPixelCoordf& uvd, math::CMatrixDouble& J_undist);
55 
56  /** Calculate the image coordinates undistorted
57  */
58  void jacob_undistor(
60 
61  /** Return the pixel position distorted by the camera
62  */
63  void distort_a_point(
65 
66  /** Return the pixel position undistorted by the camera
67  * The input values 'col' and 'row' will be replace for the new values
68  *(undistorted)
69  */
70  void undistort_point(
72  mrpt::img::TPixelCoordf& undistorted_p);
73 
74  /** Return the (distorted) pixel position of a 3D point given in
75  * coordinates relative to the camera (+Z pointing forward, +X to the right)
76  * \sa unproject_3D_point
77  */
78  void project_3D_point(
79  const mrpt::math::TPoint3D& p3D,
80  mrpt::img::TPixelCoordf& distorted_p) const;
81 
82  /** Return the 3D location of a point (at a fixed distance z=1), for the
83  * given (distorted) pixel position
84  * \sa project_3D_point
85  * \note Of course, there is a depth ambiguity, so the returned 3D point
86  * must be considered a direction from the camera focus, or a vector, rather
87  * than a meaninful physical point.
88  */
89  void unproject_3D_point(
90  const mrpt::img::TPixelCoordf& distorted_p,
91  mrpt::math::TPoint3D& p3D) const;
92 
93  /** Jacobian of the projection of 3D points (with distortion), as done in
94  project_3D_point \f$ \frac{\partial h}{\partial y} \f$, evaluated at the
95  point p3D (read below the full explanation)
96 
97  We define \f$ h = (h_x ~ h_y) \f$ as the projected point in pixels (origin
98  at the top-left corner),
99  and \f$ y=( y_x ~ y_y ~ y_z ) \f$ as the 3D point in space, in coordinates
100  relative to the camera (+Z pointing forwards).
101 
102  Then this method computes the 2x3 Jacobian:
103 
104  \f[
105  \frac{\partial h}{\partial y} = \frac{\partial h}{\partial u}
106  \frac{\partial u}{\partial y}
107  \f]
108 
109  With:
110 
111  \f[
112  \frac{\partial u}{\partial y} =
113  \left( \begin{array}{ccc}
114  \frac{f_x}{y_z} & 0 & - y \frac{f_x}{y_z^2} \\
115  0 & \frac{f_y}{y_z} & - y \frac{f_y}{y_z^2} \\
116  \end{array} \right)
117  \f]
118 
119  where \f$ f_x, f_y \f$ is the focal length in units of pixel sizes in x and
120  y, respectively.
121  And, if we define:
122 
123  \f[
124  f = 1+ 2 k_1 (u_x^2+u_y^2)
125  \f]
126 
127  then:
128 
129  \f[
130  \frac{\partial h}{\partial u} =
131  \left( \begin{array}{cc}
132  \frac{ 1+2 k_1 u_y^2 }{f^{3/2}} & -\frac{2 u_x u_y k_1 }{f^{3/2}} \\
133  -\frac{2 u_x u_y k_1 }{f^{3/2}} & \frac{ 1+2 k_1 u_x^2 }{f^{3/2}}
134  \end{array} \right)
135  \f]
136 
137  \note JLBC: Added in March, 2009. Should be equivalent to Davison's
138  WideCamera::ProjectionJacobian
139  \sa project_3D_point
140  */
142  const mrpt::math::TPoint3D& p3D, math::CMatrixDouble& dh_dy) const;
143 
144  /** Jacobian of the unprojection of a pixel (with distortion) back into a 3D
145  point, as done in unproject_3D_point \f$ \frac{\partial y}{\partial h} \f$,
146  evaluated at the pixel p
147  \note JLBC: Added in March, 2009. Should be equivalent to Davison's
148  WideCamera::UnprojectionJacobian
149  \sa unproject_3D_point
150  */
152  const mrpt::img::TPixelCoordf& p, math::CMatrixDouble& dy_dh) const;
153 
154  template <typename T>
156  {
157  T x_, y_;
158  T x_2, y_2;
159  T R;
160  T K;
161  T x__, y__;
162  };
163  template <typename T, typename POINT>
165  const POINT& p, CameraTempVariables<T>& v) const
166  {
167  v.x_ = p[1] / p[0];
168  v.y_ = p[2] / p[0];
169  v.x_2 = square(v.x_);
170  v.y_2 = square(v.y_);
171  v.R = v.x_2 + v.y_2;
172  v.K = 1 + v.R * (cam.k1() + v.R * (cam.k2() + v.R * cam.k3()));
173  T xy = v.x_ * v.y_, p1 = cam.p1(), p2 = cam.p2();
174  v.x__ = v.x_ * v.K + 2 * p1 * xy + p2 * (3 * v.x_2 + v.y_2);
175  v.y__ = v.y_ * v.K + p1 * (v.x_2 + 3 * v.y_2) + 2 * p2 * xy;
176  }
177 
178  template <typename T, typename POINT, typename PIXEL>
179  inline void getFullProjection(const POINT& pIn, PIXEL& pOut) const
180  {
183  getFullProjectionT(tmp, pOut);
184  }
185 
186  template <typename T, typename PIXEL>
187  inline void getFullProjectionT(
188  const CameraTempVariables<T>& tmp, PIXEL& pOut) const
189  {
190  pOut[0] = cam.fx() * tmp.x__ + cam.cx();
191  pOut[1] = cam.fy() * tmp.y__ + cam.cy();
192  }
193 
194  template <typename T, typename POINT, typename MATRIX>
195  inline void getFullJacobian(const POINT& pIn, MATRIX& mOut) const
196  {
199  getFullJacobianT(pIn, tmp, mOut);
200  }
201 
202  template <typename T, typename POINT, typename MATRIX>
204  const POINT& pIn, const CameraTempVariables<T>& tmp, MATRIX& mOut) const
205  {
206  T x_ = 1 / pIn[0];
207  T x_2 = square(x_);
208  // First two jacobians...
210  T tmpK = 2 * (cam.k1() + tmp.R * (2 * cam.k2() + 3 * tmp.R * cam.k3()));
211  T tmpKx = tmpK * tmp.x_;
212  T tmpKy = tmpK * tmp.y_;
213  T yx2 = -pIn[1] * x_2;
214  T zx2 = -pIn[2] * x_2;
215  J21.set_unsafe(0, 0, yx2);
216  J21.set_unsafe(0, 1, x_);
217  J21.set_unsafe(0, 2, 0);
218  J21.set_unsafe(1, 0, zx2);
219  J21.set_unsafe(1, 1, 0);
220  J21.set_unsafe(1, 2, x_);
221  J21.set_unsafe(2, 0, tmpKx * yx2 + tmpKy * zx2);
222  J21.set_unsafe(2, 1, tmpKx * x_);
223  J21.set_unsafe(2, 2, tmpKy * x_);
224  // Last two jacobians...
225  T pxpy = 2 * (cam.p1() * tmp.x_ + cam.p2() * tmp.y_);
226  T p1y = cam.p1() * tmp.y_;
227  T p2x = cam.p2() * tmp.x_;
229  T fx = cam.fx(), fy = cam.fy();
230  J43.set_unsafe(0, 0, fx * (tmp.K + 2 * p1y + 6 * p2x));
231  J43.set_unsafe(0, 1, fx * pxpy);
232  J43.set_unsafe(0, 2, fx * tmp.x_);
233  J43.set_unsafe(1, 0, fy * pxpy);
234  J43.set_unsafe(1, 1, fy * (tmp.K + 6 * p1y + 2 * p2x));
235  J43.set_unsafe(1, 2, fy * tmp.y_);
236  mOut.multiply(J43, J21);
237  // cout<<"J21:\n"<<J21<<"\nJ43:\n"<<J43<<"\nmOut:\n"<<mOut;
238  }
239 
240  private:
241  // These functions are little tricks to avoid multiple initialization.
242  // They are intended to initialize the common parts of the jacobians just
243  // once,
244  // and not in each iteration.
245  // They are mostly useless outside the scope of this function.
247  {
249  res.set_unsafe(0, 1, 0);
250  res.set_unsafe(1, 0, 0);
251  return res;
252  }
254  {
256  res.set_unsafe(0, 0, 1);
257  res.set_unsafe(0, 1, 0);
258  res.set_unsafe(1, 0, 0);
259  res.set_unsafe(1, 1, 1);
260  return res;
261  }
263  {
265  res.set_unsafe(0, 1, 0);
266  res.set_unsafe(0, 2, 0);
267  res.set_unsafe(1, 0, 0);
268  res.set_unsafe(1, 2, 0);
269  res.set_unsafe(2, 0, 0);
270  res.set_unsafe(2, 1, 0);
271  res.set_unsafe(2, 2, 1);
272  res.set_unsafe(2, 3, 0);
273  return res;
274  }
275 
276  public:
277  template <typename POINTIN, typename POINTOUT, typename MAT22>
279  const POINTIN& pIn, POINTOUT& pOut, MAT22& jOut) const
280  {
281  // Temporary variables (well, there are some more, but these are the
282  // basics)
283  // WARNING!: this shortcut to avoid repeated initialization makes the
284  // method somewhat
285  // faster, but makes it incapable of being used in more than one thread
286  // simultaneously!
287  using mrpt::square;
294  static mrpt::math::CMatrixFixedNumeric<double, 2, 3> J4; // This is not
295  // initialized
296  // in a
297  // special
298  // way,
299  // although
300  // declaring
301  // it
303  mrpt::math::CArrayNumeric<double, 2> tmp2; // This would be a
304  // array<double,3>, but to
305  // avoid copying, we let
306  // "R2" lie in tmp1.
307  // Camera Parameters
308  double cx = cam.cx(), cy = cam.cy(), ifx = 1 / cam.fx(),
309  ify = 1 / cam.fy();
310  double K1 = cam.k1(), K2 = cam.k2(), p1 = cam.p1(), p2 = cam.p2(),
311  K3 = cam.k3();
312  // First step: intrinsic matrix.
313  tmp1[0] = (pIn[0] - cx) * ifx;
314  tmp1[1] = (pIn[1] - cy) * ify;
315  J1.set_unsafe(0, 0, ifx);
316  J1.set_unsafe(1, 1, ify);
317  // Second step: adding temporary variables, related to the distortion.
318  tmp1[2] = square(tmp1[0]) + square(tmp1[1]);
319  double sK1 = square(K1);
320  double K12 = sK1 - K2;
321  double K123 = -K1 * sK1 + 2 * K1 * K2 - K3; //-K1^3+2K1K2-K3
322  // tmp1[3]=1-K1*tmp1[2]+K12*square(tmp1[2]);
323  tmp1[3] = 1 + tmp1[2] * (-K1 + tmp1[2] * (K12 + tmp1[2] * K123));
324  J2.set_unsafe(2, 0, 2 * tmp1[0]);
325  J2.set_unsafe(2, 1, 2 * tmp1[1]);
326  double jTemp = -2 * K1 + 4 * tmp1[2] * K12 + 6 * square(tmp1[2]) * K123;
327  J2.set_unsafe(3, 0, tmp1[0] * jTemp);
328  J2.set_unsafe(3, 1, tmp1[1] * jTemp);
329  // Third step: radial distortion. Really simple, since most work has
330  // been done in the previous step.
331  tmp2[0] = tmp1[0] * tmp1[3];
332  tmp2[1] = tmp1[1] * tmp1[3];
333  J3.set_unsafe(0, 0, tmp1[3]);
334  J3.set_unsafe(0, 3, tmp1[0]);
335  J3.set_unsafe(1, 1, tmp1[3]);
336  J3.set_unsafe(1, 3, tmp1[1]);
337  // Fourth step: tangential distorion. A little more complicated, but not
338  // much more.
339  double prod = tmp2[0] * tmp2[1];
340  // References to tmp1[2] are not errors! That element is "R2".
341  pOut[0] = tmp2[0] - p1 * prod - p2 * (tmp1[2] + 2 * square(tmp2[0]));
342  pOut[1] = tmp2[1] - p1 * (tmp1[2] + 2 * square(tmp2[1])) - p2 * prod;
343  J4.set_unsafe(0, 0, 1 - p1 * tmp2[1] - 4 * p2 * tmp2[0]);
344  J4.set_unsafe(0, 1, -p1 * tmp2[0]);
345  J4.set_unsafe(0, 2, -p2);
346  J4.set_unsafe(1, 0, -p2 * tmp2[1]);
347  J4.set_unsafe(1, 1, 1 - 4 * p1 * tmp2[1] - p2 * tmp2[0]);
348  J4.set_unsafe(1, 2, -p1);
349  // As fast as possible, and without more temporaries, let the jacobian
350  // be J4*J3*J2*J1;
351  jOut.multiply_ABC(J4, J3, J2); // Note that using the other order is
352  // not possible due to matrix sizes
353  // (jOut may, and most probably will, be
354  // fixed).
355  jOut.multiply(jOut, J1);
356  }
357 
358 }; // end class
359 
360 }
361 #endif //__CCamModel_H
362 
363 
double k3() const
Get the value of the k3 distortion parameter.
Definition: TCamera.h:183
void loadFromConfigFile(const mrpt::config::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:298
void getFullProjection(const POINT &pIn, PIXEL &pOut) const
Definition: CCamModel.h:179
This is a virtual base class for sets of options than can be loaded from and/or saved to configuratio...
double fx() const
Get the value of the focal length x-value (in pixels).
Definition: TCamera.h:163
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:198
double fy() const
Get the value of the focal length y-value (in pixels).
Definition: TCamera.h:165
CArrayNumeric is an array for numeric types supporting several mathematical operations (actually...
Definition: CArrayNumeric.h:25
A pair (x,y) of pixel coordinates (subpixel resolution).
Definition: TPixelCoord.h:18
void getFullInverseModelWithJacobian(const POINTIN &pIn, POINTOUT &pOut, MAT22 &jOut) const
Definition: CCamModel.h:278
double k2() const
Get the value of the k2 distortion parameter.
Definition: TCamera.h:177
T square(const T x)
Inline function for the square of a number.
A numeric matrix of compile-time fixed size.
This class allows loading and storing values and vectors of different types from a configuration text...
double cy() const
Get the value of the principal point y-coordinate (in pixels).
Definition: TCamera.h:161
void getTemporaryVariablesForTransform(const POINT &p, CameraTempVariables< T > &v) const
Definition: CCamModel.h:164
void jacob_undistor_fm(const mrpt::img::TPixelCoordf &uvd, math::CMatrixDouble &J_undist)
Jacobian for undistortion the image coordinates.
Definition: CCamModel.cpp:26
Classes for computer vision, detectors, features, etc.
Definition: CCamModel.h:18
Structure to hold the parameters of a pinhole camera model.
Definition: TCamera.h:27
void getFullProjectionT(const CameraTempVariables< T > &tmp, PIXEL &pOut) const
Definition: CCamModel.h:187
GLsizei const GLchar ** string
Definition: glext.h:4101
double p1() const
Get the value of the p1 distortion parameter.
Definition: TCamera.h:179
void unproject_3D_point(const mrpt::img::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:178
double cx() const
Get the value of the principal point x-coordinate (in pixels).
Definition: TCamera.h:159
void getFullJacobian(const POINT &pIn, MATRIX &mOut) const
Definition: CCamModel.h:195
const GLdouble * v
Definition: glext.h:3678
This class represent a pinhole camera model for Monocular SLAM and implements some associated Jacobia...
Definition: CCamModel.h:34
mrpt::img::TCamera cam
The parameters of a camera.
Definition: CCamModel.h:38
void dumpToTextStream(std::ostream &out) const override
This method displays clearly all the contents of the structure in textual form, sending it to a CStre...
Definition: CCamModel.cpp:330
void jacob_undistor(const mrpt::img::TPixelCoordf &p, mrpt::math::CMatrixDouble &J_undist)
Calculate the image coordinates undistorted.
Definition: CCamModel.cpp:57
mrpt::math::CMatrixFixedNumeric< double, 4, 2 > secondInverseJacobian() const
Definition: CCamModel.h:253
void undistort_point(const mrpt::img::TPixelCoordf &p, mrpt::img::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:126
void project_3D_point(const mrpt::math::TPoint3D &p3D, mrpt::img::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
void distort_a_point(const mrpt::img::TPixelCoordf &p, mrpt::img::TPixelCoordf &distorted_p)
Return the pixel position distorted by the camera.
Definition: CCamModel.cpp:104
GLsizei GLsizei GLchar * source
Definition: glext.h:4082
void jacobian_unproject_with_distortion(const mrpt::img::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:263
void getFullJacobianT(const POINT &pIn, const CameraTempVariables< T > &tmp, MATRIX &mOut) const
Definition: CCamModel.h:203
double p2() const
Get the value of the p2 distortion parameter.
Definition: TCamera.h:181
double k1() const
Get the value of the k1 distortion parameter.
Definition: TCamera.h:175
mrpt::math::CMatrixFixedNumeric< double, 3, 4 > thirdInverseJacobian() const
Definition: CCamModel.h:262
GLuint res
Definition: glext.h:7268
Lightweight 3D point.
CCamModel()
Default Constructor.
Definition: CCamModel.cpp:24
GLfloat GLfloat p
Definition: glext.h:6305
mrpt::math::CMatrixFixedNumeric< double, 2, 2 > firstInverseJacobian() const
Definition: CCamModel.h:246



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020