1 #include <opencv2/calib3d/calib3d.hpp> 3 #ifndef OPENCV_VERSION_3 11 if (_rvec.empty())
return cv::Mat();
12 cv::Mat Matrix=cv::Mat::eye ( 4,4,CV_32FC1 );
13 cv::Mat R33=cv::Mat ( Matrix,cv::Rect ( 0,0,3,3 ) );
14 cv::Rodrigues ( _rvec,R33 );
15 for (
int i=0; i<3; i++ ) Matrix.at<
float> ( i,3 ) =_tvec.ptr<
float> ( 0 ) [i];
21 assert ( M.cols==M.rows && M.cols==4 );
22 assert ( M.type() ==CV_32F || M.type() ==CV_64F );
24 cv::Mat r33=cv::Mat ( M,cv::Rect ( 0,0,3,3 ) );
26 cv::Mat Rpure=svd.u*svd.vt;
27 cv::Rodrigues ( Rpure,R );
28 T.create ( 1,3,M.type() );
29 if ( M.type() ==CV_32F )
30 for (
int i=0; i<3; i++ )
31 T.ptr<
float> ( 0 ) [i]=M.at<
float> ( i,3 );
33 for (
int i=0; i<3; i++ )
34 T.ptr<
double> ( 0 ) [i]=M.at<
double> ( i,3 );
36 #ifndef OPENCV_VERSION_3 39 double __aruco_solve_pnp(
const std::vector<cv::Point3f> & p3d,
const std::vector<cv::Point2f> & p2d,
const cv::Mat &cam_matrix,
const cv::Mat &dist,cv::Mat &r_io,cv::Mat &t_io){
41 assert(r_io.type()==CV_32F);
42 assert(t_io.type()==CV_32F);
43 assert(t_io.total()==r_io.total());
44 assert(t_io.total()==3);
45 auto toSol=[](
const cv::Mat &r,
const cv::Mat &t){
48 sol(i)=r.ptr<
float>(0)[i];
49 sol(i+3)=t.ptr<
float>(0)[i];
57 r.ptr<
float>(0)[i]=sol(i);
58 t.ptr<
float>(0)[i]=sol(i+3);
64 std::vector<cv::Point2f> p2d_rej;
67 cv::projectPoints(p3d,r,t,cam_matrix,dist,p2d_rej,Jacb);
68 err.resize(p3d.size()*2);
70 for(
size_t i=0;i<p3d.size();i++){
71 err(err_idx++)=p2d_rej[i].x-p2d[i].x;
72 err(err_idx++)=p2d_rej[i].y-p2d[i].y;
77 J.resize(p3d.size()*2,6);
78 for(
size_t i=0;i<p3d.size()*2;i++){
79 double *jacb=Jacb.ptr<
double>(i);
80 for(
int j=0;j<6;j++) J(i,j)=jacb[j];
88 auto err=solver.
solve(sol,err_f,jac_f);
90 fromSol(sol,r_io,t_io);
95 double __aruco_solve_pnp(
const std::vector<cv::Point3f> & p3d,
const std::vector<cv::Point2f> & p2d,
const cv::Mat &cam_matrix,
const cv::Mat &dist,cv::Mat &r_io,cv::Mat &t_io){
96 #ifdef DOUBLE_PRECISION_PNP 97 return __aruco_solve_pnp<double>(p3d,p2d,cam_matrix,dist,r_io,t_io);
99 return __aruco_solve_pnp<float>(p3d,p2d,cam_matrix,dist,r_io,t_io);
110 double errorRatio=solutions[1].second/solutions[0].second;
111 if (errorRatio<minerrorRatio)
return false;
113 rv.convertTo(
_rvec,CV_32F);
114 tv.convertTo(
_tvec,CV_32F);
118 #ifdef OPENCV_VERSION_3 120 #else //solve Pnp does not work properly in OpenCV2 143 _cam_params=cam_params;
144 if (!cam_params.isValid())
145 throw cv::Exception(9001,
"Invalid camera parameters",
"MarkerMapPoseTracker::setParams", __FILE__, __LINE__);
147 throw cv::Exception(9001,
"You should indicate the markersize sice the MarkerMap is in pixels",
"MarkerMapPoseTracker::setParams", __FILE__, __LINE__);
149 throw cv::Exception(9001,
"Invlaid MarkerMap",
"MarkerMapPoseTracker::setParams", __FILE__, __LINE__);
151 _msconf=_msconf.convertToMeters(markerSize);
158 _map_mm.insert(make_pair(m.id,m));
164 vector<cv::Point2f> p2d;
165 vector<cv::Point3f> p3d;
166 for(
auto marker:v_m){
167 if ( _map_mm.find(marker.id)!=_map_mm.end()){
168 for(
auto p:marker) p2d.push_back(p);
169 for(
auto p:_map_mm[marker.id]) p3d.push_back(p);
179 cv::solvePnPRansac(p3d,p2d,_cam_params.CameraMatrix,_cam_params.Distorsion,rv,tv);
182 assert(tv.type()==CV_64F);
184 rv.convertTo(
_rvec,CV_32F);
185 tv.convertTo(
_tvec,CV_32F);
189 _rvec.create(1,3,CV_32F);
190 _tvec.create(1,3,CV_32F);
191 for(
int i=0;i<3;i++){
192 _rvec.ptr<
float>(0)[i]=rv.at<
double>(i,0);
193 _tvec.ptr<
float>(0)[i]=tv.at<
double>(i,0);
198 #ifdef OPENCV_VERSION_3 201 #else //SolvePnP does not work properly in opencv 2 cv::Mat getRTMatrix() const
std::vector< cv::Mat > solvePnP(const std::vector< cv::Point3f > &objPoints, const std::vector< cv::Point2f > &imgPoints, cv::InputArray cameraMatrix, cv::InputArray distCoeffs)
void impl__aruco_getRTfromMatrix44(const cv::Mat &M, cv::Mat &R, cv::Mat &T)
static vector< cv::Point3f > get3DPoints(float msize)
double __aruco_solve_pnp(const std::vector< cv::Point3f > &p3d, const std::vector< cv::Point2f > &p2d, const cv::Mat &cam_matrix, const cv::Mat &dist, cv::Mat &r_io, cv::Mat &t_io)
bool estimatePose(Marker &m, const CameraParameters &cam_params, float markerSize, float minErrorRatio=4)
estimatePose
cv::Mat getRTMatrix() const
bool estimatePose(const vector< Marker > &v_m)
void setParams(int maxIters, double minError, double min_step_error_diff=0, double tau=1, double der_epsilon=1e-3)
setParams
void setParams(const CameraParameters &cam_params, const MarkerMap &msconf, float markerSize=-1)
cv::Mat impl__aruco_getRTMatrix(const cv::Mat &_rvec, const cv::Mat &_tvec)
This class represents a marker. It is a vector of the fours corners ot the marker.
Parameters of the camera.
std::vector< std::pair< cv::Mat, double > > solvePnP_(const std::vector< cv::Point3f > &objPoints, const std::vector< cv::Point2f > &imgPoints, cv::InputArray cameraMatrix, cv::InputArray distCoeffs)
double solve(eVector &z, F_z_x, F_z_J)
solve non linear minimization problem ||F(z)||, where F(z)=f(z) f(z)^t
This class defines a set of markers whose locations are attached to a common reference system...