将检测的区域矩形化

#include
#include
#include
#include
#include
#include

#include

#include

using namespace  std;
                                                                               
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((src=cvLoadImage("F:\\XuLiguo Project\\recognition\\cvTest\\light.bmp",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);                                                                            
}                                                                                                          
}   

你可能感兴趣的:(图像处理)