天天看點

如何用Opencv求圖像的灰階投影曲線

   眼睛定位方法中利用灰階投影函數,假設I(x,y)表示點(x,y)處的像素灰階值, 在區間[x1,x2]和[y1,y2] 内的垂直積分投影函數

   int* v = NULL;//垂直投影

   int* h = NULL;//水準投影

   CvScalar s,t;//投影時矩陣的元素

         IplImage* pBinaryImg = NULL;//二值化後圖像

   IplImage* pVerticImg = NULL;//垂直投影圖像

   IplImage* pHorizImg = NULL;//水準投影圖像

   int x,y;//圖像像素坐标                                

                                v=new int[pBinaryImg->width]; 

      h=new int[pBinaryImg->height];

      for(i=0;i<pBinaryImg->width;i++)  

         v[i]=0; 

      for(i=0;i<pBinaryImg->height;i++)  

         h[i]=0;

      for( x=0;x<pBinaryImg->width;x++)      

      {  

         for(y=0;y<pBinaryImg->height;y++)

         {  

            s=cvGet2D(pBinaryImg,y,x);    //t=cvGet2D(paint,y,x);       

            if(s.val[0]==0)    

               v[x]++;     //cvSet2D(paint,y,x,t);        

         }    

      }

      for( y=0;y<pBinaryImg->height;y++)

      {  

         for( x=0;x<pBinaryImg->width;x++)

         {   

            s=cvGet2D(pBinaryImg,y,x);    //t=cvGet2D(paint,y,x);       

            if(s.val[0]==0)    

               h[y]++;         

         }    

      }

      pVerticImg = cvCreateImage( cvGetSize(pBinaryImg),8, 1 );

      pHorizImg = cvCreateImage( cvGetSize(pBinaryImg),8, 1 );

      cvZero(pVerticImg);

      cvZero(pHorizImg);

      for(x=0;x<pBinaryImg->width;x++)

      {  

         for(y=0;y<v[x];y++)

         {   

            t=cvGet2D(pVerticImg,y,x);   //s=cvGet2D(paint,y,x);    //t=cvGet2D(paint,y,x); 

            t.val[0]=255;   

            cvSet2D(pVerticImg,y,x,t);         

         }   

      }

      for(y=0;y<pBinaryImg->height;y++)

      {  

         for(x=0;x<h[y];x++)

         {   

            t=cvGet2D(pHorizImg,y,x);   //s=cvGet2D(paint,y,x);    //t=cvGet2D(paint,y,x); 

            t.val[0]=255;   

            cvSet2D(pHorizImg,y,x,t);        

         }    

      }