opencv寻找轮廓源代码

本文深入探讨了使用OpenCV进行图像处理和计算机视觉应用的技术,包括阈值化、轮廓检测、边界框绘制等核心操作,旨在帮助读者理解和实现复杂图像分析任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#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);                                                                            

 }                                                                                                          

}         

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值