opencv:鼠标事件

来源:互联网 发布:python如何运行代码 编辑:程序博客网 时间:2024/06/05 19:07

鼠标事件类型

CV_EVENT_MOUSEMOVE0CV_EVENT_LBUTTONDOWN1CV_EVENT_RBUTTONDOWN2CV_EVENT_MRUTTONDOWN3CV_EVENT_LBUTTONUP4CV_EVENT_RBUTTONUP5CV_EVENT_MBUTTONUP6CV_EVENT_LBUTTONDBLCLK7CV_EVENT_RBUTTONDBLCLK8CV_EVENT_MBUTTONDBLCLK9

鼠标事件标志

CV_EVENT_FLAG_LBUTTON1CV_EVENT_FLAG_RBUTTON2CV_EVENT_FLAG_MBUTTON3CV_EVENT_FLAG_CTRLKEY8CV_EVENT_FLAG_SHIFTKEY16CV_EVENT_FLAG_ALTKEY32

用鼠标画矩形

//鼠标事件注册void cvSetMouseCallback(const char* window_name,CvMouseCallback on_mouse,void* param = NULL);
#include "cv.h"#include "highgui.h"//鼠标事件声明void my_mouse_callback(int event,int x,int y,int flags,void* param);//声明矩形CvRect box;//画矩形的标志变量bool drawing_box = false;//在图像上画一个矩形void draw_box(IplImage* img,CvRect rect){    cvRectangle(img,cvPoint(box.x,box.y),cvPoint(box.x+box.width,box.y+box.height),cvScalar(0xff,0x00,0x00));}int main(int argc,char** argv){    box = cvRect(-1,-1,0,0);    IplImage* image = cvCreateImage(cvSize(200,200),IPL_DEPTH_8U,3);    cvZero(image);    IplImage* temp = cvCloneImage(image);    cvNamedWindow("Example3_6",0);    //注册鼠标事件    cvSetMouseCallback("Example3_6",my_mouse_callback,(void*)image);    while (1)    {         cvCopyImage(image,temp);         if (drawing_box)         {             draw_box(temp,box);         }         cvShowImage("Example3_6",temp);         if (cvWaitKey(15) == 27)         {             break;         }    }    cvReleaseImage(&image);    cvReleaseImage(&temp);    cvDestroyAllWindows();}void my_mouse_callback(int event,int x,int y,int flags,void* param){    IplImage* image = (IplImage*)param;    switch (event)    {    case CV_EVENT_MOUSEMOVE:         {             if (drawing_box)             {                 box.width = x - box.x;                 box.height = y - box.y;             }         }         break;    case CV_EVENT_LBUTTONDOWN:         {             drawing_box = true;             box = cvRect(x,y,0,0);         }         break;    case CV_EVENT_LBUTTONUP:         {             drawing_box = false;             if (box.width < 0)             {                 box.x +=box.width;                 box.width *= -1;             }             if (box.height <0)             {                 box.y += box.height;                 box.height *= -1;             }             draw_box(image,box);         }         break;    default:         break;    }}
0 0
原创粉丝点击