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



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST