Main MRPT website > C++ reference for MRPT 1.9.9
upnp.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 // M*//////////////////////////////////////////////////////////////////////////////////////
10 //
11 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
12 //
13 // By downloading, copying, installing or using the software you agree to this
14 // license.
15 // If you do not agree to this license, do not download, install,
16 // copy or use the software.
17 //
18 //
19 // License Agreement
20 // For Open Source Computer Vision Library
21 //
22 // Copyright (C) 2000, Intel Corporation, all rights reserved.
23 // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
24 // Third party copyrights are property of their respective owners.
25 //
26 // Redistribution and use in source and binary forms, with or without
27 // modification,
28 // are permitted provided that the following conditions are met:
29 //
30 // * Redistribution's of source code must retain the above copyright notice,
31 // this list of conditions and the following disclaimer.
32 //
33 // * Redistribution's in binary form must reproduce the above copyright notice,
34 // this list of conditions and the following disclaimer in the documentation
35 // and/or other materials provided with the distribution.
36 //
37 // * The name of the copyright holders may not be used to endorse or promote
38 // products
39 // derived from this software without specific prior written permission.
40 //
41 // This software is provided by the copyright holders and contributors "as is"
42 // and
43 // any express or implied warranties, including, but not limited to, the implied
44 // warranties of merchantability and fitness for a particular purpose are
45 // disclaimed.
46 // In no event shall the Intel Corporation or contributors be liable for any
47 // direct,
48 // indirect, incidental, special, exemplary, or consequential damages
49 // (including, but not limited to, procurement of substitute goods or services;
50 // loss of use, data, or profits; or business interruption) however caused
51 // and on any theory of liability, whether in contract, strict liability,
52 // or tort (including negligence or otherwise) arising in any way out of
53 // the use of this software, even if advised of the possibility of such damage.
54 //
55 // M*/
56 
57 /****************************************************************************************\
58 * Exhaustive Linearization for Robust Camera Pose and Focal Length Estimation.
59 * Contributed by Edgar Riba
60 \****************************************************************************************/
61 
62 #ifndef OPENCV_CALIB3D_UPNP_H_
63 #define OPENCV_CALIB3D_UPNP_H_
64 
65 #include <mrpt/config.h>
66 #include <mrpt/otherlibs/do_opencv_includes.h>
67 
68 #if MRPT_HAS_OPENCV
69 //#include <opencv2/core/core_c.h>
70 #include <iostream>
71 
72 namespace mrpt
73 {
74 namespace vision
75 {
76 namespace pnp
77 {
78 /** \addtogroup pnp Perspective-n-Point pose estimation
79  * \ingroup mrpt_vision_grp
80  * @{
81  */
82 
83 /**
84  * @class upnp
85  * @author Chandra Mangipudi
86  * @date 12/08/16
87  * @file upnp.h
88  * @brief Unified PnP - Eigen Wrapper for OpenCV function
89  */
90 class upnp
91 {
92  public:
93  //! Constructor for UPnP class
94  upnp(
95  const cv::Mat& cameraMatrix, const cv::Mat& opoints,
96  const cv::Mat& ipoints);
97 
98  //! Destructor for UPnP class
99  ~upnp();
100 
101  /**
102  * @brief Function to compute pose
103  * @param[out] R Rotation Matrix
104  * @param[out] t Translation Vector
105  * @return
106  */
107  double compute_pose(cv::Mat& R, cv::Mat& t);
108 
109  private:
110  /**
111  * @brief Initialize camera variables using camera intrinsic matrix
112  * @param[in] cameraMatrix Camera Intrinsic Matrix
113  */
114  template <typename T>
115  void init_camera_parameters(const cv::Mat& cameraMatrix)
116  {
117  uc = cameraMatrix.at<T>(0, 2);
118  vc = cameraMatrix.at<T>(1, 2);
119  fu = 1;
120  fv = 1;
121  }
122 
123  /**
124  * @brief Iniialize Object points and image points from OpenCV Matrix
125  * @param[in] opoints Object Points
126  * @param[in] ipoints Image Points
127  */
128  template <typename OpointType, typename IpointType>
129  void init_points(const cv::Mat& opoints, const cv::Mat& ipoints)
130  {
131  for (int i = 0; i < number_of_correspondences; i++)
132  {
133  pws[3 * i] = opoints.at<OpointType>(i).x;
134  pws[3 * i + 1] = opoints.at<OpointType>(i).y;
135  pws[3 * i + 2] = opoints.at<OpointType>(i).z;
136 
137  us[2 * i] = ipoints.at<IpointType>(i).x;
138  us[2 * i + 1] = ipoints.at<IpointType>(i).y;
139  }
140  }
141 
142  /**
143  * @brief Compute the reprojection error using the estimated Rotation matrix
144  * and Translation Vector
145  * @param[in] R Rotation matrix
146  * @param[in] t Trnaslation Vector
147  * @return Linear least squares error in pixels
148  */
149  double reprojection_error(const double R[3][3], const double t[3]);
150 
151  /**
152  * @brief Function to select 4 control points
153  */
154  void choose_control_points();
155 
156  /**
157  * @brief Function to comput @alphas
158  */
159  void compute_alphas();
160 
161  /**
162  * @brief Function to compute Maucaulay matrix M
163  * @param[out] M Maucaulay matrix
164  * @param[in] row Internal member
165  * @param[in] alphas Internal member
166  * @param[in] u Image pixel x co-ordinate
167  * @param[in] v Image pixel y co-ordinate
168  */
169  void fill_M(
170  cv::Mat* M, const int row, const double* alphas, const double u,
171  const double v);
172 
173  /**
174  * @brief Compute the control points
175  * @param[in] betas Internal member variable
176  * @param[in] ut Internal member variable
177  */
178  void compute_ccs(const double* betas, const double* ut);
179 
180  /**
181  * @brief Compute object points based on control points
182  */
183  void compute_pcs(void);
184 
185  /**
186  * @brief Internal member function
187  */
188  void solve_for_sign(void);
189 
190  /**
191  * @brief Function to approximately calculate betas and focal length
192  * @param[in] Ut
193  * @param[in] Rho
194  * @param[out] betas
195  * @param[out] efs
196  */
198  cv::Mat* Ut, cv::Mat* Rho, double* betas, double* efs);
199 
200  /**
201  * @brief Function to calculate betas and focal length (more accurate)
202  * @param[in] Ut
203  * @param[in] Rho
204  * @param[out] betas
205  * @param[out] efs
206  */
208  cv::Mat* Ut, cv::Mat* Rho, double* betas, double* efs);
209 
210  /**
211  * @brief Function to do a QR decomposition
212  * @param[in] A Matrix to be decomposed
213  * @param[out] b
214  * @param[out] X
215  */
216  void qr_solve(cv::Mat* A, cv::Mat* b, cv::Mat* X);
217 
218  /**
219  * @brief Internal function
220  * @param[in] M1
221  * @return Distance
222  */
224  const cv::Mat& M1);
225 
226  /**
227  * @brief Internal function
228  * @param[in] M1
229  * @param[in] M2
230  * @return Distance
231  */
233  const cv::Mat& M1, const cv::Mat& M2);
234 
235  /**
236  * @brief Get all possible solutions
237  * @param[in] betas
238  * @param[out] solutions
239  */
241  const double betas[5], double solutions[18][3]);
242 
243  /**
244  * @brief Return the sign of the scalar
245  * @param[in] v
246  * @return True if positive else Flase
247  */
248  double sign(const double v);
249 
250  /**
251  * @brief Compute the dot product between two vectors
252  * @param[in] v1
253  * @param[in] v2
254  * @return Dot product
255  */
256  double dot(const double* v1, const double* v2);
257 
258  /**
259  * @brief Compute dot product in 2D with only x and y components
260  * @param[in] v1
261  * @param[in] v2
262  * @return Dot product
263  */
264  double dotXY(const double* v1, const double* v2);
265 
266  /**
267  * @brief Compute the dot product using only z component
268  * @param[in] v1
269  * @param[in] v2
270  * @return Dot product
271  */
272  double dotZ(const double* v1, const double* v2);
273 
274  /**
275  * @brief Compute the euclidean distance squared between two points in 3D
276  * @param[in] p1
277  * @param[in] p2
278  * @return Distance squared
279  */
280  double dist2(const double* p1, const double* p2);
281 
282  /**
283  * @brief Internal fucntion
284  * @param[out] rho
285  */
286  void compute_rho(double* rho);
287 
288  /**
289  * @brief Internal function
290  * @param[in] ut
291  * @param[out] l_6x12
292  */
293  void compute_L_6x12(const double* ut, double* l_6x12);
294 
295  /**
296  * @brief Gauss Newton Iterative optimization
297  * @param[in] L_6x12
298  * @param[in] Rho
299  * @param[in,out] current_betas
300  * @param[out] efs
301  */
302  void gauss_newton(
303  const cv::Mat* L_6x12, const cv::Mat* Rho, double current_betas[4],
304  double* efs);
305 
306  /**
307  * @brief Compute matrix A and vector b
308  * @param[in] l_6x12
309  * @param[in] rho
310  * @param[in] cb
311  * @param[out] A
312  * @param[out] b
313  * @param[in] f
314  */
316  const double* l_6x12, const double* rho, const double cb[4], cv::Mat* A,
317  cv::Mat* b, double const f);
318 
319  /**
320  * @brief Function to compute the pose
321  * @param[in] ut
322  * @param[in] betas
323  * @param[out] R Rotation Matrix
324  * @param[out] t Translation VectorS
325  * @return
326  */
327  double compute_R_and_t(
328  const double* ut, const double* betas, double R[3][3], double t[3]);
329 
330  /**
331  * @brief Helper function to function compute_R_and_t()
332  * @param[out] R Rotaiton matrix
333  * @param[out] t Translation vector
334  */
335  void estimate_R_and_t(double R[3][3], double t[3]);
336 
337  /**
338  * @brief Function to copy the pose
339  * @param[in] R_dst
340  * @param[in] t_dst
341  * @param[out] R_src
342  * @param[out] t_src
343  */
344  void copy_R_and_t(
345  const double R_dst[3][3], const double t_dst[3], double R_src[3][3],
346  double t_src[3]);
347 
348  double uc; //! Image center in x-direction
349  double vc; //! Image center in y-direction
350  double fu; //! Focal length in x-direction
351  double fv; //! Focal length in y-direction
352 
353  std::vector<double> pws; //! Object points
354  std::vector<double> us; //! Image points
355  std::vector<double> alphas, pcs; //! Internal variable
356  int number_of_correspondences; //! Number of 2d/3d correspondences
357 
358  double cws[4][3], ccs[4][3]; //! Control point variables
359  int max_nr; //! Internal variable
360  double *A1, *A2; //! Internal variable
361 };
362 
363 /** @} */ // end of grouping
364 }
365 }
366 }
367 #endif // Check for OPENCV_LIB
368 #endif // OPENCV_CALIB3D_UPNP_H_
mrpt::vision::pnp::upnp::estimate_R_and_t
void estimate_R_and_t(double R[3][3], double t[3])
Helper function to function compute_R_and_t()
mrpt::vision::pnp::upnp::fu
double fu
Image center in y-direction.
Definition: upnp.h:350
mrpt::vision::pnp::upnp::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])
Function to copy the pose.
mrpt::vision::pnp::upnp::compute_rho
void compute_rho(double *rho)
Internal fucntion.
mrpt::vision::pnp::upnp::dotXY
double dotXY(const double *v1, const double *v2)
Compute dot product in 2D with only x and y components.
t
GLdouble GLdouble t
Definition: glext.h:3689
mrpt::vision::pnp::upnp::compute_pose
double compute_pose(cv::Mat &R, cv::Mat &t)
Function to compute pose.
mrpt::vision::pnp::upnp::pws
std::vector< double > pws
Focal length in y-direction.
Definition: upnp.h:353
mrpt::vision::pnp::upnp::compute_ccs
void compute_ccs(const double *betas, const double *ut)
Compute the control points.
mrpt::vision::pnp::upnp::choose_control_points
void choose_control_points()
Function to select 4 control points.
mrpt::vision::pnp::upnp::dot
double dot(const double *v1, const double *v2)
Compute the dot product between two vectors.
mrpt::vision::pnp::upnp::compute_alphas
void compute_alphas()
Function to comput @alphas.
mrpt::vision::pnp::upnp::fill_M
void fill_M(cv::Mat *M, const int row, const double *alphas, const double u, const double v)
Function to compute Maucaulay matrix M.
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
mrpt::vision::pnp::upnp::init_camera_parameters
void init_camera_parameters(const cv::Mat &cameraMatrix)
Initialize camera variables using camera intrinsic matrix.
Definition: upnp.h:115
mrpt::vision::pnp::upnp::upnp
upnp(const cv::Mat &cameraMatrix, const cv::Mat &opoints, const cv::Mat &ipoints)
Constructor for UPnP class.
mrpt::vision::pnp::upnp::uc
double uc
Definition: upnp.h:348
mrpt::vision::pnp::upnp::dotZ
double dotZ(const double *v1, const double *v2)
Compute the dot product using only z component.
R
const float R
Definition: CKinematicChain.cpp:138
mrpt::vision::pnp::upnp::find_betas_and_focal_approx_1
void find_betas_and_focal_approx_1(cv::Mat *Ut, cv::Mat *Rho, double *betas, double *efs)
Function to approximately calculate betas and focal length.
mrpt::vision::pnp::upnp::compute_pcs
void compute_pcs(void)
Compute object points based on control points.
mrpt::vision::pnp::upnp::A1
double * A1
Internal variable.
Definition: upnp.h:360
mrpt::vision::pnp::upnp::number_of_correspondences
int number_of_correspondences
Internal variable.
Definition: upnp.h:356
v
const GLdouble * v
Definition: glext.h:3678
mrpt::vision::pnp::upnp::pcs
std::vector< double > pcs
Definition: upnp.h:355
mrpt::vision::pnp::upnp::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 the pose.
v1
GLfloat GLfloat v1
Definition: glext.h:4105
mrpt::vision::pnp::upnp::qr_solve
void qr_solve(cv::Mat *A, cv::Mat *b, cv::Mat *X)
Function to do a QR decomposition.
mrpt::vision::pnp::upnp::generate_all_possible_solutions_for_f_unk
void generate_all_possible_solutions_for_f_unk(const double betas[5], double solutions[18][3])
Get all possible solutions.
mrpt::vision::pnp::upnp::vc
double vc
Image center in x-direction.
Definition: upnp.h:349
mrpt::vision::pnp::upnp::us
std::vector< double > us
Object points.
Definition: upnp.h:354
mrpt::vision::pnp::upnp::compute_constraint_distance_3param_6eq_6unk_f_unk
cv::Mat compute_constraint_distance_3param_6eq_6unk_f_unk(const cv::Mat &M1, const cv::Mat &M2)
Internal function.
mrpt::vision::pnp::upnp::compute_L_6x12
void compute_L_6x12(const double *ut, double *l_6x12)
Internal function.
v2
GLfloat GLfloat GLfloat v2
Definition: glext.h:4107
b
GLubyte GLubyte b
Definition: glext.h:6279
mrpt::vision::pnp::upnp::gauss_newton
void gauss_newton(const cv::Mat *L_6x12, const cv::Mat *Rho, double current_betas[4], double *efs)
Gauss Newton Iterative optimization.
mrpt::vision::pnp::upnp::init_points
void init_points(const cv::Mat &opoints, const cv::Mat &ipoints)
Iniialize Object points and image points from OpenCV Matrix.
Definition: upnp.h:129
mrpt::vision::pnp::upnp::ccs
double ccs[4][3]
Definition: upnp.h:358
mrpt::vision::pnp::upnp::compute_A_and_b_gauss_newton
void compute_A_and_b_gauss_newton(const double *l_6x12, const double *rho, const double cb[4], cv::Mat *A, cv::Mat *b, double const f)
Compute matrix A and vector b.
mrpt::vision::pnp::upnp::A2
double * A2
Definition: upnp.h:360
mrpt::vision::pnp::upnp::sign
double sign(const double v)
Return the sign of the scalar.
mrpt::vision::pnp::upnp
Definition: upnp.h:90
row
GLenum GLenum GLvoid * row
Definition: glext.h:3576
mrpt::vision::pnp::upnp::~upnp
~upnp()
Destructor for UPnP class.
mrpt::vision::pnp::upnp::max_nr
int max_nr
Control point variables.
Definition: upnp.h:359
mrpt::vision::pnp::upnp::alphas
std::vector< double > alphas
Image points.
Definition: upnp.h:355
z
GLdouble GLdouble z
Definition: glext.h:3872
mrpt::vision::pnp::upnp::dist2
double dist2(const double *p1, const double *p2)
Compute the euclidean distance squared between two points in 3D.
mrpt::vision::pnp::upnp::find_betas_and_focal_approx_2
void find_betas_and_focal_approx_2(cv::Mat *Ut, cv::Mat *Rho, double *betas, double *efs)
Function to calculate betas and focal length (more accurate)
mrpt::vision::pnp::upnp::fv
double fv
Focal length in x-direction.
Definition: upnp.h:351
mrpt::vision::pnp::upnp::solve_for_sign
void solve_for_sign(void)
Internal member function.
mrpt::vision::pnp::upnp::reprojection_error
double reprojection_error(const double R[3][3], const double t[3])
Compute the reprojection error using the estimated Rotation matrix and Translation Vector.
y
GLenum GLint GLint y
Definition: glext.h:3538
mrpt::vision::pnp::upnp::cws
double cws[4][3]
Number of 2d/3d correspondences.
Definition: upnp.h:358
mrpt::vision::pnp::upnp::compute_constraint_distance_2param_6eq_2unk_f_unk
cv::Mat compute_constraint_distance_2param_6eq_2unk_f_unk(const cv::Mat &M1)
Internal function.
x
GLenum GLint x
Definition: glext.h:3538



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