opencv学习笔记——鼠标操作

#include <opencv2/opencv.hpp>

using namespace cv;

#define WINDOW_NAME     "【程序窗口】"            //为窗口标题定义的宏

/* 全局函数的声明 */
void on_MouseHandle(int event, int x, int y, int flags, void *param);
void DrawRectangle(cv::Mat& img, cv::Rect box);
void ShowHelpText();

/* 全局变量的声明 */
Rect g_rectangle;
bool g_bDrawingBox = false;                     //是否进行绘制
RNG g_rng(12345);

int main(int argc, char** argv)
{
    //【1】准备参数
    g_rectangle = Rect(-1, -1, 0, 0);
    Mat srcImage(600, 800, CV_8UC3), tempImage;
    srcImage.copyTo(tempImage);
    g_rectangle = Rect(-1, -1, 0, 0);
    srcImage = Scalar::all(0);

    //【2】设置鼠标操作回调函数
    namedWindow(WINDOW_NAME);
    setMouseCallback(WINDOW_NAME, on_MouseHandle, (void *)&srcImage);

    //【3】程序主循环,当进行绘制的标识符为真时,进行绘制
    while (1) {
        srcImage.copyTo(tempImage);
        if (g_bDrawingBox)
            DrawRectangle(tempImage, g_rectangle);      //当进行绘制的标识符为真,则进行绘制
        imshow(WINDOW_NAME, tempImage);
        if (waitKey(10) == 27)
            break;                                      //按下ESC键,程序退出
    }

    return 0;
}

/* 鼠标回调函数,根据不同的鼠标事件进行不同的操作 */
void on_MouseHandle(int event, int x, int y, int flags, void *param)
{
    Mat image = *(cv::Mat*)param;
    switch (event) {
        //鼠标移动消息
        case EVENT_MOUSEMOVE:
            if (g_bDrawingBox) {                        //如果是否进行绘制的标识符为真,则记录下长和宽到RECT型变量中
                g_rectangle.width = x - g_rectangle.x;
                g_rectangle.height = y - g_rectangle.y;
            }
            break;

        //左键按下消息
        case EVENT_LBUTTONDOWN:
            g_bDrawingBox = true;
            g_rectangle = Rect(x, y, 0, 0);             //记录起始点
            break;

        //左键抬起消息
        case EVENT_LBUTTONUP:
            g_bDrawingBox = false;                      //置标识符为false
            //对宽和高小于0的处理
            if (g_rectangle.width < 0) {
                g_rectangle.x += g_rectangle.width;
                g_rectangle.width *= -1;
            }
            if (g_rectangle.height < 0) {
                g_rectangle.y += g_rectangle.height;
                g_rectangle.height *= -1;
            }
            //调用函数进行绘制
            DrawRectangle(image, g_rectangle);
            break;
    }
}

/* 自定义矩形绘制函数 */
void DrawRectangle(cv::Mat& img, cv::Rect box)
{
    //随机函数
    rectangle(img, box.tl(), box.br(), Scalar(g_rng.uniform(0, 255), g_rng.uniform(0, 255), g_rng.uniform(0, 255)));
}

setMouseCallback函数

void setMouseCallback(const string& winname, MouseCallback onMouse, void* userdata = 0)
参数 说明
winname const string&类型,窗口的名字
onMouse MouseCallback类型,指定窗口里每次鼠标事件发生时调用的函数指针

鼠标回调函数

void Foo(int event, int x, int y, int flags, void* param)

  event是EVENT_+变量之一,x和y是鼠标指针在图像坐标系中的坐标,flags是EVENT_FLAG的组合,param是用户传递到setMouseCallback函数调用的参数。

Rect函数

Rect(x,y,width,height)

  x, y 为左上角坐标, width, height 则为长和宽。

//利用对角线两点来画矩形:
void rectangle(Mat& img, Point pt1,Point pt2,const Scalar& color, int thickness=1, int lineType=8, int shift=0)
//传入矩形参数来画矩形:
void rectangle(Mat& img, Rect rec, const Scalar& color, int thickness=1, int lineType=8, int shift=0 )

猜你喜欢

转载自blog.csdn.net/horotororensu/article/details/78430154
今日推荐