鼠标绘制矩形

程序之一,在OpenCV中利用鼠标绘制矩形

#include <cv.h>
#include <highgui.h>
#include <stdio.h>

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, 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.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);                                                                 //取图像感兴趣区域的图像,rect里的图像
		cvCopy(org, dst);      
		cvResetImageROI(org);
		cvDestroyWindow("dst");
		cvNamedWindow("dst", 1);
		cvShowImage("dst", dst);
		cvSaveImage("dst.jpg", dst);
	}
}
int main()
{
	org = cvLoadImage("D:6.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;
}


效果图如下

 

程序之二,在OpenCV中利用鼠标绘制矩形并截取该矩形区域的图像

[c-sharp]  view plain copy
  1. #include <cv.h>  
  2. #include <highgui.h>  
  3. #include <stdio.h>  
  4. #pragma comment( lib, "cv.lib" )  
  5. #pragma comment( lib, "cxcore.lib" )  
  6. #pragma comment( lib, "highgui.lib" )  
  7. IplImage* org = 0;  
  8. IplImage* img = 0;   
  9. IplImage* tmp = 0;   
  10. IplImage* dst = 0;   
  11. void on_mouse( int eventint x, int y, int flags, void* ustc)  
  12. {  
  13.     static CvPoint pre_pt = {-1,-1};  
  14.     static CvPoint cur_pt = {-1,-1};  
  15.     CvFont font;  
  16.     cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5, 0, 1, CV_AA);  
  17.     char temp[16];  
  18.       
  19.     ifevent == CV_EVENT_LBUTTONDOWN )  
  20.     {  
  21.         cvCopy(org,img);  
  22.         sprintf(temp,"(%d,%d)",x,y);  
  23.         pre_pt = cvPoint(x,y);  
  24.         cvPutText(img,temp, pre_pt, &font, cvScalar(0,0, 0, 255));  
  25.         cvCircle( img, pre_pt, 3,cvScalar(255,0,0,0) ,CV_FILLED, CV_AA, 0 );  
  26.         cvShowImage( "img", img );  
  27.         cvCopy(img,tmp);  
  28.     }  
  29.     else ifevent == CV_EVENT_MOUSEMOVE && !(flags & CV_EVENT_FLAG_LBUTTON))  
  30.     {  
  31.         cvCopy(tmp,img);  
  32.         sprintf(temp,"(%d,%d)",x,y);  
  33.         cur_pt = cvPoint(x,y);        
  34.         cvPutText(img,temp, cur_pt, &font, cvScalar(0,0, 0, 255));  
  35.         cvShowImage( "img", img );  
  36.     }  
  37.     else ifevent == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON))  
  38.     {  
  39.         cvCopy(tmp,img);  
  40.         sprintf(temp,"(%d,%d)",x,y);  
  41.         cur_pt = cvPoint(x,y);        
  42.         cvPutText(img,temp, cur_pt, &font, cvScalar(0,0, 0, 255));  
  43.         cvRectangle(img, pre_pt, cur_pt, cvScalar(0,255,0,0), 1, 8, 0 );  
  44.         cvShowImage( "img", img );  
  45.     }  
  46.     else ifevent == CV_EVENT_LBUTTONUP )  
  47.     {  
  48.         cvCopy(tmp,img);  
  49.         sprintf(temp,"(%d,%d)",x,y);  
  50.         cur_pt = cvPoint(x,y);        
  51.         cvPutText(img,temp, cur_pt, &font, cvScalar(0,0, 0, 255));  
  52.         cvCircle( img, cur_pt, 3,cvScalar(255,0,0,0) ,CV_FILLED, CV_AA, 0 );  
  53.         cvRectangle( img, pre_pt, cur_pt, cvScalar(0,255,0,0), 1, 8, 0 );  
  54.         cvShowImage( "img", img );  
  55.         cvCopy(img,tmp);  
  56.         int width=abs(pre_pt.x-cur_pt.x);  
  57.         int height=abs(pre_pt.y-cur_pt.y);  
  58.         if(width==0 || height==0)  
  59.         {  
  60.             cvDestroyWindow("dst");  
  61.             return;  
  62.         }  
  63.         dst=cvCreateImage(cvSize(width,height),org->depth,org->nChannels);  
  64.         CvRect rect;  
  65.         if(pre_pt.x<cur_pt.x && pre_pt.y<cur_pt.y)  
  66.         {  
  67.             rect=cvRect(pre_pt.x,pre_pt.y,width,height);  
  68.         }  
  69.         else if(pre_pt.x>cur_pt.x && pre_pt.y<cur_pt.y)  
  70.         {  
  71.             rect=cvRect(cur_pt.x,pre_pt.y,width,height);  
  72.         }  
  73.         else if(pre_pt.x>cur_pt.x && pre_pt.y>cur_pt.y)  
  74.         {  
  75.             rect=cvRect(cur_pt.x,cur_pt.y,width,height);  
  76.         }  
  77.         else if(pre_pt.x<cur_pt.x && pre_pt.y>cur_pt.y)  
  78.         {  
  79.             rect=cvRect(pre_pt.x,cur_pt.y,width,height);  
  80.         }  
  81.         cvSetImageROI(org,rect);  
  82.         cvCopy(org,dst);  
  83.         cvResetImageROI(org);  
  84.         cvDestroyWindow("dst");  
  85.         cvNamedWindow("dst",1);  
  86.         cvShowImage("dst",dst);  
  87.         cvSaveImage("dst.jpg",dst);  
  88.     }  
  89. }  
  90. int main()  
  91. {  
  92.     org=cvLoadImage("lena.jpg",1);  
  93.     img=cvCloneImage(org);  
  94.     tmp=cvCloneImage(org);  
  95.     cvNamedWindow("img",1);  
  96.     cvSetMouseCallback( "img", on_mouse, 0 );  
  97.       
  98.     cvShowImage("img",img);  
  99.     cvWaitKey(0);   
  100.     cvDestroyAllWindows();  
  101.     cvReleaseImage(&org);  
  102.     cvReleaseImage(&img);  
  103.     cvReleaseImage(&tmp);  
  104.     cvReleaseImage(&dst);  
  105.     return 0;  
  106. }  

 

效果图如下

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值