OpenCV笔记-highgui之响应鼠标消息

主要使用cvSetMouseCallback设置鼠标响应的回调函数

/* assign callback for mouse events */
CVAPI(void) cvSetMouseCallback( const char* window_name, CvMouseCallback on_mouse,
void* param CV_DEFAULT(NULL));

window_name:响应鼠标消息的窗口名

on_mouse:响应鼠标消息的回调函数

param:调用on_mouse回调函数时传递的额外参数,默认为NULL

 opencv的鼠标回调函数格式:

void on_mouse(

    int event,

    int x,

    int y,

    int flags,

    void *param

);

event:鼠标所发生的事件

事件有:

enum

{

    CV_EVENT_MOUSEMOVE      =0,//鼠标移动

    CV_EVENT_LBUTTONDOWN    =1,//鼠标左键按下

    CV_EVENT_RBUTTONDOWN    =2,//鼠标右键按下

    CV_EVENT_MBUTTONDOWN    =3,//鼠标中键按下

    CV_EVENT_LBUTTONUP      =4,//鼠标左键弹起

    CV_EVENT_RBUTTONUP      =5,//鼠标右键弹起

    CV_EVENT_MBUTTONUP      =6,//鼠标中键弹起

    CV_EVENT_LBUTTONDBLCLK  =7,//鼠标左键双击

    CV_EVENT_RBUTTONDBLCLK  =8,//右键双击

    CV_EVENT_MBUTTONDBLCLK  =9 //中键双击

};

x,y:鼠标位置

flags:事件发生时的状态

enum

{

    CV_EVENT_FLAG_LBUTTON   =1,//左键按下

    CV_EVENT_FLAG_RBUTTON   =2,//右键按下

    CV_EVENT_FLAG_MBUTTON   =4,//中键按下

    CV_EVENT_FLAG_CTRLKEY   =8,//ctrl键按下

    CV_EVENT_FLAG_SHIFTKEY  =16,//shift键按下

    CV_EVENT_FLAG_ALTKEY    =32//alt键按下

};

param:通过cvSetMouseCallback传递的额外参数

示例代码:

#include <cv.h>

#include <highgui.h>



void on_mouse(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()

{

    box = cvRect(-1, -1, 0, 0);



    //创建一张图片

    IplImage    *image = cvCreateImage(

                            cvSize(200, 200),

                            IPL_DEPTH_8U,

                            3

                        );



    cvZero(image);//初始化为全0,即图片全黑

    IplImage *temp = cvCloneImage(image);//克隆图片



    cvNamedWindow("draw boxes");//创建窗口



    cvSetMouseCallback(//设置鼠标响应

        "draw boxes",

        on_mouse,

        (void*)image

    );



    while(1)

    {

        cvCopyImage(image, temp);//把image拷贝到temp

        if(drawing_box)    draw_box(temp, box);//如果在画图,则化矩形

        cvShowImage("draw boxes", temp);//显示所画的矩形



        if(cvWaitKey(15) == 27) break;

    }



    cvReleaseImage(&image);

    cvReleaseImage(&temp);

    cvDestroyWindow("draw boxes");

    return 0;

}



void on_mouse(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;

    }

}

 

你可能感兴趣的:(opencv)