opencv寻找轮廓源代码

来源:互联网 发布:原单男装 淘宝店铺 编辑:程序博客网 时间:2024/06/02 07:31

#include "cv.h"                                                                                             

#include "highgui.h"                                                                                        

IplImage* src;                                                                                              

IplImage* img;                                                                                              

IplImage* dst;                                                                                              

CvMemStorage* storage=NULL;                                                                                 

                                                                                       

int thresh=50;                                                                                            

void on_trackbar(int pos)                                                                                   

{         

 CvSeq* contour=0;      

 if(storage==NULL)                                                                                          

 {                                                                                                          

  dst=cvCreateImage(cvGetSize(src),8,3);                                                                    

  storage=cvCreateMemStorage(0);                                                                            

 }                                                                                                          

 else                                                                                                       

 {                                                                                                          

  cvClearMemStorage(storage);                                                                               

 } 

 cvSmooth(src,src,CV_GAUSSIAN,3,3,0,0);

 cvThreshold( src,img,thresh,200,CV_THRESH_BINARY);                                                         

 cvNamedWindow( "threshold",1);                                                                             

 cvShowImage( "threshold", img );                                                                           

 cvFindContours(img,storage,&contour,sizeof(CvContour),CV_RETR_CCOMP,CV_CHAIN_APPROX_NONE,cvPoint(0,0));

 cvZero( dst ); 

 //cvDrawContours(dst,contour,CV_RGB(255,0,0),CV_RGB(0,255,0),-1,1,CV_AA,cvPoint(0,0));

 for( ; contour; contour = contour->h_next )                                                           

 {

  CvRect rect=cvBoundingRect(contour,1);

  CvPoint pt1=cvPoint(rect.x,rect.y),

         pt2=cvPoint(rect.x+rect.width,rect.y+rect.height);

  cvRectangle(dst,pt1,pt2,CV_RGB(255,0,0),1,CV_AA,0);

  cvLine(dst,pt1,pt2,CV_RGB(0,255,0),1,CV_AA,0);

  pt1=cvPoint(rect.x,rect.y+rect.height),

  pt2=cvPoint(rect.x+rect.width,rect.y);

  cvLine(dst,pt1,pt2,CV_RGB(0,255,0),1,CV_AA,0);                    

 }                                                                                                          

 cvShowImage( "Components", dst ); 

}                                                                                                           

int main( int argc, char** argv )                                                                           

{                                                                                                             

                                                                                                            

 if(argc==2&&(src=cvLoadImage(argv[1],0))!=0)                                                               

 {                                                                                                          

  img=cvCreateImage(cvGetSize(src),8,1);                                                                                                                                     

  cvNamedWindow( "Source",1);                                                                               

  cvShowImage( "Source", src );                                                                             

  cvNamedWindow( "Components",1);                                                                           

  cvCreateTrackbar("Threshold","Components",&thresh,200,on_trackbar);                                       

  on_trackbar(0);                                                                                           

  cvWaitKey(0);                                                                                             

  cvDestroyWindow( "sorce" );                                                                               

  cvDestroyWindow( "threshold" );                                                                           

  cvDestroyWindow( "Components" );                                                                          

  cvReleaseImage( &src);                                                                                    

  cvReleaseImage( &img );                                                                                   

  cvReleaseImage(&dst);                                                                                     

  cvReleaseMemStorage(&storage);                                                                            

 }                                                                                                          

}