• 鼠标操作和响应


    示例代码如下:

    Point sp(-1, -1);
    Point st(-1, -1);
    Mat temp;
    static void on_draw(int event, int x, int y, int flags, void* userdata)
    {
        Mat image = *((Mat*)userdata);
        if (event == EVENT_LBUTTONDOWN)
        {
            sp.x = x;
            sp.y = y;
            cout << sp << endl;
        }
        else if (event == EVENT_LBUTTONUP)
        {
            
            st.x = x - sp.x;
            st.y = y - sp.y;
            int x1 = st.x;
            int y1 = st.y;
            Rect rec(sp.x, sp.y, x1, y1);
            if (st.x > 0 && st.y > 0)
            {
                rectangle(image,rec, Scalar(255, 0, 0), 2, 8, 0);
                imshow("鼠标绘制", image);
                imshow("ROI区域", image(rec));
            }
            sp.x = -1;
            sp.y = -1;//鼠标左键弹起时,不再画矩形
        }
        else if (event == EVENT_MOUSEMOVE)
        {
            if (sp.x > 0 && sp.y > 0)//和394,395代码配合使用
            {
                st.x = x - sp.x;
                st.y = y - sp.y;
                int x1 = st.x;
                int y1 = st.y;
                Rect rec(sp.x, sp.y, x1, y1);
                if (st.x > 0 && st.y > 0)
                {
                    temp.copyTo(image);//把temp复制到image
                    rectangle(image, rec, Scalar(255, 0, 0), 2, 8, 0);
                    imshow("鼠标绘制", image);
                }
            }
        }
    }
    void demo::mouse_demo(Mat& image)
    {
        namedWindow("鼠标绘制", WINDOW_AUTOSIZE);
        temp = image.clone();
        setMouseCallback("鼠标绘制", on_draw, (void*)(&image));
        
    }

    这里的难点是setMouseCallback()函数,声明在highgui.hpp

    CV_EXPORTS void setMouseCallback(const String& winname, MouseCallback onMouse, void* userdata = 0);

    1.window_name,窗口的名字

    2.on_Mouse,指定窗口里每次鼠标事件发生时,被调用的函数指针。

         这个函数的原型大概形式为 void XXX(int event, int x, int y, int flags, void* param) .

         其中 event 是 EVENT_XXX 类型数据,代表鼠标操作事件:

    1. enum MouseEventTypes {
    2. EVENT_MOUSEMOVE = 0, //!< indicates that the mouse pointer has moved over the window.
    3. EVENT_LBUTTONDOWN = 1, //!< indicates that the left mouse button is pressed.
    4. EVENT_RBUTTONDOWN = 2, //!< indicates that the right mouse button is pressed.
    5. EVENT_MBUTTONDOWN = 3, //!< indicates that the middle mouse button is pressed.
    6. EVENT_LBUTTONUP = 4, //!< indicates that left mouse button is released.
    7. EVENT_RBUTTONUP = 5, //!< indicates that right mouse button is released.
    8. EVENT_MBUTTONUP = 6, //!< indicates that middle mouse button is released.
    9. EVENT_LBUTTONDBLCLK = 7, //!< indicates that left mouse button is double clicked.
    10. EVENT_RBUTTONDBLCLK = 8, //!< indicates that right mouse button is double clicked.
    11. EVENT_MBUTTONDBLCLK = 9, //!< indicates that middle mouse button is double clicked.
    12. EVENT_MOUSEWHEEL = 10,//!< positive and negative values mean forward and backward scrolling, respectively.
    13. EVENT_MOUSEHWHEEL = 11 //!< positive and negative values mean right and left scrolling, respectively.
    14. };

         x 和 y 是鼠标指针在图像坐标系(不是窗口坐标系)中的坐标值,

         flags 是 EVENT_FLAG_XXX 类型数据,也代表鼠标操作事件

    1. enum MouseEventFlags {
    2. EVENT_FLAG_LBUTTON = 1, //!< indicates that the left mouse button is down.
    3. EVENT_FLAG_RBUTTON = 2, //!< indicates that the right mouse button is down.
    4. EVENT_FLAG_MBUTTON = 4, //!< indicates that the middle mouse button is down.
    5. EVENT_FLAG_CTRLKEY = 8, //!< indicates that CTRL Key is pressed.
    6. EVENT_FLAG_SHIFTKEY = 16,//!< indicates that SHIFT Key is pressed.
    7. EVENT_FLAG_ALTKEY = 32 //!< indicates that ALT Key is pressed.
    8. };

         param 是用户定义的传递到 setMouseCallback 函数调用的参数。

    3.userdata,用户定义的传递到回调函数的参数,有默认值 0。注意类型转换。

    结果如下:

     over!!!

  • 相关阅读:
    冰冰学习笔记:二叉树的进阶OJ题与非递归遍历
    J2EE基础-自定义MVC(上)
    Docker轻量级可视化工具Portainer
    C++ 共享内存相关的API
    java计算机毕业设计医院预约挂号系统源码+系统+mysql数据库+lw文档
    目标检测yolov3+文字识别CRNN 实现文本检测和识别
    龙格-库塔法(Runge-Kutta methods)
    什么时候不要采用微服务架构
    (一)H264视频解码问题:出现部分绿屏问题的解决
    【计算机视觉】图像聚类&噪声
  • 原文地址:https://blog.csdn.net/weixin_58752235/article/details/126011650