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::vision::pnp
73 {
74 /** \addtogroup pnp Perspective-n-Point pose estimation
75  * \ingroup mrpt_vision_grp
76  * @{
77  */
78 
79 /**
80  * @class upnp
81  * @author Chandra Mangipudi
82  * @date 12/08/16
83  * @file upnp.h
84  * @brief Unified PnP - Eigen Wrapper for OpenCV function
85  */
86 class upnp
87 {
88  public:
89  //! Constructor for UPnP class
90  upnp(
91  const cv::Mat& cameraMatrix, const cv::Mat& opoints,
92  const cv::Mat& ipoints);
93 
94  //! Destructor for UPnP class
95  ~upnp();
96 
97  /**
98  * @brief Function to compute pose
99  * @param[out] R Rotation Matrix
100  * @param[out] t Translation Vector
101  * @return
102  */
103  double compute_pose(cv::Mat& R, cv::Mat& t);
104 
105  private:
106  /**
107  * @brief Initialize camera variables using camera intrinsic matrix
108  * @param[in] cameraMatrix Camera Intrinsic Matrix
109  */
110  template <typename T>
111  void init_camera_parameters(const cv::Mat& cameraMatrix)
112  {
113  uc = cameraMatrix.at<T>(0, 2);
114  vc = cameraMatrix.at<T>(1, 2);
115  fu = 1;
116  fv = 1;
117  }
118 
119  /**
120  * @brief Iniialize Object points and image points from OpenCV Matrix
121  * @param[in] opoints Object Points
122  * @param[in] ipoints Image Points
123  */
124  template <typename OpointType, typename IpointType>
125  void init_points(const cv::Mat& opoints, const cv::Mat& ipoints)
126  {
127  for (int i = 0; i < number_of_correspondences; i++)
128  {
129  pws[3 * i] = opoints.at<OpointType>(i).x;
130  pws[3 * i + 1] = opoints.at<OpointType>(i).y;
131  pws[3 * i + 2] = opoints.at<OpointType>(i).z;
132 
133  us[2 * i] = ipoints.at<IpointType>(i).x;
134  us[2 * i + 1] = ipoints.at<IpointType>(i).y;
135  }
136  }
137 
138  /**
139  * @brief Compute the reprojection error using the estimated Rotation matrix
140  * and Translation Vector
141  * @param[in] R Rotation matrix
142  * @param[in] t Trnaslation Vector
143  * @return Linear least squares error in pixels
144  */
145  double reprojection_error(const double R[3][3], const double t[3]);
146 
147  /**
148  * @brief Function to select 4 control points
149  */
150  void choose_control_points();
151 
152  /**
153  * @brief Function to comput @alphas
154  */
155  void compute_alphas();
156 
157  /**
158  * @brief Function to compute Maucaulay matrix M
159  * @param[out] M Maucaulay matrix
160  * @param[in] row Internal member
161  * @param[in] alphas Internal member
162  * @param[in] u Image pixel x co-ordinate
163  * @param[in] v Image pixel y co-ordinate
164  */
165  void fill_M(
166  cv::Mat* M, const int row, const double* alphas, const double u,
167  const double v);
168 
169  /**
170  * @brief Compute the control points
171  * @param[in] betas Internal member variable
172  * @param[in] ut Internal member variable
173  */
174  void compute_ccs(const double* betas, const double* ut);
175 
176  /**
177  * @brief Compute object points based on control points
178  */
179  void compute_pcs(void);
180 
181  /**
182  * @brief Internal member function
183  */
184  void solve_for_sign(void);
185 
186  /**
187  * @brief Function to approximately calculate betas and focal length
188  * @param[in] Ut
189  * @param[in] Rho
190  * @param[out] betas
191  * @param[out] efs
192  */
194  cv::Mat* Ut, cv::Mat* Rho, double* betas, double* efs);
195 
196  /**
197  * @brief Function to calculate betas and focal length (more accurate)
198  * @param[in] Ut
199  * @param[in] Rho
200  * @param[out] betas
201  * @param[out] efs
202  */
204  cv::Mat* Ut, cv::Mat* Rho, double* betas, double* efs);
205 
206  /**
207  * @brief Function to do a QR decomposition
208  * @param[in] A Matrix to be decomposed
209  * @param[out] b
210  * @param[out] X
211  */
212  void qr_solve(cv::Mat* A, cv::Mat* b, cv::Mat* X);
213 
214  /**
215  * @brief Internal function
216  * @param[in] M1
217  * @return Distance
218  */
220  const cv::Mat& M1);
221 
222  /**
223  * @brief Internal function
224  * @param[in] M1
225  * @param[in] M2
226  * @return Distance
227  */
229  const cv::Mat& M1, const cv::Mat& M2);
230 
231  /**
232  * @brief Get all possible solutions
233  * @param[in] betas
234  * @param[out] solutions
235  */
237  const double betas[5], double solutions[18][3]);
238 
239  /**
240  * @brief Return the sign of the scalar
241  * @param[in] v
242  * @return True if positive else Flase
243  */
244  double sign(const double v);
245 
246  /**
247  * @brief Compute the dot product between two vectors
248  * @param[in] v1
249  * @param[in] v2
250  * @return Dot product
251  */
252  double dot(const double* v1, const double* v2);
253 
254  /**
255  * @brief Compute dot product in 2D with only x and y components
256  * @param[in] v1
257  * @param[in] v2
258  * @return Dot product
259  */
260  double dotXY(const double* v1, const double* v2);
261 
262  /**
263  * @brief Compute the dot product using only z component
264  * @param[in] v1
265  * @param[in] v2
266  * @return Dot product
267  */
268  double dotZ(const double* v1, const double* v2);
269 
270  /**
271  * @brief Compute the euclidean distance squared between two points in 3D
272  * @param[in] p1
273  * @param[in] p2
274  * @return Distance squared
275  */
276  double dist2(const double* p1, const double* p2);
277 
278  /**
279  * @brief Internal fucntion
280  * @param[out] rho
281  */
282  void compute_rho(double* rho);
283 
284  /**
285  * @brief Internal function
286  * @param[in] ut
287  * @param[out] l_6x12
288  */
289  void compute_L_6x12(const double* ut, double* l_6x12);
290 
291  /**
292  * @brief Gauss Newton Iterative optimization
293  * @param[in] L_6x12
294  * @param[in] Rho
295  * @param[in,out] current_betas
296  * @param[out] efs
297  */
298  void gauss_newton(
299  const cv::Mat* L_6x12, const cv::Mat* Rho, double current_betas[4],
300  double* efs);
301 
302  /**
303  * @brief Compute matrix A and vector b
304  * @param[in] l_6x12
305  * @param[in] rho
306  * @param[in] cb
307  * @param[out] A
308  * @param[out] b
309  * @param[in] f
310  */
312  const double* l_6x12, const double* rho, const double cb[4], cv::Mat* A,
313  cv::Mat* b, double const f);
314 
315  /**
316  * @brief Function to compute the pose
317  * @param[in] ut
318  * @param[in] betas
319  * @param[out] R Rotation Matrix
320  * @param[out] t Translation VectorS
321  * @return
322  */
323  double compute_R_and_t(
324  const double* ut, const double* betas, double R[3][3], double t[3]);
325 
326  /**
327  * @brief Helper function to function compute_R_and_t()
328  * @param[out] R Rotaiton matrix
329  * @param[out] t Translation vector
330  */
331  void estimate_R_and_t(double R[3][3], double t[3]);
332 
333  /**
334  * @brief Function to copy the pose
335  * @param[in] R_dst
336  * @param[in] t_dst
337  * @param[out] R_src
338  * @param[out] t_src
339  */
340  void copy_R_and_t(
341  const double R_dst[3][3], const double t_dst[3], double R_src[3][3],
342  double t_src[3]);
343 
344  double uc; //! Image center in x-direction
345  double vc; //! Image center in y-direction
346  double fu; //! Focal length in x-direction
347  double fv; //! Focal length in y-direction
348 
349  std::vector<double> pws; //! Object points
350  std::vector<double> us; //! Image points
351  std::vector<double> alphas, pcs; //! Internal variable
352  int number_of_correspondences; //! Number of 2d/3d correspondences
353 
354  double cws[4][3], ccs[4][3]; //! Control point variables
355  int max_nr; //! Internal variable
356  double *A1, *A2; //! Internal variable
357 };
358 
359 /** @} */ // end of grouping
360 }
361 #endif // Check for OPENCV_LIB
362 #endif // OPENCV_CALIB3D_UPNP_H_
363 
364 
double compute_R_and_t(const double *ut, const double *betas, double R[3][3], double t[3])
Function to compute the pose.
void fill_M(cv::Mat *M, const int row, const double *alphas, const double u, const double v)
Function to compute Maucaulay matrix M.
GLdouble GLdouble t
Definition: glext.h:3689
GLdouble GLdouble z
Definition: glext.h:3872
void init_points(const cv::Mat &opoints, const cv::Mat &ipoints)
Iniialize Object points and image points from OpenCV Matrix.
Definition: upnp.h:125
void estimate_R_and_t(double R[3][3], double t[3])
Helper function to function compute_R_and_t()
double ccs[4][3]
Definition: upnp.h:354
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.
Perspective n Point (PnP) Algorithms toolkit for MRPT mrpt_vision_grp.
Definition: pnp_algos.h:23
std::vector< double > pws
Focal length in y-direction.
Definition: upnp.h:349
double cws[4][3]
Number of 2d/3d correspondences.
Definition: upnp.h:354
void compute_pcs(void)
Compute object points based on control points.
void compute_ccs(const double *betas, const double *ut)
Compute the control points.
void compute_alphas()
Function to comput .
std::vector< double > pcs
Definition: upnp.h:351
int number_of_correspondences
Internal variable.
Definition: upnp.h:352
double dotXY(const double *v1, const double *v2)
Compute dot product in 2D with only x and y components.
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.
double compute_pose(cv::Mat &R, cv::Mat &t)
Function to compute pose.
std::vector< double > alphas
Image points.
Definition: upnp.h:351
void qr_solve(cv::Mat *A, cv::Mat *b, cv::Mat *X)
Function to do a QR decomposition.
double dot(const double *v1, const double *v2)
Compute the dot product between two vectors.
double dotZ(const double *v1, const double *v2)
Compute the dot product using only z component.
upnp(const cv::Mat &cameraMatrix, const cv::Mat &opoints, const cv::Mat &ipoints)
Constructor for UPnP class.
void generate_all_possible_solutions_for_f_unk(const double betas[5], double solutions[18][3])
Get all possible solutions.
double dist2(const double *p1, const double *p2)
Compute the euclidean distance squared between two points in 3D.
GLubyte GLubyte b
Definition: glext.h:6279
cv::Mat compute_constraint_distance_3param_6eq_6unk_f_unk(const cv::Mat &M1, const cv::Mat &M2)
Internal function.
double sign(const double v)
Return the sign of the scalar.
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.
void init_camera_parameters(const cv::Mat &cameraMatrix)
Initialize camera variables using camera intrinsic matrix.
Definition: upnp.h:111
const GLdouble * v
Definition: glext.h:3678
GLfloat GLfloat v1
Definition: glext.h:4105
const float R
std::vector< double > us
Object points.
Definition: upnp.h:350
GLenum GLenum GLvoid * row
Definition: glext.h:3576
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)
int max_nr
Control point variables.
Definition: upnp.h:355
double vc
Image center in x-direction.
Definition: upnp.h:345
GLenum GLint GLint y
Definition: glext.h:3538
~upnp()
Destructor for UPnP class.
GLfloat GLfloat GLfloat v2
Definition: glext.h:4107
GLenum GLint x
Definition: glext.h:3538
cv::Mat compute_constraint_distance_2param_6eq_2unk_f_unk(const cv::Mat &M1)
Internal function.
void compute_rho(double *rho)
Internal fucntion.
void compute_L_6x12(const double *ut, double *l_6x12)
Internal function.
double reprojection_error(const double R[3][3], const double t[3])
Compute the reprojection error using the estimated Rotation matrix and Translation Vector...
void choose_control_points()
Function to select 4 control points.
void solve_for_sign(void)
Internal member function.
double * A1
Internal variable.
Definition: upnp.h:356
double fu
Image center in y-direction.
Definition: upnp.h:346
void gauss_newton(const cv::Mat *L_6x12, const cv::Mat *Rho, double current_betas[4], double *efs)
Gauss Newton Iterative optimization.
double fv
Focal length in x-direction.
Definition: upnp.h:347



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