图像的水平与垂直积分投影

测试图片:

 

图像的水平与垂直积分投影_第1张图片

 

代码:


#include "stdafx.h"
#include <cv.h>
#include <highgui.h>
int _tmain(int argc, _TCHAR* argv[])
{
 IplImage * pImg=cvLoadImage("c://test.jpg",0);
 cvNamedWindow("ShowImage",1);
 cvNamedWindow("result",1);
 cvNamedWindow("result_h",1);
 CvScalar s,t;
 cvSmooth( pImg, pImg, CV_BLUR, 3, 3, 0, 0 );
 cvThreshold( pImg, pImg ,50, 255, CV_THRESH_BINARY_INV ); //取阀值为50把图像转为二值图像
 int* v=new int[pImg->width];
 int* h=new int[pImg->height];
 for(int i=0;i<pImg->width;i++)
  v[i]=0;
 for(int i=0;i<pImg->height;i++)
  h[i]=0;
 for(int x=0;x<pImg->width;x++){
  for(int y=0;y<pImg->height;y++){
   s=cvGet2D(pImg,y,x);
    ///t=cvGet2D(paint,y,x);
    
   if(s.val[0]==0)
    v[x]++;
     //cvSet2D(paint,y,x,t);
   
    
  }
   
 }
 for(int y=0;y<pImg->height;y++){
  for(int x=0;x<pImg->width;x++){
   s=cvGet2D(pImg,y,x);
    ///t=cvGet2D(paint,y,x);
    
   if(s.val[0]==0)
    h[y]++;
   
    
  }
   
 }
 IplImage* paint=cvCreateImage( cvGetSize(pImg),IPL_DEPTH_8U, 1 );
 IplImage* painty=cvCreateImage( cvGetSize(pImg),IPL_DEPTH_8U, 1 );
 
 cvZero(paint);
 cvZero(painty);
 for(int x=0;x<pImg->width;x++){
  for(int y=0;y<v[x];y++){
   t=cvGet2D(paint,y,x);
   //s=cvGet2D(paint,y,x);
    ///t=cvGet2D(paint,y,x);
    
   t.val[0]=255;
   cvSet2D(paint,y,x,t);
   
    
  }
   
 }
 //----------h------------
 
 
 
 for(int y=0;y<pImg->height;y++){
  for(int x=0;x<h[y];x++){
   t=cvGet2D(painty,y,x);
   //s=cvGet2D(paint,y,x);
    ///t=cvGet2D(paint,y,x);
    
   t.val[0]=255;
   cvSet2D(painty,y,x,t);
   
    
  }
   
 }
 cvShowImage("result",paint);
 cvSaveImage("c://tou.jpg",paint);
 cvShowImage("result_h",painty);
 cvShowImage("ShowImage",pImg);
 cvWaitKey(0);
 cvDestroyWindow("ShowImage");
 
 return 0;
}

你可能感兴趣的:(c,测试)