Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. void pose_estimation_3d3d (
  2. const vector<Point3f>& pts1,
  3. const vector<Point3f>& pts2,
  4. Mat& R, Mat& t
  5. )
  6. {
  7. Point3f p1, p2; // center of mass
  8. int N = pts1.size();
  9. for ( int i=0; i<N; i++ )
  10. {
  11. p1 += pts1[i];
  12. p2 += pts2[i];
  13. }
  14. p1 = Point3f( Vec3f(p1) / N);
  15. p2 = Point3f( Vec3f(p2) / N);
  16. vector<Point3f> q1 ( N ), q2 ( N ); // remove the center
  17. for ( int i=0; i<N; i++ )
  18. {
  19. q1[i] = pts1[i] - p1;
  20. q2[i] = pts2[i] - p2;
  21. }
  22.  
  23. // compute q1*q2^T
  24. Eigen::Matrix3d W = Eigen::Matrix3d::Zero();
  25. for ( int i=0; i<N; i++ )
  26. {
  27. W += Eigen::Vector3d ( q1[i].x, q1[i].y, q1[i].z ) * Eigen::Vector3d ( q2[i].x, q2[i].y, q2[i].z ).transpose();
  28. }
  29. cout<<"W="<<W<<endl;
  30.  
  31. // SVD on W
  32. Eigen::JacobiSVD<Eigen::Matrix3d> svd ( W, Eigen::ComputeFullU|Eigen::ComputeFullV );
  33. Eigen::Matrix3d U = svd.matrixU();
  34. Eigen::Matrix3d V = svd.matrixV();
  35. cout<<"U="<<U<<endl;
  36. cout<<"V="<<V<<endl;
  37.  
  38. Eigen::Matrix3d R_ = U* ( V.transpose() );
  39. Eigen::Vector3d t_ = Eigen::Vector3d ( p1.x, p1.y, p1.z ) - R_ * Eigen::Vector3d ( p2.x, p2.y, p2.z );
  40.  
  41. // convert to cv::Mat
  42. R = ( Mat_<double> ( 3,3 ) <<
  43. R_ ( 0,0 ), R_ ( 0,1 ), R_ ( 0,2 ),
  44. R_ ( 1,0 ), R_ ( 1,1 ), R_ ( 1,2 ),
  45. R_ ( 2,0 ), R_ ( 2,1 ), R_ ( 2,2 )
  46. );
  47. t = ( Mat_<double> ( 3,1 ) << t_ ( 0,0 ), t_ ( 1,0 ), t_ ( 2,0 ) );
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement