http://www.cnblogs.com/cvlabs/archive/2010/01/13/1646902.html
Normalized Cross correlation (NCC)
NCC(u,v) = [(wl - w)/(|wl - w|)]*[(wr - w)/(|wr - w|)] 選擇最大值
Sum of Squared Defferences (SSD)
SSD(u,v) = Sum{[Left(u,v) - Right(u,v)] * [Left(u,v) - Right(u,v)]} 選擇最大值
Sum of Absolute Defferences (SAD)
SAD(u,v) = Sum{|Left(u,v) - Right(u,v)|} 選擇最小值
先說說SAD算法的基本流程:
1.構造一個小窗口,類似與卷積核。
2.用窗口覆蓋左邊的圖像,選擇出窗口覆蓋區域內的所有像素點。
3.同樣用窗口覆蓋右邊的圖像並選擇出覆蓋區域的像素點。
4.左邊覆蓋區域減去右邊覆蓋區域,並求出所有像素點差的絕對值的和。
5.移動右邊圖像的窗口,重復3,4的動作。(這裡有個搜索范圍,超過這個范圍跳出)
6.找到這個范圍內SAD值最小的窗口,即找到了左邊圖像的最佳匹配的像素塊。
OpenCV代碼示范SAD:
代碼Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1 IplImage* generateDisparityImage(IplImage* greyLeftImg32,
IplImage* greyRightImg32,
int windowSize,int DSR){
int offset=floor((double)windowSize/2);
int height=greyLeftImg32->height;
int width=greyLeftImg32->width;
double* localSAD=new double[DSR];//DSR即搜索范圍
int x=0, y=0,d=0,m=0;
int N=windowSize;
IplImage* winImg=cvCreateImage(cvSize(N,N),32,1);//mySubImage(greyLeftImg32,cvRect(0,0,N,N));
IplImage* disparity=cvCreateImage(cvSize(width,height),8,1);//or IPL_DEPTH_8U
BwImage imgA(disparity);
for (y=0;y<height;y++){
for (x=0;x<width;x++){
imgA[y][x]=0;
}
}
CvScalar sum;
//CvScalar s2;
for (y=0;y<height-N;y++){ //height-N
for (x=0;x<width-N;x++){//width-N
cvSetImageROI(greyLeftImg32, cvRect(x,y,N,N));
d=0;
//initialise localSAD
for (m=0;m<DSR;m++){localSAD[m]=0;}
//start matching
do{
if (x-d>=0){
cvSetImageROI(greyRightImg32, cvRect(x-d,y,N,N));
}else{
break;
}
cvAbsDiff(greyLeftImg32,greyRightImg32,winImg);//absolute difference
sum=cvSum(winImg);//sum
localSAD[d]=sum.val[0];//0 means single channel
cvResetImageROI(greyRightImg32);
d++;
}while(d<=DSR);
//to find the best d and store
imgA[y+offset][x+offset]=getMaxMin(localSAD,DSR,0)*16; //0 means return minimum index
cvResetImageROI(greyLeftImg32);
}//x
if (y%10==0)cout<<"row="<<y<<" of "<<height<<endl;
}//y
cvReleaseImage(&winImg);
//cvReleaseImage(&rightWinImg);
return disparity;
}