1 #ifndef P3P_H 2 #define P3P_H 3 4 5 #include "precomp.hpp" 6 7 class p3p 8 { 9 public: 10 p3p(double fx, double fy, double cx, double cy); 11 p3p(cv::Mat cameraMatrix); 12 13 bool solve(cv::Mat& R, cv::Mat& tvec, const cv::Mat& opoints, const cv::Mat& ipoints); 14 int solve(double R[4][3][3], double t[4][3], 15 double mu0, double mv0, double X0, double Y0, double Z0, 16 double mu1, double mv1, double X1, double Y1, double Z1, 17 double mu2, double mv2, double X2, double Y2, double Z2); 18 bool solve(double R[3][3], double t[3], 19 double mu0, double mv0, double X0, double Y0, double Z0, 20 double mu1, double mv1, double X1, double Y1, double Z1, 21 double mu2, double mv2, double X2, double Y2, double Z2, 22 double mu3, double mv3, double X3, double Y3, double Z3); 23 24 private: 25 template <typename T> init_camera_parameters(const cv::Mat & cameraMatrix)26 void init_camera_parameters(const cv::Mat& cameraMatrix) 27 { 28 cx = cameraMatrix.at<T> (0, 2); 29 cy = cameraMatrix.at<T> (1, 2); 30 fx = cameraMatrix.at<T> (0, 0); 31 fy = cameraMatrix.at<T> (1, 1); 32 } 33 template <typename OpointType, typename IpointType> extract_points(const cv::Mat & opoints,const cv::Mat & ipoints,std::vector<double> & points)34 void extract_points(const cv::Mat& opoints, const cv::Mat& ipoints, std::vector<double>& points) 35 { 36 points.clear(); 37 points.resize(20); 38 for(int i = 0; i < 4; i++) 39 { 40 points[i*5] = ipoints.at<IpointType>(i).x*fx + cx; 41 points[i*5+1] = ipoints.at<IpointType>(i).y*fy + cy; 42 points[i*5+2] = opoints.at<OpointType>(i).x; 43 points[i*5+3] = opoints.at<OpointType>(i).y; 44 points[i*5+4] = opoints.at<OpointType>(i).z; 45 } 46 } 47 void init_inverse_parameters(); 48 int solve_for_lengths(double lengths[4][3], double distances[3], double cosines[3]); 49 bool align(double M_start[3][3], 50 double X0, double Y0, double Z0, 51 double X1, double Y1, double Z1, 52 double X2, double Y2, double Z2, 53 double R[3][3], double T[3]); 54 55 bool jacobi_4x4(double * A, double * D, double * U); 56 57 double fx, fy, cx, cy; 58 double inv_fx, inv_fy, cx_fx, cy_fy; 59 }; 60 61 #endif // P3P_H 62