OpenCV使用鼠标响应裁剪图像-创新互联

给定一幅图像,将其中的某一部分兴趣区域裁剪出来,这在PS中很好实现,但是使用openCV如何实现呢?因此本文主要介绍openCV使用鼠标响应来裁剪图像:

10年积累的网站制作、成都网站建设经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先做网站设计后付款的网站建设流程,更有靖州免费网站建设让你可以放心的选择与我们合作。

一、代码部分:

#include "stdafx.h"
#include "cv.h" 
#include  
#include  

IplImage* org = 0; 
IplImage* img = 0; 
IplImage* tmp = 0; 
IplImage* dst = 0; 
//The mouse cuts the image accordingly
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, 1, 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), 1, 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.xcur_pt.x && pre_pt.ycur_pt.x && pre_pt.y>cur_pt.y) 
 { 
  rect=cvRect(cur_pt.x,cur_pt.y,width,height); 
 } 
 else if(pre_pt.xcur_pt.y) 
 { 
  rect=cvRect(pre_pt.x,cur_pt.y,width,height); 
 } 
 cvSetImageROI(org,rect); 
 cvCopy(org,dst); 
 cvResetImageROI(org); 
 cvDestroyWindow("dst"); 
 cvNamedWindow("dst",1); 
 cvShowImage("dst",dst); 
 cvWaitKey(0);
 cvSaveImage("..\\post_img\\71253.jpg",dst); 
 } 
} 

int _tmain(int argc, _TCHAR* argv[])
{
 org=cvLoadImage("..\\image_norm\\71253.jpg",1); 
 img=cvCloneImage(org); 
 tmp=cvCloneImage(org); 
 cvNamedWindow("img",1); 
 cvSetMouseCallback( "img", on_mouse, 0); 

 cvShowImage("img",img); 
 cvWaitKey(0); 
 cvDestroyAllWindows(); 
 cvReleaseImage(&org); 
 cvReleaseImage(&img); 
 cvReleaseImage(&tmp); 
 cvReleaseImage(&dst); 
 return 0;
}

另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


当前题目:OpenCV使用鼠标响应裁剪图像-创新互联
文章来源:http://myzitong.com/article/edgph.html