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