Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. Point2f pixel2cam ( const Point2d& p, const Mat& K )
  2. {
  3. return Point2f
  4. (
  5. ( p.x - K.at<double>(0,2) ) / K.at<double>(0,0),
  6. ( p.y - K.at<double>(1,2) ) / K.at<double>(1,1)
  7. );
  8. }
  9.  
  10. /*
  11. *intrinsic: 3x3 double matrix
  12. */
  13. void triangulation ( const Mat & intrinsic,
  14. const vector< KeyPoint >& keypoint_1,
  15. const vector< KeyPoint >& keypoint_2,
  16. const std::vector< DMatch >& matches,
  17. const Mat& R, const Mat& t,
  18. vector< Point3d >& points )
  19. {
  20. Mat T1 = (Mat_<float> (3,4) <<
  21. 1,0,0,0,
  22. 0,1,0,0,
  23. 0,0,1,0);
  24. Mat T2 = (Mat_<float> (3,4) <<
  25. R.at<double>(0,0), R.at<double>(0,1), R.at<double>(0,2), t.at<double>(0,0),
  26. R.at<double>(1,0), R.at<double>(1,1), R.at<double>(1,2), t.at<double>(1,0),
  27. R.at<double>(2,0), R.at<double>(2,1), R.at<double>(2,2), t.at<double>(2,0)
  28. );
  29.  
  30. // Mat K = ( Mat_<double> ( 3,3 ) << 520.9, 0, 325.1, 0, 521.0, 249.7, 0, 0, 1 );
  31. vector<Point2f> pts_1, pts_2;
  32. for ( DMatch m:matches )
  33. {
  34. // 将像素坐标转换至相机坐标
  35. pts_1.push_back ( pixel2cam( keypoint_1[m.queryIdx].pt, intrinsic) );
  36. pts_2.push_back ( pixel2cam( keypoint_2[m.trainIdx].pt, intrinsic) );
  37. }
  38.  
  39. Mat pts_4d;
  40. cv::triangulatePoints( T1, T2, pts_1, pts_2, pts_4d );
  41.  
  42. // 转换成非齐次坐标
  43. for ( int i=0; i<pts_4d.cols; i++ )
  44. {
  45. Mat x = pts_4d.col(i);
  46. x /= x.at<float>(3,0); // 归一化
  47. Point3d p (
  48. x.at<float>(0,0),
  49. x.at<float>(1,0),
  50. x.at<float>(2,0)
  51. );
  52. points.push_back( p );
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement