Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Point2f pixel2cam ( const Point2d& p, const Mat& K )
- {
- return Point2f
- (
- ( p.x - K.at<double>(0,2) ) / K.at<double>(0,0),
- ( p.y - K.at<double>(1,2) ) / K.at<double>(1,1)
- );
- }
- /*
- *intrinsic: 3x3 double matrix
- */
- void triangulation ( const Mat & intrinsic,
- const vector< KeyPoint >& keypoint_1,
- const vector< KeyPoint >& keypoint_2,
- const std::vector< DMatch >& matches,
- const Mat& R, const Mat& t,
- vector< Point3d >& points )
- {
- Mat T1 = (Mat_<float> (3,4) <<
- 1,0,0,0,
- 0,1,0,0,
- 0,0,1,0);
- Mat T2 = (Mat_<float> (3,4) <<
- R.at<double>(0,0), R.at<double>(0,1), R.at<double>(0,2), t.at<double>(0,0),
- R.at<double>(1,0), R.at<double>(1,1), R.at<double>(1,2), t.at<double>(1,0),
- R.at<double>(2,0), R.at<double>(2,1), R.at<double>(2,2), t.at<double>(2,0)
- );
- // Mat K = ( Mat_<double> ( 3,3 ) << 520.9, 0, 325.1, 0, 521.0, 249.7, 0, 0, 1 );
- vector<Point2f> pts_1, pts_2;
- for ( DMatch m:matches )
- {
- // 将像素坐标转换至相机坐标
- pts_1.push_back ( pixel2cam( keypoint_1[m.queryIdx].pt, intrinsic) );
- pts_2.push_back ( pixel2cam( keypoint_2[m.trainIdx].pt, intrinsic) );
- }
- Mat pts_4d;
- cv::triangulatePoints( T1, T2, pts_1, pts_2, pts_4d );
- // 转换成非齐次坐标
- for ( int i=0; i<pts_4d.cols; i++ )
- {
- Mat x = pts_4d.col(i);
- x /= x.at<float>(3,0); // 归一化
- Point3d p (
- x.at<float>(0,0),
- x.at<float>(1,0),
- x.at<float>(2,0)
- );
- points.push_back( p );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement