OpenCV 接收鼠标消息——用鼠标画长方形

OpenCV 接收鼠标消息——用鼠标画长方形

创建一幅黑色的图像,显示后,用鼠标在显示窗口中,拖动以画出长方形:
  1. #include "stdafx.h"  
  2.   
  3. #include <cv.h>  
  4. #include <cxcore.h>  
  5. #include <highgui.h>  
  6.   
  7. void myMouseCallBack(int event, int x, int y, int flags, void * param);  
  8. CvRect box;  
  9. bool drawing_box = false;  
  10. void draw_box(IplImage * img, CvRect rect)  
  11. {  
  12.     cvRectangle(  
  13.         img,  
  14.         cvPoint(box.x, box.y),  
  15.         cvPoint(box.x + box.width, box.y + box.height),  
  16.         cvScalar(0x00, 0x00, 0xff)  
  17.         );  
  18. }  
  19.   
  20. int _tmain(int argc, _TCHAR* argv[])  
  21. {     
  22.     box = cvRect(-1, -1, 0, 0);  
  23.   
  24.     IplImage * image = cvCreateImage(cvSize(200,200), IPL_DEPTH_8U, 3);  
  25.       
  26.     cvZero(image);  
  27.   
  28.     IplImage * temp = cvCloneImage(image);//会重新分配内存空间  
  29.   
  30.     cvNamedWindow("Box Example");  
  31.   
  32.     cvSetMouseCallback("Box Example", myMouseCallBack, (void*)image);  
  33.   
  34.     while(1)  
  35.     {  
  36.         cvCopyImage(image, temp);//只是复制值,不会分配新空间  
  37.         if(drawing_box)   
  38.             draw_box(temp,box);  
  39.   
  40.         cvShowImage("Box Example", temp);  
  41.         if(cvWaitKey(15) == 27)  
  42.             break;  
  43.     }  
  44.   
  45.     cvReleaseImage(&image);  
  46.     cvReleaseImage(&temp);  
  47.     cvDestroyWindow("Box Example");  
  48.   
  49.     return 0;  
  50. }  
  51.   
  52.   
  53. void myMouseCallBack(int event, int x, int y, int flags, void * param)  
  54. {  
  55.     IplImage * image = (IplImage*)param;  
  56.   
  57.     switch(event)  
  58.     {  
  59.     case CV_EVENT_MOUSEMOVE:  
  60.         {  
  61.             if(drawing_box)  
  62.             {  
  63.                 box.width = x - box.x;  
  64.                 box.height = y - box.y;  
  65.             }  
  66.         }  
  67.         break;  
  68.     case CV_EVENT_LBUTTONDOWN:  
  69.         {  
  70.             drawing_box = true;  
  71.             box = cvRect(x,y,0,0);  
  72.         }  
  73.         break;  
  74.     case CV_EVENT_LBUTTONUP:  
  75.         {  
  76.             drawing_box = false;  
  77.             if(box.width < 0)  
  78.             {  
  79.                 box.x += box.width;  
  80.                 box.width *= -1;  
  81.             }  
  82.             if(box.height < 0)  
  83.             {  
  84.                 box.y += box.height;  
  85.                 box.height *= -1;  
  86.             }  
  87.             draw_box(image,box);  
  88.         }  
  89.         break;  
  90.     }  
  91.   
  92. }  
结果:
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值