【OpenCV开发之五】立体匹配

方法一:

关键看匹配函数,不同版本对应的函数可能发生变化:

#include "cv.h"
#include "cvaux.h"
#include "cxcore.h"
#include "highgui.h"
//reference: OpenCV1.1 cvref.htm
//by Gong Peiliang in SIA, CAS;
//11-11 2017
int main( int argc, char** argv)
{
IplImage * cv_left_rectified;
IplImage * cv_right_rectified;

//note the sequence of the stereo pairs
cv_left_rectified = cvLoadImage( "tsukuba2color.png", CV_LOAD_IMAGE_GRAYSCALE);
cv_right_rectified = cvLoadImage( "tsukuba1color.png", CV_LOAD_IMAGE_GRAYSCALE);
CvSize size = cvGetSize( cv_left_rectified );
//the disparity map is an array h*w, with 16bit signed elements.
CvMat* disparity_left = cvCreateMat( size.height, size.width, CV_16S );
CvMat* disparity_right = cvCreateMat( size.height, size.width, CV_16S );
CvStereoGCState* state = cvCreateStereoGCState( 16, 2 );
// 匹配
cvFindStereoCorrespondenceGC( cv_left_rectified,
  cv_right_rectified,
  disparity_left,
  disparity_right,
  state,
  0 );
cvReleaseStereoGCState( &state );
    //post-progressing the result
CvMat* disparity_left_visual = cvCreateMat( size.height, size.width, CV_8U );
cvConvertScale( disparity_left, disparity_left_visual, -16 );
cvSave( "disparity.png", disparity_left_visual );

cvNamedWindow( "disparity", 1);
cvShowImage("disparity", disparity_left_visual );
cvWaitKey( 0 );
cvDestroyWindow( "disparity" );
return 0;
}

方法二:

	//【2】立体匹配
	Mat left_disp_;
	Mat left_vdisp;
	Mat right_disp_;

	left_disp_ = Mat::zeros(srcImage1.rows, srcImage1.cols, CV_32F);
	right_disp_ = Mat::zeros(srcImage1.rows, srcImage1.cols, CV_32F);
	left_vdisp = Mat::zeros(srcImage1.rows, srcImage1.cols, CV_8U);
	// 方法二
	CvStereoBMState *BMState = cvCreateStereoBMState();
	int SADWindowSize = 15;
	BMState->SADWindowSize = SADWindowSize > 0 ? SADWindowSize : 9;
	BMState->minDisparity = 0;
	BMState->numberOfDisparities = 32;
	BMState->textureThreshold = 10;
	BMState->uniquenessRatio = 15;
	BMState->speckleWindowSize = 100;
	BMState->speckleRange = 32;
	BMState->disp12MaxDiff = 1;
	// 匹配
	cvFindStereoCorrespondenceBM(&(IplImage)srcImage1, &(IplImage)srcImage2, &(IplImage)left_disp_, BMState);
	cvNormalize(&(IplImage)left_disp_, &(IplImage)left_vdisp, 0, 255, CV_MINMAX);


方法三:

	// 方法三
	cv::StereoSGBM sgbm;
	sgbm.preFilterCap = 63;
	int SADWindowSize = 11;
	int cn = 1;
	sgbm.SADWindowSize = SADWindowSize > 0 ? SADWindowSize : 3;
	sgbm.P1 = 4 * cn*sgbm.SADWindowSize*sgbm.SADWindowSize;
	sgbm.P2 = 32 * cn*sgbm.SADWindowSize*sgbm.SADWindowSize;
	sgbm.minDisparity = 0;
	sgbm.numberOfDisparities = 32;
	sgbm.uniquenessRatio = 10;
	sgbm.speckleWindowSize = 100;
	sgbm.speckleRange = 32;
	sgbm.disp12MaxDiff = 1;
	// 匹配
	sgbm(srcImage1, srcImage2, left_disp_);
	sgbm(srcImage2, srcImage1, right_disp_);


经测试,三种方法在时间上和匹配效果上进行对比。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值