c – OpenCV – 从视差图计算实际距离
发布时间:2020-12-16 07:18:05 所属栏目:百科 来源:网络整理
导读:我在OpenCV 3.1中使用StereoSGBM算法计算了视差图.我校准了立体声相机,RMS误差很小.现在我想计算视差图中某些点的实际距离(mm). 这似乎是一个相对容易的问题.根据我的理解,我可以简单地使用公式 distance = (baseline * focal length) / disparity 我可以使
我在OpenCV 3.1中使用StereoSGBM算法计算了视差图.我校准了立体声相机,RMS误差很小.现在我想计算视差图中某些点的实际距离(mm).
这似乎是一个相对容易的问题.根据我的理解,我可以简单地使用公式 distance = (baseline * focal length) / disparity 我可以使用矩阵Q(stereoRectify输出). Q [2] [3] =焦距,1 / Q [3] [2] =基线. 计算出的Q矩阵是: Q: !!opencv-matrix rows: 4 cols: 4 dt: d data: [ 1.,0.,-1.5668458938598633e+02,1.,-1.1948609733581543e+02,2.3598119491957863e+02,1.6254073321947445e-02,0. ] 问题是结果与现实不符.例如,对于瞄准房间天花板的相机,距离约为. 2.5米(其中视差被正确地计算为12),实际距离计算为1,3米.对于非常近的物体(例如30厘米),它似乎是正确的,但远物体是非常不正确的.在校准过程中,我指定了棋盘方块的确切尺寸,单位为毫米. 我做了以下几点: // compute rectification transforms from calibration data stereoRectify(M1,D1,M2,D2,Size(FRAME_WIDTH,FRAME_HEIGHT),R,T,R1,R2,P1,P2,Q,CALIB_ZERO_DISPARITY,0.0,&roi1,&roi2); // compute the undistortion and rectification transformation maps for each camera initUndistortRectifyMap(M1,CV_16SC2,map11,map12); initUndistortRectifyMap(M2,map21,map22); ... // get images from camera (VideoCapture) camLeft.read(_frameLeft); camRight.read(_frameRight); // remap images using the calibration data remap(_frameLeft,frameLeft,map12,INTER_LINEAR); remap(_frameRight,frameRight,map22,INTER_LINEAR); // compute disparity from undistorted images stereo->compute(frameLeft,disparityMap); ... // compute the real-world distance [mm] float fMaxDistance = static_cast<float>((1. / Q.at<double>(3,2)) * Q.at<double>(2,3)); // outputDisparityValue is single 16-bit value from disparityMap // DISP_SCALE = 16 float fDisparity = outputDisparityValue / (float)StereoMatcher::DISP_SCALE; float fDistance = fMaxDistance / fDisparity; 我做错了吗? 解决方法
我知道OpenCV 2.4中的视差立体算法(stereoBM,stereoSGBM)给出的差异是实际值的16倍,如
in the documentation报道的那样.我没有使用C,openCv3而且我没有找到差异方法SGBM在您的代码中指定,但我认为它可能是相同的.尝试将每个差异值除以16(再次,这对OpenCV 2.4来说肯定是正确的,我不知道3.0版本)
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |