方法一:
关键看匹配函数,不同版本对应的函数可能发生变化:
#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_);