• opencv4 鼠标事件 鼠标画线条


    #include "opencv2/core.hpp"
    #include "opencv2/imgproc.hpp"
    #include "opencv2/highgui.hpp"
    #include "opencv2/videoio.hpp"
    #include <iostream>
    #include <stdlib.h>
    using namespace cv;
    using namespace std;
    #define WINDOW_NAME "test"
    
    struct node{
        Mat mt;
        int* P;
    };
    
    //event和flags都是事件描述
    //x,y为鼠标坐标
    //data为传过来的数据的指针,
    //  一定是void*,所以使用的时候需要转换
    void on_MouseHandle(int event,int x,int y,int flags,void* data){
    
        node* d = (node*)data;
        Mat mt = (*d).mt;
        int* P = (*d).P;
    
        //使用event参数实现
        if(event == EVENT_LBUTTONDOWN){ //如果是左键点击
            (*P) = 1;
        }
        else if(event == EVENT_MOUSEMOVE && (*P)){ //如果是鼠标移动
            Vec3b& v = mt.at<Vec3b>(y,x);
            v[0] = rand() % 256;
            v[1] = rand() % 256;
            v[2] = rand() % 256;
        }
        else if(event == EVENT_LBUTTONUP){ //如果是放开左键
            (*P) = 0;
        }
    
        /*
        //使用flags参数实现
        //flags & EVENT_FLAG_LBUTTON 的意思是
        //  提取flags中的EVENT_FLAG_LBUTTON位,如果是这个事件,则为1
    
        if(flags & EVENT_FLAG_LBUTTON){
            Vec3b& v = mt.at<Vec3b>(y,x);
            v[0] = rand() % 256;
            v[1] = rand() % 256;
            v[2] = rand() % 256;
        }
        */
        imshow(WINDOW_NAME,mt);
    }
    
    int main(int argc, char ** argv)
    {
        Mat mt(300,300,CV_8UC3,Scalar(0,0,0));
        int P = 0;
        node x;
        x.mt = mt;
        (x.P) = &P;
    
        //创建窗口
        namedWindow(WINDOW_NAME);
    
        //设置鼠标操作
        //参数说明
        //1.窗口名字
        //2.回调函数,只要有鼠标操作都会调用它
        //3.要传给回调函数的数据
        setMouseCallback(WINDOW_NAME, on_MouseHandle, &x);
    
        waitKey(0);
        return 0;
    }
    View Code

    event和flags的参数说明

    截图自https://www.bbsmax.com/A/1O5ER4nrd7/

  • 相关阅读:
    node.js 基础篇
    node.js 开发环境搭建
    Velocity VelocityEngine 支持多种loader 乱码问题
    tomcat集群及session共享
    上海支付宝终面后等了两周,没能收到offer却来了杭州淘宝的电话面试
    支付宝hr终面,忐忑的等待结果
    mysql 数据范围总结
    rsync同步文件(多台机器同步代码...)
    linux基础命令
    路飞学城项目之前后端交互,跨域问题
  • 原文地址:https://www.cnblogs.com/InitRain/p/12885961.html
Copyright © 2020-2023  润新知