Main MRPT website > C++ reference for MRPT 1.5.9
epnp.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-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 #ifndef _mrpt_epnp
11 #define _mrpt_epnp
12 #include <mrpt/otherlibs/do_opencv_includes.h>
13 
14 #if MRPT_HAS_OPENCV
15 
16  namespace mrpt
17  {
18  namespace vision
19  {
20  namespace pnp
21  {
22  /** \addtogroup pnp Perspective-n-Point pose estimation
23  * \ingroup mrpt_vision_grp
24  * @{
25  */
26 
27  /**
28  * @class epnp
29  * @author Chandra Mangipudi
30  * @date 11/08/16
31  * @file epnp.h
32  * @brief Efficient PnP - Eigen Wrapper for OpenCV calib3d implementation
33  */
34  class epnp
35  {
36  public:
37  //! Constructor for EPnP class
38  epnp(const cv::Mat& cameraMatrix, const cv::Mat& opoints, const cv::Mat& ipoints);
39 
40  //! Destructor for EPnP class
41  ~epnp();
42 
43  /**
44  * @brief Add a 2d/3d correspondence
45  * @param[in] X X coordinate in Camera coordinate system
46  * @param[in] Y Y coordinate in Camera coordinate system
47  * @param[in] Z Z coordinate in Camera coordinate system
48  * @param[in] u Image pixel coordinate u in x axis
49  * @param[in] v Image pixel coordinate v in y axis
50  */
51  void add_correspondence(const double X, const double Y, const double Z,
52  const double u, const double v);
53 
54  /**
55  * @brief OpenCV wrapper to compute pose
56  * @param[out] R Rotation Matrix
57  * @param[out] t Translation Vector
58  */
59  void compute_pose(cv::Mat& R, cv::Mat& t);
60  private:
61  /**
62  * @brief Initialize Camera Matrix
63  * @param[in] cameraMatrix Camera Intrinsic matrix as a OpenCV Matrix
64  */
65  template <typename T>
66  void init_camera_parameters(const cv::Mat& cameraMatrix)
67  {
68  uc = cameraMatrix.at<T> (0, 2);
69  vc = cameraMatrix.at<T> (1, 2);
70  fu = cameraMatrix.at<T> (0, 0);
71  fv = cameraMatrix.at<T> (1, 1);
72  }
73 
74  /**
75  * @brief Convert object points and image points from OpenCV format to STL matrices
76  * @param opoints Object points in Camera coordinate system
77  * @param ipoints Imate points in pixel coordinates
78  */
79  template <typename OpointType, typename IpointType>
80  void init_points(const cv::Mat& opoints, const cv::Mat& ipoints)
81  {
82  for(int i = 0; i < number_of_correspondences; i++)
83  {
84  pws[3 * i ] = opoints.at<OpointType>(0,i).x;
85  pws[3 * i + 1] = opoints.at<OpointType>(0,i).y;
86  pws[3 * i + 2] = opoints.at<OpointType>(0,i).z;
87 
88  us[2 * i ] = ipoints.at<IpointType>(0,i).x*fu + uc;
89  us[2 * i + 1] = ipoints.at<IpointType>(0,i).y*fv + vc;
90  }
91  }
92 
93  /**
94  * @brief Function to compute reprojection error
95  * @param R Rotation Matrix
96  * @param t Translation Vector
97  * @return
98  */
99  double reprojection_error(const double R[3][3], const double t[3]);
100 
101  /**
102  * @brief Function to select 4 control points from n points
103  */
104  void choose_control_points(void);
105 
106  /**
107  * @brief Convert from object space to relative object space (Barycentric coordinates)
108  */
110 
111  /**
112  * @brief Generate the Matrix M
113  * @param[out] M
114  * @param[in] row
115  * @param[in] alphas
116  * @param[in] u
117  * @param[in] v
118  */
119  void fill_M(CvMat * M, const int row, const double * alphas, const double u, const double v);
120 
121  /**
122  * @brief Internal function
123  * @param[in] betas
124  * @param[in] ut
125  */
126  void compute_ccs(const double * betas, const double * ut);
127 
128  /**
129  * @brief Internal function
130  */
131  void compute_pcs(void);
132 
133 
134  /**
135  * @brief Internal function
136  */
137  void solve_for_sign(void);
138 
139  /**
140  * @brief Internal function
141  * @param[out] L_6x10
142  * @param[in] Rho
143  * @param[in] betas
144  */
145  void find_betas_approx_1(const CvMat * L_6x10, const CvMat * Rho, double * betas);
146 
147  /**
148  * @brief Internal function
149  * @param[out] L_6x10
150  * @param[in] Rho
151  * @param[in] betas
152  */
153  void find_betas_approx_2(const CvMat * L_6x10, const CvMat * Rho, double * betas);
154 
155  /**
156  * @brief Internal function
157  * @param[out] L_6x10
158  * @param[in] Rho
159  * @param[in] betas
160  */
161  void find_betas_approx_3(const CvMat * L_6x10, const CvMat * Rho, double * betas);
162 
163  /**
164  * @brief QR optimization algorithm
165  * @param[in] A
166  * @param[out] b
167  * @param[out] X
168  */
169  void qr_solve(CvMat * A, CvMat * b, CvMat * X);
170 
171  /**
172  * @brief Dot product of two OpenCV vectors
173  * @param[in] v1
174  * @param[in] v2
175  * @return
176  */
177  double dot(const double * v1, const double * v2);
178 
179  /**
180  * @brief Squared distance between two vectors
181  * @param[in] p1
182  * @param[in] p2
183  * @return
184  */
185  double dist2(const double * p1, const double * p2);
186 
187  /**
188  * @brief Get distances between all object points taken 2 at a time(nC2)
189  * @param rho
190  */
191  void compute_rho(double * rho);
192 
193  /**
194  * @brief Internal function
195  * @param[in] ut
196  * @param[out] l_6x10
197  */
198  void compute_L_6x10(const double * ut, double * l_6x10);
199 
200  /**
201  * @brief Gauss Newton iterative algorithm
202  * @param[in] L_6x10
203  * @param[in] Rho
204  * @param[in,out] current_betas
205  */
206  void gauss_newton(const CvMat * L_6x10, const CvMat * Rho, double current_betas[4]);
207 
208  /**
209  * @brief Internal function
210  * @param[in] l_6x10
211  * @param[in] rho
212  * @param[in] cb
213  * @param[out] A
214  * @param[out] b
215  */
216  void compute_A_and_b_gauss_newton(const double * l_6x10, const double * rho,
217  const double cb[4], CvMat * A, CvMat * b);
218 
219  /**
220  * @brief Function to compute pose
221  * @param[in] ut
222  * @param[in] betas
223  * @param[out] R
224  * @param[out] t
225  * @return
226  */
227  double compute_R_and_t(const double * ut, const double * betas,
228  double R[3][3], double t[3]);
229 
230  /**
231  * @brief Helper function to @func compute_R_and_t()
232  * @param R
233  * @param t
234  */
235  void estimate_R_and_t(double R[3][3], double t[3]);
236 
237  /**
238  * @brief Copy function of output result
239  * @param[out] R_dst
240  * @param[out] t_dst
241  * @param[in] R_src
242  * @param[in] t_src
243  */
244  void copy_R_and_t(const double R_dst[3][3], const double t_dst[3],
245  double R_src[3][3], double t_src[3]);
246 
247 
248  double uc; //! Image center in x-direction
249  double vc; //! Image center in y-direction
250  double fu; //! Focal length in x-direction
251  double fv; //! Focal length in y-direction
252 
253  std::vector<double> pws, us, alphas, pcs; //! Internal member variables
254  int number_of_correspondences; //! Number of 2d/3d correspondences
255 
256  double cws[4][3], ccs[4][3]; //! Internal member variables
257  double cws_determinant; //! Internal member variable
258  int max_nr; //! Internal member variable
259  double * A1, * A2; //! Internal member variables
260  };
261 
262  /** @} */ // end of grouping
263 
264  }
265  }
266  }
267 #endif
268 #endif
void compute_A_and_b_gauss_newton(const double *l_6x10, const double *rho, const double cb[4], CvMat *A, CvMat *b)
Internal function.
void compute_barycentric_coordinates(void)
Convert from object space to relative object space (Barycentric coordinates)
void choose_control_points(void)
Function to select 4 control points from n points.
double cws_determinant
Internal member variables.
Definition: epnp.h:257
GLdouble GLdouble t
Definition: glext.h:3610
GLdouble GLdouble z
Definition: glext.h:3734
std::vector< double > pws
Focal length in y-direction.
Definition: epnp.h:253
std::vector< double > us
Definition: epnp.h:253
void find_betas_approx_3(const CvMat *L_6x10, const CvMat *Rho, double *betas)
Internal function.
void compute_pose(cv::Mat &R, cv::Mat &t)
OpenCV wrapper to compute pose.
void compute_rho(double *rho)
Get distances between all object points taken 2 at a time(nC2)
~epnp()
Destructor for EPnP class.
double reprojection_error(const double R[3][3], const double t[3])
Function to compute reprojection error.
double dist2(const double *p1, const double *p2)
Squared distance between two vectors.
int number_of_correspondences
Internal member variables.
Definition: epnp.h:254
void gauss_newton(const CvMat *L_6x10, const CvMat *Rho, double current_betas[4])
Gauss Newton iterative algorithm.
void copy_R_and_t(const double R_dst[3][3], const double t_dst[3], double R_src[3][3], double t_src[3])
Copy function of output result.
void solve_for_sign(void)
Internal function.
std::vector< double > pcs
Definition: epnp.h:253
void fill_M(CvMat *M, const int row, const double *alphas, const double u, const double v)
Generate the Matrix M.
void init_camera_parameters(const cv::Mat &cameraMatrix)
Initialize Camera Matrix.
Definition: epnp.h:66
double * A1
Internal member variable.
Definition: epnp.h:259
double dot(const double *v1, const double *v2)
Dot product of two OpenCV vectors.
double cws[4][3]
Number of 2d/3d correspondences.
Definition: epnp.h:256
void init_points(const cv::Mat &opoints, const cv::Mat &ipoints)
Convert object points and image points from OpenCV format to STL matrices.
Definition: epnp.h:80
double ccs[4][3]
Definition: epnp.h:256
GLubyte GLubyte b
Definition: glext.h:5575
void qr_solve(CvMat *A, CvMat *b, CvMat *X)
QR optimization algorithm.
double fu
Image center in y-direction.
Definition: epnp.h:250
std::vector< double > alphas
Definition: epnp.h:253
void estimate_R_and_t(double R[3][3], double t[3])
Helper function to compute_R_and_t()
const GLdouble * v
Definition: glext.h:3603
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
GLfloat GLfloat v1
Definition: glext.h:3922
const float R
void add_correspondence(const double X, const double Y, const double Z, const double u, const double v)
Add a 2d/3d correspondence.
void find_betas_approx_2(const CvMat *L_6x10, const CvMat *Rho, double *betas)
Internal function.
GLenum GLenum GLvoid * row
Definition: glext.h:3533
double compute_R_and_t(const double *ut, const double *betas, double R[3][3], double t[3])
Function to compute pose.
int max_nr
Internal member variable.
Definition: epnp.h:258
void compute_ccs(const double *betas, const double *ut)
Internal function.
GLenum GLint GLint y
Definition: glext.h:3516
void compute_pcs(void)
Internal function.
GLfloat GLfloat GLfloat v2
Definition: glext.h:3923
GLenum GLint x
Definition: glext.h:3516
double fv
Focal length in x-direction.
Definition: epnp.h:251
epnp(const cv::Mat &cameraMatrix, const cv::Mat &opoints, const cv::Mat &ipoints)
Constructor for EPnP class.
double vc
Image center in x-direction.
Definition: epnp.h:249
void compute_L_6x10(const double *ut, double *l_6x10)
Internal function.
void find_betas_approx_1(const CvMat *L_6x10, const CvMat *Rho, double *betas)
Internal function.



Page generated by Doxygen 1.8.14 for MRPT 1.5.9 Git: 690a4699f Wed Apr 15 19:29:53 2020 +0200 at miƩ abr 15 19:30:12 CEST 2020