Main MRPT website > C++ reference for MRPT 1.9.9
p3p.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 #ifndef P3P_H
10 #define P3P_H
11 
12 #include <vector>
13 #include <mrpt/math/types_math.h> // Eigen must be included first via MRPT to enable the plugin system
14 #include <Eigen/Dense>
15 #include <Eigen/SVD>
16 #include <mrpt/otherlibs/do_opencv_includes.h>
17 
18 namespace mrpt
19 {
20 namespace vision
21 {
22 namespace pnp
23 {
24 /** \addtogroup pnp Perspective-n-Point pose estimation
25  * \ingroup mrpt_vision_grp
26  * @{
27  */
28 
29 /**
30  * @class p3p
31  * @author Chandra Mangipudi
32  * @date 11/08/16
33  * @file p3p.h
34  * @brief P3P Pose estimation Algorithm - Eigen Implementation
35  */
36 class p3p
37 {
38  public:
39  //! Constructor for p3p class using C
40  p3p(double fx, double fy, double cx, double cy);
41 
42  //! Constructor using Eigen matrix
43  p3p(Eigen::MatrixXd cam_intrinsic);
44 
45 #if MRPT_HAS_OPENCV
46  //! Constructor using OpenCV matrix
47  p3p(cv::Mat cameraMatrix);
48 
49  /**
50  * @brief Function to compute pose by P3P using OpenCV
51  * @param[out] R Rotation Matrix
52  * @param[out] tvec Translation Vector
53  * @param[in] opoints Object points
54  * @param[in] ipoints Image points
55  * @return Success flag
56  */
57  bool solve(
58  cv::Mat& R, cv::Mat& tvec, const cv::Mat& opoints,
59  const cv::Mat& ipoints);
60 #endif
61  bool solve(
62  Eigen::Ref<Eigen::Matrix3d> R, Eigen::Ref<Eigen::Vector3d> t,
63  Eigen::MatrixXd obj_pts, Eigen::MatrixXd img_pts);
64 
65  /**
66  * @brief Function to compute pose from 3 points using C function
67  * @param[out] R Rotation Matrix
68  * @param[out] t Translation Vector
69  * @param[in] mu0 x- coordinate of image point 1
70  * @param[in] mv0 y- coordinate of image point 1
71  * @param[in] X0 X- coordinate of object point 1
72  * @param[in] Y0 Y- coordinate of object point 1
73  * @param[in] Z0 Z- coordinate of object point 1
74  * @param[in] mu1 x- coordinate of image point 2
75  * @param[in] mv1 y- coordinate of image point 2
76  * @param[in] X1 X- coordinate of object point 2
77  * @param[in] Y1 Y- coordinate of object point 2
78  * @param[in] Z1 Z- coordinate of object point 2
79  * @param[in] mu2 x- coordinate of image point 3
80  * @param[in] mv2 y- coordinate of image point 3
81  * @param[in] X2 X- coordinate of object point 3
82  * @param[in] Y2 Y- coordinate of object point 3
83  * @param[in] Z2 Z- coordinate of object point 3
84  * @return Success flag
85  */
86  int solve(
87  double R[4][3][3], double t[4][3], double mu0, double mv0, double X0,
88  double Y0, double Z0, double mu1, double mv1, double X1, double Y1,
89  double Z1, double mu2, double mv2, double X2, double Y2, double Z2);
90 
91  /**
92  * @brief Function to compute pose from 4 points using C function
93  * @param[out] R Rotation Matrix
94  * @param[out] t Translation Vector
95  * @param[in] mu0 x- coordinate of image point 1
96  * @param[in] mv0 y- coordinate of image point 1
97  * @param[in] X0 X- coordinate of object point 1
98  * @param[in] Y0 Y- coordinate of object point 1
99  * @param[in] Z0 Z- coordinate of object point 1
100  * @param[in] mu1 x- coordinate of image point 2
101  * @param[in] mv1 y- coordinate of image point 2
102  * @param[in] X1 X- coordinate of object point 2
103  * @param[in] Y1 Y- coordinate of object point 2
104  * @param[in] Z1 Z- coordinate of object point 2
105  * @param[in] mu2 x- coordinate of image point 3
106  * @param[in] mv2 y- coordinate of image point 3
107  * @param[in] X2 X- coordinate of object point 3
108  * @param[in] Y2 Y- coordinate of object point 3
109  * @param[in] Z2 Z- coordinate of object point 3
110  * @param[in] mu3 x- coordinate of image point 4
111  * @param[in] mv3 y- coordinate of image point 4
112  * @param[in] X3 X- coordinate of object point 4
113  * @param[in] Y3 Y- coordinate of object point 4
114  * @param[in] Z3 Z- coordinate of object point 4
115  * @return
116  */
117  bool solve(
118  double R[3][3], double t[3], double mu0, double mv0, double X0,
119  double Y0, double Z0, double mu1, double mv1, double X1, double Y1,
120  double Z1, double mu2, double mv2, double X2, double Y2, double Z2,
121  double mu3, double mv3, double X3, double Y3, double Z3);
122 
123  private:
124 #if MRPT_HAS_OPENCV
125  /**
126  * @brief OpenCV Initialization function to access camera intrinsic matrix
127  * @param[in] cameraMatrix Camera Intrinsic Matrix in OpenCV Mat format
128  */
129  template <typename T>
130  void init_camera_parameters(const cv::Mat& cameraMatrix)
131  {
132  cx = cameraMatrix.at<T>(0, 2);
133  cy = cameraMatrix.at<T>(1, 2);
134  fx = cameraMatrix.at<T>(0, 0);
135  fy = cameraMatrix.at<T>(1, 1);
136  }
137 
138  /**
139  * @brief OpoenCV wrapper for extracting object and image points
140  * @param[in] opoints Object points (OpenCV Mat)
141  * @param[in] ipoints Image points (OpenCV Mat)
142  * @param[out] points Combination of object and image points (C structure)
143  */
144  template <typename OpointType, typename IpointType>
145  void extract_points(
146  const cv::Mat& opoints, const cv::Mat& ipoints,
147  std::vector<double>& points)
148  {
149  points.clear();
150  points.resize(20);
151  for (int i = 0; i < 4; i++)
152  {
153  points[i * 5] = ipoints.at<IpointType>(i).x * fx + cx;
154  points[i * 5 + 1] = ipoints.at<IpointType>(i).y * fy + cy;
155  points[i * 5 + 2] = opoints.at<OpointType>(i).x;
156  points[i * 5 + 3] = opoints.at<OpointType>(i).y;
157  points[i * 5 + 4] = opoints.at<OpointType>(i).z;
158  }
159  }
160 #endif
161 
162  /**
163  * @brief Eigen wrapper for extracting object and image points
164  * @param[in] opoints Object points (Eigen Mat)
165  * @param[in] ipoints Image points (Eigen Mat)
166  * @param[out] points Combination of object and image points (C structure)
167  */
169  Eigen::MatrixXd obj_pts, Eigen::MatrixXd img_pts,
170  std::vector<double>& points)
171  {
172  points.clear();
173  points.resize(20);
174  for (int i = 0; i < 4; i++)
175  {
176  points[i * 5] = img_pts(i, 0) * fx + cx;
177  points[i * 5 + 1] = img_pts(i, 1) * fy + cy;
178  points[i * 5 + 2] = obj_pts(i, 0);
179  points[i * 5 + 3] = obj_pts(i, 1);
180  points[i * 5 + 4] = obj_pts(i, 2);
181  }
182  }
183  /**
184  * @brief Function to compute inverse parameters of camera intrinsic matrix
185  */
187 
188  /**
189  * @brief Helper function to @func solve()
190  * @param[out] lengths Internal lengths used for P3P
191  * @param[in] distances Internal distances used for computation of lengths
192  * @param[in] cosines Internal cosines used for computation of lengths
193  * @return
194  */
195  int solve_for_lengths(
196  double lengths[4][3], double distances[3], double cosines[3]);
197  bool align(
198  double M_start[3][3], double X0, double Y0, double Z0, double X1,
199  double Y1, double Z1, double X2, double Y2, double Z2, double R[3][3],
200  double T[3]);
201  /**
202  * @brief Function used to compute the SVD
203  * @param[in] A Input Matrix for which SVD is to be computed
204  * @param[out] D Diagonal Matrix
205  * @param[out] U Matrix of left eigen vectors
206  * @return
207  */
208  bool jacobi_4x4(double* A, double* D, double* U);
209 
210  double fx; //! Focal length x
211  double fy; //! Focal length y
212  double cx; //! Image center x
213  double cy; //! Image center y
214  double inv_fx; //! Inverse of focal length x
215  double inv_fy; //! Inverse of focal length y
216  double cx_fx; //! Inverse of image center point x
217  double cy_fy; //! Inverse of image center point y
218 };
219 
220 /** @} */ // end of grouping
221 }
222 }
223 }
224 
225 #endif // P3P_H
mrpt::vision::pnp::p3p::fx
double fx
Definition: p3p.h:210
mrpt::vision::pnp::p3p::solve
bool solve(Eigen::Ref< Eigen::Matrix3d > R, Eigen::Ref< Eigen::Vector3d > t, Eigen::MatrixXd obj_pts, Eigen::MatrixXd img_pts)
Definition: p3p.cpp:91
mrpt::vision::pnp::p3p::init_inverse_parameters
void init_inverse_parameters()
Function to compute inverse parameters of camera intrinsic matrix.
Definition: p3p.cpp:26
mrpt::vision::pnp::p3p::cx_fx
double cx_fx
Inverse of focal length y.
Definition: p3p.h:216
t
GLdouble GLdouble t
Definition: glext.h:3689
mrpt::vision::pnp::p3p::p3p
p3p(double fx, double fy, double cx, double cy)
Constructor for p3p class using C.
Definition: p3p.cpp:43
points
GLsizei const GLfloat * points
Definition: glext.h:5339
mrpt::vision::pnp::p3p::cx
double cx
Focal length y.
Definition: p3p.h:212
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
R
const float R
Definition: CKinematicChain.cpp:138
mrpt::vision::pnp::p3p::fy
double fy
Focal length x.
Definition: p3p.h:211
mrpt::vision::pnp::p3p::extract_points
void extract_points(Eigen::MatrixXd obj_pts, Eigen::MatrixXd img_pts, std::vector< double > &points)
Eigen wrapper for extracting object and image points.
Definition: p3p.h:168
mrpt::vision::pnp::p3p::inv_fx
double inv_fx
Image center y.
Definition: p3p.h:214
mrpt::vision::pnp::p3p::solve_for_lengths
int solve_for_lengths(double lengths[4][3], double distances[3], double cosines[3])
Helper function to @func solve()
Definition: p3p.cpp:241
mrpt::vision::pnp::p3p::jacobi_4x4
bool jacobi_4x4(double *A, double *D, double *U)
Function used to compute the SVD.
Definition: p3p.cpp:420
mrpt::vision::pnp::p3p
Definition: p3p.h:36
mrpt::vision::pnp::p3p::cy
double cy
Image center x.
Definition: p3p.h:213
z
GLdouble GLdouble z
Definition: glext.h:3872
mrpt::vision::pnp::p3p::inv_fy
double inv_fy
Inverse of focal length x.
Definition: p3p.h:215
mrpt::vision::pnp::p3p::align
bool align(double M_start[3][3], double X0, double Y0, double Z0, double X1, double Y1, double Z1, double X2, double Y2, double Z2, double R[3][3], double T[3])
Definition: p3p.cpp:342
mrpt::vision::pnp::p3p::cy_fy
double cy_fy
Inverse of image center point x.
Definition: p3p.h:217
y
GLenum GLint GLint y
Definition: glext.h:3538
types_math.h
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