Main MRPT website > C++ reference for MRPT 1.3.2
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-2015, 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/utils/TCamera.h>
13 #include <mrpt/system/os.h>
14 #include <mrpt/vision/utils.h>
16 
17 namespace mrpt
18 {
19  namespace vision
20  {
21  /** This class represent a pinhole camera model for Monocular SLAM and 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::utils::TCamera, CMonoSlam, the application <a href="http://www.mrpt.org/Application:camera-calib-gui" >camera-calib-gui</a> for calibrating a camera
30  * \ingroup mrpt_vision_grp
31  */
33  {
34  public:
35  mrpt::utils::TCamera cam; //!< The parameters of a camera
36 
37  /** Default Constructor */
38  CCamModel();
39 
40  /** This method loads the options from a ".ini"-like file or memory-stored string list.
41  */
42  void loadFromConfigFile(
43  const mrpt::utils::CConfigFileBase &source,
44  const std::string &section);
45 
46  /** This method displays clearly all the contents of the structure in textual form, sending it to a CStream. */
47  void dumpToTextStream( mrpt::utils::CStream &out) const;
48 
49  /** Constructor from a ini file
50  */
52 
53  /** Jacobian for undistortion the image coordinates */
54  void jacob_undistor_fm(const mrpt::utils::TPixelCoordf &uvd, math::CMatrixDouble &J_undist);
55 
56  /** Calculate the image coordinates undistorted
57  */
58  void jacob_undistor(const mrpt::utils::TPixelCoordf &p, mrpt::math::CMatrixDouble &J_undist );
59 
60  /** Return the pixel position distorted by the camera
61  */
62  void distort_a_point(const mrpt::utils::TPixelCoordf &p, mrpt::utils::TPixelCoordf &distorted_p);
63 
64  /** Return the pixel position undistorted by the camera
65  * The input values 'col' and 'row' will be replace for the new values (undistorted)
66  */
68 
69  /** Return the (distorted) pixel position of a 3D point given in coordinates relative to the camera (+Z pointing forward, +X to the right)
70  * \sa unproject_3D_point
71  */
72  void project_3D_point(const mrpt::math::TPoint3D &p3D, mrpt::utils::TPixelCoordf &distorted_p) const;
73 
74  /** Return the 3D location of a point (at a fixed distance z=1), for the given (distorted) pixel position
75  * \sa project_3D_point
76  * \note Of course, there is a depth ambiguity, so the returned 3D point must be considered a direction from the camera focus, or a vector, rather than a meaninful physical point.
77  */
78  void unproject_3D_point(const mrpt::utils::TPixelCoordf &distorted_p, mrpt::math::TPoint3D &p3D) const;
79 
80  /** Jacobian of the projection of 3D points (with distortion), as done in project_3D_point \f$ \frac{\partial h}{\partial y} \f$, evaluated at the point p3D (read below the full explanation)
81 
82  We define \f$ h = (h_x ~ h_y) \f$ as the projected point in pixels (origin at the top-left corner),
83  and \f$ y=( y_x ~ y_y ~ y_z ) \f$ as the 3D point in space, in coordinates relative to the camera (+Z pointing forwards).
84 
85  Then this method computes the 2x3 Jacobian:
86 
87  \f[
88  \frac{\partial h}{\partial y} = \frac{\partial h}{\partial u} \frac{\partial u}{\partial y}
89  \f]
90 
91  With:
92 
93  \f[
94  \frac{\partial u}{\partial y} =
95  \left( \begin{array}{ccc}
96  \frac{f_x}{y_z} & 0 & - y \frac{f_x}{y_z^2} \\
97  0 & \frac{f_y}{y_z} & - y \frac{f_y}{y_z^2} \\
98  \end{array} \right)
99  \f]
100 
101  where \f$ f_x, f_y \f$ is the focal length in units of pixel sizes in x and y, respectively.
102  And, if we define:
103 
104  \f[
105  f = 1+ 2 k_1 (u_x^2+u_y^2)
106  \f]
107 
108  then:
109 
110  \f[
111  \frac{\partial h}{\partial u} =
112  \left( \begin{array}{cc}
113  \frac{ 1+2 k_1 u_y^2 }{f^{3/2}} & -\frac{2 u_x u_y k_1 }{f^{3/2}} \\
114  -\frac{2 u_x u_y k_1 }{f^{3/2}} & \frac{ 1+2 k_1 u_x^2 }{f^{3/2}}
115  \end{array} \right)
116  \f]
117 
118  \note JLBC: Added in March, 2009. Should be equivalent to Davison's WideCamera::ProjectionJacobian
119  \sa project_3D_point
120  */
121  void jacobian_project_with_distortion(const mrpt::math::TPoint3D &p3D, math::CMatrixDouble & dh_dy ) const;
122 
123 
124  /** Jacobian of the unprojection of a pixel (with distortion) back into a 3D point, as done in unproject_3D_point \f$ \frac{\partial y}{\partial h} \f$, evaluated at the pixel p
125  \note JLBC: Added in March, 2009. Should be equivalent to Davison's WideCamera::UnprojectionJacobian
126  \sa unproject_3D_point
127  */
128  void jacobian_unproject_with_distortion(const mrpt::utils::TPixelCoordf &p, math::CMatrixDouble & dy_dh ) const;
129 
130  template<typename T> struct CameraTempVariables {
131  T x_,y_;
132  T x_2,y_2;
133  T R;
134  T K;
135  T x__,y__;
136  };
137  template<typename T,typename POINT> void getTemporaryVariablesForTransform(const POINT &p,CameraTempVariables<T> &v) const {
138  v.x_=p[1]/p[0];
139  v.y_=p[2]/p[0];
140  v.x_2=square(v.x_);
141  v.y_2=square(v.y_);
142  v.R=v.x_2+v.y_2;
143  v.K=1+v.R*(cam.k1()+v.R*(cam.k2()+v.R*cam.k3()));
144  T xy=v.x_*v.y_,p1=cam.p1(),p2=cam.p2();
145  v.x__=v.x_*v.K+2*p1*xy+p2*(3*v.x_2+v.y_2);
146  v.y__=v.y_*v.K+p1*(v.x_2+3*v.y_2)+2*p2*xy;
147  }
148 
149  template<typename T,typename POINT,typename PIXEL> inline void getFullProjection(const POINT &pIn,PIXEL &pOut) const {
151  getTemporaryVariablesForTransform(pIn,tmp);
152  getFullProjectionT(tmp,pOut);
153  }
154 
155  template<typename T,typename PIXEL> inline void getFullProjectionT(const CameraTempVariables<T> &tmp,PIXEL &pOut) const {
156  pOut[0]=cam.fx()*tmp.x__+cam.cx();
157  pOut[1]=cam.fy()*tmp.y__+cam.cy();
158  }
159 
160  template<typename T,typename POINT,typename MATRIX> inline void getFullJacobian(const POINT &pIn,MATRIX &mOut) const {
162  getTemporaryVariablesForTransform(pIn,tmp);
163  getFullJacobianT(pIn,tmp,mOut);
164  }
165 
166  template<typename T,typename POINT,typename MATRIX> void getFullJacobianT(const POINT &pIn,const CameraTempVariables<T> &tmp,MATRIX &mOut) const {
167  T x_=1/pIn[0];
168  T x_2=square(x_);
169  //First two jacobians...
171  T tmpK=2*(cam.k1()+tmp.R*(2*cam.k2()+3*tmp.R*cam.k3()));
172  T tmpKx=tmpK*tmp.x_;
173  T tmpKy=tmpK*tmp.y_;
174  T yx2=-pIn[1]*x_2;
175  T zx2=-pIn[2]*x_2;
176  J21.set_unsafe(0,0,yx2);
177  J21.set_unsafe(0,1,x_);
178  J21.set_unsafe(0,2,0);
179  J21.set_unsafe(1,0,zx2);
180  J21.set_unsafe(1,1,0);
181  J21.set_unsafe(1,2,x_);
182  J21.set_unsafe(2,0,tmpKx*yx2+tmpKy*zx2);
183  J21.set_unsafe(2,1,tmpKx*x_);
184  J21.set_unsafe(2,2,tmpKy*x_);
185  //Last two jacobians...
186  T pxpy=2*(cam.p1()*tmp.x_+cam.p2()*tmp.y_);
187  T p1y=cam.p1()*tmp.y_;
188  T p2x=cam.p2()*tmp.x_;
190  T fx=cam.fx(),fy=cam.fy();
191  J43.set_unsafe(0,0,fx*(tmp.K+2*p1y+6*p2x));
192  J43.set_unsafe(0,1,fx*pxpy);
193  J43.set_unsafe(0,2,fx*tmp.x_);
194  J43.set_unsafe(1,0,fy*pxpy);
195  J43.set_unsafe(1,1,fy*(tmp.K+6*p1y+2*p2x));
196  J43.set_unsafe(1,2,fy*tmp.y_);
197  mOut.multiply(J43,J21);
198  //cout<<"J21:\n"<<J21<<"\nJ43:\n"<<J43<<"\nmOut:\n"<<mOut;
199  }
200  private:
201  //These functions are little tricks to avoid multiple initialization.
202  //They are intended to initialize the common parts of the jacobians just once,
203  //and not in each iteration.
204  //They are mostly useless outside the scope of this function.
207  res.set_unsafe(0,1,0);
208  res.set_unsafe(1,0,0);
209  return res;
210  }
213  res.set_unsafe(0,0,1);
214  res.set_unsafe(0,1,0);
215  res.set_unsafe(1,0,0);
216  res.set_unsafe(1,1,1);
217  return res;
218  }
221  res.set_unsafe(0,1,0);
222  res.set_unsafe(0,2,0);
223  res.set_unsafe(1,0,0);
224  res.set_unsafe(1,2,0);
225  res.set_unsafe(2,0,0);
226  res.set_unsafe(2,1,0);
227  res.set_unsafe(2,2,1);
228  res.set_unsafe(2,3,0);
229  return res;
230  }
231  public:
232  template<typename POINTIN,typename POINTOUT,typename MAT22> void getFullInverseModelWithJacobian(const POINTIN &pIn,POINTOUT &pOut,MAT22 &jOut) const {
233  //Temporary variables (well, there are some more, but these are the basics)
234  //WARNING!: this shortcut to avoid repeated initialization makes the method somewhat
235  //faster, but makes it incapable of being used in more than one thread
236  //simultaneously!
237  using mrpt::utils::square;
238  static mrpt::math::CMatrixFixedNumeric<double,2,2> J1(firstInverseJacobian());
239  static mrpt::math::CMatrixFixedNumeric<double,4,2> J2(secondInverseJacobian());
240  static mrpt::math::CMatrixFixedNumeric<double,3,4> J3(thirdInverseJacobian());
241  static mrpt::math::CMatrixFixedNumeric<double,2,3> J4; //This is not initialized in a special way, although declaring it
243  mrpt::math::CArray<double,2> tmp2; //This would be a CArray<double,3>, but to avoid copying, we let "R2" lie in tmp1.
244  //Camera Parameters
245  double cx=cam.cx(),cy=cam.cy(),ifx=1/cam.fx(),ify=1/cam.fy();
246  double K1=cam.k1(),K2=cam.k2(),p1=cam.p1(),p2=cam.p2(),K3=cam.k3();
247  //First step: intrinsic matrix.
248  tmp1[0]=(pIn[0]-cx)*ifx;
249  tmp1[1]=(pIn[1]-cy)*ify;
250  J1.set_unsafe(0,0,ifx);
251  J1.set_unsafe(1,1,ify);
252  //Second step: adding temporary variables, related to the distortion.
253  tmp1[2]=square(tmp1[0])+square(tmp1[1]);
254  double sK1=square(K1);
255  double K12=sK1-K2;
256  double K123=-K1*sK1+2*K1*K2-K3; //-K1^3+2K1K2-K3
257  //tmp1[3]=1-K1*tmp1[2]+K12*square(tmp1[2]);
258  tmp1[3]=1+tmp1[2]*(-K1+tmp1[2]*(K12+tmp1[2]*K123));
259  J2.set_unsafe(2,0,2*tmp1[0]);
260  J2.set_unsafe(2,1,2*tmp1[1]);
261  double jTemp=-2*K1+4*tmp1[2]*K12+6*square(tmp1[2])*K123;
262  J2.set_unsafe(3,0,tmp1[0]*jTemp);
263  J2.set_unsafe(3,1,tmp1[1]*jTemp);
264  //Third step: radial distortion. Really simple, since most work has been done in the previous step.
265  tmp2[0]=tmp1[0]*tmp1[3];
266  tmp2[1]=tmp1[1]*tmp1[3];
267  J3.set_unsafe(0,0,tmp1[3]);
268  J3.set_unsafe(0,3,tmp1[0]);
269  J3.set_unsafe(1,1,tmp1[3]);
270  J3.set_unsafe(1,3,tmp1[1]);
271  //Fourth step: tangential distorion. A little more complicated, but not much more.
272  double prod=tmp2[0]*tmp2[1];
273  //References to tmp1[2] are not errors! That element is "R2".
274  pOut[0]=tmp2[0]-p1*prod-p2*(tmp1[2]+2*square(tmp2[0]));
275  pOut[1]=tmp2[1]-p1*(tmp1[2]+2*square(tmp2[1]))-p2*prod;
276  J4.set_unsafe(0,0,1-p1*tmp2[1]-4*p2*tmp2[0]);
277  J4.set_unsafe(0,1,-p1*tmp2[0]);
278  J4.set_unsafe(0,2,-p2);
279  J4.set_unsafe(1,0,-p2*tmp2[1]);
280  J4.set_unsafe(1,1,1-4*p1*tmp2[1]-p2*tmp2[0]);
281  J4.set_unsafe(1,2,-p1);
282  //As fast as possible, and without more temporaries, let the jacobian be J4*J3*J2*J1;
283  jOut.multiply_ABC(J4,J3,J2); //Note that using the other order is not possible due to matrix sizes (jOut may, and most probably will, be fixed).
284  jOut.multiply(jOut,J1);
285  }
286 
287  }; // end class
288 
289  } // end namespace
290 } // end namespace
291 #endif //__CCamModel_H
mrpt::utils::TCamera cam
The parameters of a camera.
Definition: CCamModel.h:35
A pair (x,y) of pixel coordinates (subpixel resolution).
Definition: TPixelCoord.h:21
mrpt::math::CMatrixFixedNumeric< double, 4, 2 > secondInverseJacobian() const
Definition: CCamModel.h:211
T square(const T x)
Inline function for the square of a number.
double p2() const
Get the value of the p2 distortion parameter.
Definition: TCamera.h:176
double k1() const
Get the value of the k1 distortion parameter.
Definition: TCamera.h:170
double k2() const
Get the value of the k2 distortion parameter.
Definition: TCamera.h:172
mrpt::math::CMatrixFixedNumeric< double, 3, 4 > thirdInverseJacobian() const
Definition: CCamModel.h:219
This class allows loading and storing values and vectors of different types from a configuration text...
void getFullJacobianT(const POINT &pIn, const CameraTempVariables< T > &tmp, MATRIX &mOut) const
Definition: CCamModel.h:166
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:38
double fy() const
Get the value of the focal length y-value (in pixels).
Definition: TCamera.h:158
A numeric matrix of compile-time fixed size.
void getTemporaryVariablesForTransform(const POINT &p, CameraTempVariables< T > &v) const
Definition: CCamModel.h:137
double k3() const
Get the value of the k3 distortion parameter.
Definition: TCamera.h:178
T square(const T x)
Inline function for the square of a number.
Definition: bits.h:113
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
This class represent a pinhole camera model for Monocular SLAM and implements some associated Jacobia...
Definition: CCamModel.h:32
A STL container (as wrapper) for arrays of constant size defined at compile time - Users will most li...
Definition: CArray.h:55
void getFullJacobian(const POINT &pIn, MATRIX &mOut) const
Definition: CCamModel.h:160
double fx() const
Get the value of the focal length x-value (in pixels).
Definition: TCamera.h:156
mrpt::math::CMatrixFixedNumeric< double, 2, 2 > firstInverseJacobian() const
Definition: CCamModel.h:205
double p1() const
Get the value of the p1 distortion parameter.
Definition: TCamera.h:174
Lightweight 3D point.
double cx() const
Get the value of the principal point x-coordinate (in pixels).
Definition: TCamera.h:152
void getFullInverseModelWithJacobian(const POINTIN &pIn, POINTOUT &pOut, MAT22 &jOut) const
Definition: CCamModel.h:232
This is a virtual base class for sets of options than can be loaded from and/or saved to configuratio...
void getFullProjectionT(const CameraTempVariables< T > &tmp, PIXEL &pOut) const
Definition: CCamModel.h:155
void VISION_IMPEXP undistort_point(const mrpt::utils::TPixelCoordf &inPt, mrpt::utils::TPixelCoordf &outPt, const mrpt::utils::TCamera &cameraModel)
Undistort one point given by its pixel coordinates and the camera parameters.
Structure to hold the parameters of a pinhole camera model.
Definition: TCamera.h:31
void getFullProjection(const POINT &pIn, PIXEL &pOut) const
Definition: CCamModel.h:149
double cy() const
Get the value of the principal point y-coordinate (in pixels).
Definition: TCamera.h:154



Page generated by Doxygen 1.8.9.1 for MRPT 1.3.2 SVN:Unversioned directory at Tue Dec 8 09:49:21 UTC 2015