OpenCV更新到3.0版本后,Stereo模块变化的挺多的,首先去掉了StereoBMState和StereoSGBMState这两个专门控制BM和SGBM算法参数的类,而且StereoBM不能直接声明实例,需要放到智能指针里cv::Ptr<cv::StereoBM>声明才行,而且不能直接通过访问state来访问参数,只能通过setter和getter方法来设置和获取参数,参考下面这段实例代码:
cv::Ptr<cv::StereoBM> bm = cv::StereoBM::create(16,9); // setter bm->setPreFilterType(CV_STEREO_BM_NORMALIZED_RESPONSE); bm->setPreFilterSize(9); bm->setPreFilterCap(31); bm->setBlockSize(21); bm->setMinDisparity(-16); bm->setNumDisparities(64); bm->setTextureThreshold(10); bm->setUniquenessRatio(5); bm->setSpeckleWindowSize(100); bm->setSpeckleRange(32); bm->setROI1(roi_left); bm->setROI2(roi_right); // getter int pfs = bm->getPreFilterSize(); int pfc = bm->getPreFilterCap(); int bs = bm->getBlockSize(); int md = bm->getMinDisparity(); int nd = bm->getNumDisparities(); int tt = bm->getTextureThreshold(); int ur = bm->getUniquenessRatio(); int sw = bm->getSpeckleWindowSize(); int sr = bm->getSpeckleRange(); // Compute disparity bm->compute(left, right, disp);
注意新版本下的BM和SGBM方法计算出的视差都是CV_16S格式的,使用32位float格式可以得到真实的视差值,所以我们需要除以16,需要加上下面一行代码:
// Convert disp.convertTo(disp, CV_32F, 1.0/16);
还有就是我怎么在3.1版本下找不到GC算法了,难道是嫌速度慢删去了?新加了StereoBeliefPropagation算法,但是需要用Cuda运行的,有机会一定要试试看效果如何~