DSP

双目视觉的例子(Stereo算法)

2019-07-13 16:15发布

刚才整理了下我以前做过的Stereo算法, 给大家分享下. 在Windows VS2008, 和MacOS下用Xcode, openCV做的.
源码在下边, 如果要直接用的话, 图像路径要改改.

Left tsukuba image: http://graphics.cis.udel.edu/research/C ... reo1/L.jpg
right tsukuba image: http://graphics.cis.udel.edu/research/C ... reo1/R.jpg

大约4,5秒钟 window size=11, disparity search range=20
matching 的算法是Normal Cross Correlation...

(VS2008+OpenCV 1.1)
// cvStereoNCC.cpp : Defines the entry point for the console application. //

#include "stdafx.h"
#include
#include
#include
#include
#include
#include
#include
#include


using namespace std;
template class Image
{
   private: 
      IplImage* imgp;
   public:
      Image(IplImage* img=0){imgp=img;}
      ~Image(){imgp=0;}
      void operator=(IplImage* img){imgp=img;}
      inline T* operator[](const int rowIndx){
         return((T*)(imgp->imageData+rowIndx*imgp->widthStep));}

};

typedef struct{
   unsigned char b,g,r;
}RgbPixel;

typedef struct{
   float b,g,r;
}RgbPixelFloat;

typedef Image RgbImage;
typedef Image RgbImageFloat;
typedef Image BwImage;
typedef Image BwImageFloat;





/*
coded and updated by Huang, Haiqiao 2010-01-07
Normalized Cross Correlation Strereo Vision algorithm
*/

void displayImageProperty(IplImage* image){ 
   cout<<"-------Image Properties--------"<
   cout<<"Image width="<width<
   cout<<"Image height="<height<
   cout<<"Image depth="<depth<
   cout<<"Image nSize="<nSize<
   cout<<"Image nChannels="<nChannels<
   char* origin;
   char* dataOrder;
   if (image->origin==0){
       origin="Top-left";
   }else{
       origin="Below-left";//image->origin=1
   }
   cout<<"Image origin="<

   if (image->dataOrder==0){
       dataOrder="Order_Pixel(Interleaved)";
   }else{
       dataOrder="Order_Plane";//image->dataOrder=1
   }
   cout<<"Image dataOrder="<
   cout<<"Image widthStep="<widthStep<<" Bytes"<


}

//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;

}





int main (int argc, char * const argv[]) {
    // insert code here...
    cout << "Stereo Normalized Cross Correlation"<
    
   //**********image input*********************//
    

   char* filename1="D:/OpenCV_stuff/SampleImages/im2_tsu.bmp";//im2_cone.png
   IplImage* greyLeftImg= cvLoadImage(filename1,0);
   char* filename2="D:/OpenCV_stuff/SampleImages/im6_tsu.bmp";