//display an image in a new window with title to be given. void displayImageNewWindow(char* title,CvArr* img){ cvNamedWindow(title, CV_WINDOW_AUTOSIZE ); cvShowImage(title,img); }
int getMaxMin(double value[],int valueSize, int maxmin) { int pos=0; int i=0; double max1=-1;//?-999999; double min1=999999; if (maxmin==1){//find max for (i=0;i if (value[i]>max1){ pos=i; max1=value[i]; } } } if (maxmin==0){//find min for (i=0;i if (value[i] pos=i; min1=value[i]; } } } return pos; }
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* localNCC=new double[DSR];
int x=0, y=0,d=0,m=0; int N=windowSize;
IplImage* leftWinImg=cvCreateImage(cvSize(N,N),32,1);//mySubImage(greyLeftImg32,cvRect(0,0,N,N)); IplImage* rightWinImg=cvCreateImage(cvSize(N,N),32,1);;//mySubImage(greyRightImg32,cvRect(0,0,N,N)); IplImage* disparity=cvCreateImage(cvSize(width,height),8,1);//or IPL_DEPTH_8U BwImage imgA(disparity); for (y=0;y for (x=0;x imgA[y][x]=0; } } CvScalar s1; CvScalar s2; for (y=0;y for (x=0;x //getWindow(i,j,leftim,wl,N); cvSetImageROI(greyLeftImg32, cvRect(x,y,N,N)); s1=cvAvg(greyLeftImg32,NULL); cvSubS(greyLeftImg32,s1,leftWinImg,NULL);//zero-means cvNormalize(leftWinImg,leftWinImg,0,0,CV_L2,NULL); d=0; //initialise localNCC for (m=0;m do{ if (x-d>=0){ cvSetImageROI(greyRightImg32, cvRect(x-d,y,N,N)); s2=cvAvg(greyRightImg32,NULL); cvSubS(greyRightImg32,s2,rightWinImg,NULL);//zero-means cvNormalize(rightWinImg,rightWinImg,0,0,CV_L2,NULL); }else{ break; } localNCC[d]=cvDotProduct(leftWinImg,rightWinImg); cvResetImageROI(greyRightImg32); d++; }while(d<=DSR); //to find the best d and store imgA[y+offset][x+offset]=getMaxMin(localNCC,DSR,1)*16; cvResetImageROI(greyLeftImg32); }//x if (y%10==0)cout<<"row="< }//y cvReleaseImage(&leftWinImg); cvReleaseImage(&rightWinImg); return disparity;