该函数是用来校正图像,并输出视觉映射,结构如下所示
void cvFindStereoCorrespondenceBM(const CvArr *leftImage,
const CvArr *rightImage,
CvArr *disparityResult,
Cv StereoBMState *BMState
)
const CvArr *leftImage代表是输入左边图像,
const CvArr *rightImage当然就是输入右边图像,
注意两幅图像都要求是单通道的,如果不是可以通过单通道灰度图像显示来使用或者利用函数cvCvtColor来转换为单通道灰度图像后再使用,此函数百度有详细说就不多说。
CvArr *disparityResult指输出视觉映射,一般定义是cvmat类型,并且长宽都应该与输入的左右图像一致,为浮点型,譬如CvMat* disparity = cvCreateMat(img_l->height, img_l->width, CV_32FC1);
Cv StereoBMState *BMState,是BM算法,这个设置算法,学习Opencv里面有例子,譬如
typedef struct CvStereoBMState
{
//pre filters (normalize input images):
int preFilterType; // 0 for now
int preFilterSize; // ~5x5..21x21
int preFilterCap; // up to ~31
//correspondence using Sum of Absolute Difference (SAD):
int SADWindowSize; // Could be 5x5,7x7...21x21
int minDisparity; // minimum disparity (=0)
int numberOfDisparities; // maximum disparity - minimum disparity, numbers of pixels to search
//post filters (knock out bad matches):
int textureThreshold; // areas with no texture are ignored
float uniquenessRatio;// filter out pixels if there are other close matches
// with different disparity
int speckleWindowSize; // the maximum area of speckles to remove
// (set to 0 to disable speckle filtering)
int speckleRange; // acceptable range of disparity variation in each connected component
// internal data
...
}CvStereoBMState;
下面代码是一个例子:
// 12-4.cpp : 定义控制台应用程序的入口点。
//
/*=========================================================================
名称:cvFindStereoCorrespondenceBM函数使用
时间:2013.07.13
说明:cvFindStereoCorrespondenceBM使用
=========================================================================*/
#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
#include "cxcore.h"
#include "math.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
IplImage* img_r, *img_l; //定义两个图像指针
img_r = cvLoadImage("./48.jpg", 0); //图像指针初始化
img_l = cvLoadImage(".//49.jpg", 0); //图像指针初始化
cvNamedWindow("img",0); //创建显示图像窗口
CvMat* disparity = cvCreateMat(img_l->height, img_l->width, CV_32FC1);//声明一个矩阵,大小与加载图像一致,为浮点型
CvMat* norm_disparity=cvCreateMat(img_l->height,img_l->width,CV_8U);//声明一个矩阵,大小与加载图像一致,为无符号型
//BM算法初始化
CvStereoBMState* BMState = cvCreateStereoBMState();
BMState->SADWindowSize=25;
BMState->minDisparity=2;
BMState->numberOfDisparities=16;
cvFindStereoCorrespondenceBM(img_l, img_r, disparity, BMState); //校正图像
cvNormalize(disparity, norm_disparity, 45, 160, CV_MINMAX, NULL); //图像归一化
//图像显示
cvShowImage("img", norm_disparity);
cvWaitKey(0);
//释放所有图像指针占内存与矩阵所占内存,关闭窗口
cvDestroyAllWindows();
cvReleaseImage(&img_l);
cvReleaseImage(&img_r);
cvReleaseMat(&disparity);
cvReleaseMat(&norm_disparity);
return 0;
}