鼠标响应

本文探讨了在编程中遇到的鼠标事件响应错误,指出LBUTTONDOWN是正确的左键按下事件,而非双击。同时,由于image变量的作用域问题,导致矩形无法在响应函数中正确绘制。解决方案是将image设为全局变量,并在main函数中调用画矩形函数。然而,这仍然会导致移动过程中的矩形显示,作者提出思考如何在不显示中间矩形的情况下实现轨迹跟踪。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

switch(event) {
        case CV_EVENT_LBUTTONDBLCLK:{
            //g_rect.x = x;
            //g_rect.y = y;
            g_isPaint = true;
            g_rect.x = x;
            g_rect.y = y;
            cout<<"left button clicked"<<endl;
        }
        break;

        //鼠标移动
        case CV_EVENT_MOUSEMOVE: {
            if(g_isPaint){
                g_rect.width = x - g_rect.x;
                g_rect.height = y - g_rect.y;
                cout<<"moving"<<endl;
            }
        }
        break;

        case EVENT_LBUTTONUP:{
            g_isPaint = false;

            if(g_rect.width<0){
                g_rect.x += g_rect.width;
                g_rect.width *= -1;
            }
            if(g_rect.height<0){
                g_rect.y += g_rect.height;
                g_rect.height *= -1;
            }
            cout<<"left button up, about to draw"<<endl;

            DrawRectangle(image, g_rect);
        }
        break;

这么写,发现点击左键事件和移动的事件都没有发生。为什么?

原来= =是事件写错了。第一个不应该是双击左键,而是LBUTTONDOWN。而且DrawRectangle函数并不能放在响应函数里面,我认为是因为image实际上是在main函数里面定义的,所以要在main函数里面被使用?这里不是很清楚。后面把image定义成全局变量,还是只有在main中调用画矩形函数才会真正地画出矩形……

#include <iostream>

#include <opencv2/opencv.hpp>
#include "opencv2/highgui/highgui.hpp"

using namespace std;
using namespace cv;


//鼠标响应
void    onMouse(int event, int x, int y, int flags, void* param);
//绘制矩形
void    DrawRectangle(Mat &img, Rect box);

Rect    g_rect;
bool    g_isPaint = false;
RNG     g_rng(1000);
Mat    srcImage1(600,800, CV_8UC1, Scalar(255, 255, 255));
#define    WINDOWNAME    "Paint"

int main(int argc, char** argv) {
    g_rect = Rect(-1, -1, 0, 0);
    Mat    srcImage1(600,800, CV_8UC1, Scalar(255, 255, 255)), tempImage;
    srcImage1.copyTo(tempImage);

    namedWindow(WINDOWNAME, WINDOW_AUTOSIZE);
    //setMouseCallback的第三个形参本身就是一个指向void类型的指针,现在让这个指针还是一个引用。
    setMouseCallback(WINDOWNAME, onMouse, (void*)&tempImage);
    waitKey(1);
    while(1) {
        imshow(WINDOWNAME, tempImage);

        if(g_isPaint) {
            DrawRectangle(tempImage, g_rect);
        }


        if(waitKey(1)==32) {
            break;
        }
    }
    return 0;
}

void onMouse(int event, int x, int y, int flags, void* param) {
    Mat& image = *(Mat*) param;

    if(event==EVENT_LBUTTONDOWN) {
        //g_rect.x = x;
        //g_rect.y = y;
        g_isPaint = true;
        g_rect.x = x;
        g_rect.y = y;
        cout << "left button clicked" << endl;
    }

    else if(event== EVENT_MOUSEMOVE) {
        if (g_isPaint==true) {
            g_rect.width = x - g_rect.x;
            g_rect.height = y - g_rect.y;
            cout << "moving" << endl;
        }
    }

    else if(event==EVENT_LBUTTONUP) {
        //在这里如果g_isPaint一直是true,主函数会一直调用画矩形函数
        g_isPaint = false;

        if (g_rect.width < 0) {
            g_rect.x += g_rect.width;
            g_rect.width *= -1;
        }
        if (g_rect.height < 0) {
            g_rect.y += g_rect.height;
            g_rect.height *= -1;
        }
        cout << "left button up, about to draw" << endl;

        //DrawRectangle(image, g_rect);
        //复原
        g_rect = Rect(-1, -1, 0, 0);
        cout<<g_rect.x<<","<<g_rect.y<<","<<g_rect.width<<","<<g_rect.height<<endl;
    }
}

void DrawRectangle(Mat &img, Rect box) {
    rectangle(img, box.tl(), box.br(), \
        Scalar(g_rng.uniform(0, 255) ,g_rng.uniform(0, 255), g_rng.uniform(0, 255)), 1, 1, 0);
    waitKey(1);
    cout<<box.tl()<<","<<box.br()<<endl;
}

不过这样还会画出来移动过程中的矩形:


所以思考一下怎么样画出跟踪轨迹的,但是过程中又不会被画出来的矩形呐?




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值