reprojectImageTo3D() in OpenCV

前端 未结 1 1019
小鲜肉
小鲜肉 2020-12-07 23:29

I\'ve been trying to compute real world coordinates of points from a disparity map using the reprojectImageTo3D() function provided by OpenCV, but the output seems to be inc

相关标签:
1条回答
  • 2020-12-08 00:05

    Your code seems fine to me. It could be a bug with the reprojectImageTo3D. Try to replace it with the following code (which has the same role):

    cv::Mat_<cv::Vec3f> XYZ(disparity32F.rows,disparity32F.cols);   // Output point cloud
    cv::Mat_<float> vec_tmp(4,1);
    for(int y=0; y<disparity32F.rows; ++y) {
        for(int x=0; x<disparity32F.cols; ++x) {
            vec_tmp(0)=x; vec_tmp(1)=y; vec_tmp(2)=disparity32F.at<float>(y,x); vec_tmp(3)=1;
            vec_tmp = Q*vec_tmp;
            vec_tmp /= vec_tmp(3);
            cv::Vec3f &point = XYZ.at<cv::Vec3f>(y,x);
            point[0] = vec_tmp(0);
            point[1] = vec_tmp(1);
            point[2] = vec_tmp(2);
        }
    }
    

    I never used reprojectImageTo3D, however I am using successfully code similar to the snippet above.

    [Initial answer]

    As it is explained in the documentation for StereoBM, if you request a CV_16S disparity map, you have to divide each disparity value by 16 before using them.

    Hence, you should convert the disparity map as follows before using it:

    imgDisparity16S.convertTo( imgDisparity32F, CV_32F, 1./16);
    

    You can also directly request a CV_32F disparity map from the StereoBM structure, in which case you directy get the true disparities.

    0 讨论(0)
提交回复
热议问题