在OpenCV中利用鼠標繪製矩形和截取圖像的矩形區域

這是兩個相關的程序,前者是後者的基礎。實際上前一個程序也是在前面博文的基礎上作的修改,請參考《在OpenCV中利用鼠標繪製直線》 。下面貼出代碼。web

程序之一,在OpenCV中利用鼠標繪製矩形ui

#include "stdafx.h"
#include <cv.h>  
#include <highgui.h>  
#include <stdio.h> 
//#pragma comment( lib, "cv.lib" )  
//#pragma comment( lib, "cxcore.lib" )  
//#pragma comment( lib, "highgui.lib" ) 
#pragma comment(lib, "opencv_core247.lib" )   
#pragma comment(lib, "opencv_core247d.lib" )   
#pragma comment(lib, "opencv_highgui247.lib" )  
#pragma comment(lib, "opencv_highgui247d.lib" )
IplImage* src = 0;       
IplImage* dst = 0;         
void on_mouse( int event, int x, int y, int flags, void* ustc)                        
{                
 static CvPoint pre_pt = {-1,-1};          
 static CvPoint cur_pt = {-1,-1};      
 CvFont font;           
 cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5, 0, 2, CV_AA); 
 char temp[16];  
 if( event == CV_EVENT_LBUTTONDOWN ) 
 {                                                                                         cvCopy(dst,src); 
  sprintf(temp,"(%d,%d)",x,y);
  pre_pt = cvPoint(x,y); 
  cvPutText(src,temp, pre_pt, &font, cvScalar(0,0, 0, 255));
  cvCircle( src, pre_pt, 3,cvScalar(255,0,0,0) ,CV_FILLED, CV_AA, 0 ); 
  cvShowImage( "src", src );
  cvCopy(src,dst);   
 }           
 else if( event == CV_EVENT_MOUSEMOVE && !(flags & CV_EVENT_FLAG_LBUTTON))
 {                              
  cvCopy(dst,src);         
  sprintf(temp,"(%d,%d)",x,y);
  cur_pt = cvPoint(x,y);     
  cvPutText(src,temp, cur_pt, &font, cvScalar(0,0, 0, 255));
  cvShowImage( "src", src );  
 }          
 else if( event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON))
 {                                 
  cvCopy(dst,src);      
  sprintf(temp,"(%d,%d)",x,y); 
  cur_pt = cvPoint(x,y);   
  cvPutText(src,temp, cur_pt, &font, cvScalar(0,0, 0, 255)); 
  cvRectangle(src, pre_pt, cur_pt, cvScalar(0,255,0,0), 1, 8, 0 ); 
  cvShowImage( "src", src );    
 }                        
 else if( event == CV_EVENT_LBUTTONUP )
 {                                 
  sprintf(temp,"(%d,%d)",x,y);  
  cur_pt = cvPoint(x,y);       
  cvPutText(src,temp, cur_pt, &font, cvScalar(0,0, 0, 255));
  cvCircle( src, cur_pt, 3,cvScalar(255,0,0,0) ,CV_FILLED, CV_AA, 0 );
  cvRectangle( src, pre_pt, cur_pt, cvScalar(0,255,0,0), 5, 8, 0 ); 
  cvShowImage( "src", src );                                   
  cvCopy(src,dst);             
 }            
}               
 
int _tmain(int argc, _TCHAR* argv[])
{
 src=cvLoadImage("st110.jpg",-1);                                
 dst=cvCloneImage(src);                                        
 cvNamedWindow("src",0);                                       
 cvSetMouseCallback( "src", on_mouse, 0 );                    
 cvShowImage("src",src);                                       
 cvWaitKey(0);                                                 
 cvDestroyAllWindows();                                        
 cvReleaseImage(&src);                                         
 cvReleaseImage(&dst);                                        
 
 return 0;
}

程序之二,在OpenCV中利用鼠標繪製矩形並截取該矩形區域的圖像spa

#include <cv.h>  
#include <highgui.h>  
#include <stdio.h> 
#pragma comment(lib, "opencv_core247.lib" )   
#pragma comment(lib, "opencv_core247d.lib" )   
#pragma comment(lib, "opencv_highgui247.lib" )  
#pragma comment(lib, "opencv_highgui247d.lib" )
IplImage* org = 0;                         
IplImage* img = 0;                         
IplImage* tmp = 0;                         
IplImage* dst = 0;                         
void on_mouse( int event, int x, int y, int flags, void* ustc)
{                                         
 static CvPoint pre_pt = {-1,-1};       
 static CvPoint cur_pt = {-1,-1};       
 CvFont font;                           
 cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5, 0, 2, CV_AA); 
 char temp[16];  
 if( event == CV_EVENT_LBUTTONDOWN )    
 {                                     
  cvCopy(org,img);                   
  sprintf(temp,"(%d,%d)",x,y);      
  pre_pt = cvPoint(x,y);             
  cvPutText(img,temp, pre_pt, &font, cvScalar(0,0, 0, 255)); 
  cvCircle( img, pre_pt, 3,cvScalar(255,0,0,0) ,CV_FILLED, CV_AA, 0 );
  cvShowImage( "img", img );        
  cvCopy(img,tmp);                   
 }                                                            
 else if( event == CV_EVENT_MOUSEMOVE && !(flags & CV_EVENT_FLAG_LBUTTON))
 {                                      
  cvCopy(tmp,img);                   
  sprintf(temp,"(%d,%d)",x,y);      
  cur_pt = cvPoint(x,y);             
  cvPutText(img,temp, cur_pt, &font, cvScalar(0,0, 0, 255));  
  cvShowImage( "img", img );         
 }                                      
 else if( event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON)) 
 {                                      
  cvCopy(tmp,img);                   
  sprintf(temp,"(%d,%d)",x,y);       
  cur_pt = cvPoint(x,y);             
  cvPutText(img,temp, cur_pt, &font, cvScalar(0,0, 0, 255)); 
  cvRectangle(img, pre_pt, cur_pt, cvScalar(0,255,0,0), 1, 8, 0 ); 
  cvShowImage( "img", img );         
 }                                      
 else if( event == CV_EVENT_LBUTTONUP )  
 {                                      
  cvCopy(tmp,img);                   
  sprintf(temp,"(%d,%d)",x,y);       
  cur_pt = cvPoint(x,y);            
  cvPutText(img,temp, cur_pt, &font, cvScalar(0,0, 0, 255));
  cvCircle( img, cur_pt, 3,cvScalar(255,0,0,0) ,CV_FILLED, CV_AA, 0 );  
  cvRectangle( img, pre_pt, cur_pt, cvScalar(0,255,0,0), 5, 8, 0 );
  cvShowImage( "img", img );        
  cvCopy(img,tmp);                   
  int width=abs(pre_pt.x-cur_pt.x);  
  int height=abs(pre_pt.y-cur_pt.y); 
  if(width==0 || height==0)          
  {                                  
   cvDestroyWindow("dst");        
   return;                        
  }                                  
  dst=cvCreateImage(cvSize(width,height),org->depth,org->nChannels); 
  CvRect rect;                      
  if(pre_pt.x<cur_pt.x && pre_pt.y<cur_pt.y) 
  {                                 
   rect=cvRect(pre_pt.x,pre_pt.y,width,height);  
  }                                      
  else if(pre_pt.x>cur_pt.x && pre_pt.y<cur_pt.y) 
  {                                         
   rect=cvRect(cur_pt.x,pre_pt.y,width,height);
  }                                           
  else if(pre_pt.x>cur_pt.x && pre_pt.y>cur_pt.y) 
  {                                              
   rect=cvRect(cur_pt.x,cur_pt.y,width,height); 
  }                                            
  else if(pre_pt.x<cur_pt.x && pre_pt.y>cur_pt.y)  
  {                                            
   rect=cvRect(pre_pt.x,cur_pt.y,width,height); 
  }                                        
  cvSetImageROI(org,rect);                
  cvCopy(org,dst);                   
  cvResetImageROI(org);              
  cvDestroyWindow("dst");            
  cvNamedWindow("dst",0);            
  cvShowImage("dst",dst);            
  cvSaveImage("dst.jpg",dst);        
 }                                                                                                                             
}                                                                                                                                
int _tmain(int argc, _TCHAR* argv[])
{
 org=cvLoadImage("st110.jpg",-1);                      
 img=cvCloneImage(org);                              
 tmp=cvCloneImage(org);                              
 cvNamedWindow("img",0);                             
 cvSetMouseCallback( "img", on_mouse, 0 );          
 cvShowImage("img",img);                             
 cvWaitKey(0);                                       
 cvDestroyAllWindows();                             
 cvReleaseImage(&org);                              
 cvReleaseImage(&img);                              
 cvReleaseImage(&tmp);                              
 cvReleaseImage(&dst);                             
 return 0;
}
相關文章
相關標籤/搜索