我想让setmousecallback函数在仍然按下鼠标左键时连续传递坐标

时间:2018-08-31 14:58:39

标签: c++ opencv image-processing mouseevent

我的代码工作正常,但是当仍然按下鼠标左键时,它不能连续传递坐标。鼠标移动时确实会传递坐标。

对不起,我没有添加mouse_callback函数,我现在添加了它。 任何帮助都会有所帮助。 代码:

#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>

using namespace cv;
using namespace std;

Point pt(-1, -1);
bool newCoords = false;
void mouse_callback(int  event, int  x, int  y, int  flag, void *param)
{
if (event == EVENT_LBUTTONDOWN)
{
    pt.x = x;
    pt.y = y;
    newCoords = true;
}
else if (flag == EVENT_FLAG_LBUTTON && event == EVENT_MOUSEMOVE) {
    pt.x = x;
    pt.y = y;
    newCoords = true;
}
}

int main(int argc, char** argv)
{
    String WindowName = "Original Feed";
    namedWindow(WindowName, CV_WINDOW_AUTOSIZE);
    Mat oriImg;
    int Frame_Width = 720;
    int Frame_Height = 540;

    VideoCapture cap(0);
    setMouseCallback(WindowName, mouse_callback);

    cap.set(CV_CAP_PROP_FRAME_WIDTH, Frame_Width);
    cap.set(CV_CAP_PROP_FRAME_HEIGHT, Frame_Height);

    while (waitKey(30) != 27) {
        cap.read(oriImg);
        if (pt.x != -1 && pt.y != -1)
        {
            circle(oriImg, pt, 3, Scalar(0, 0, 255));

            if (newCoords)
            {
                std::cout << "Clicked coordinates: " << pt << std::endl;
                newCoords = false;
            }
        }

        imshow(WindowName, oriImg);
    }
    return 0;
}

1 个答案:

答案 0 :(得分:1)

每当OS / GUI生成鼠标事件时,都会调用回调。只要鼠标的状态(按钮,滚轮,位置等)发生变化,就会生成此类事件-如果没有任何变化,则您已经知道(可以知道)当前状态。

如果您想以某种方式响应鼠标的稳定状态(例如,静止时按住按钮),则必须自己处理。


我了解您的问题的方式如下:只要按住鼠标左键,就在显示的每帧上的光标的最新位置绘制一个圆圈。

由于我们以适当的帧速率运行,因此我们有能力保持简单,并且仅根据前一帧的点击/位置信息更新当前帧。用户将无法观察到这种轻微的延迟。

仅出于后代考虑:正​​在显示图像,并且仅在cv::waitKey函数运行时调用鼠标回调。


以下列举了每次迭代中可能遇到的情况以及在以下框架中我们应如何应对它们:

  1. LMB不在waitKey的入口和出口被保留
    • LMB从未发布过(因此从未按下过)->无事可做
    • LMB至少被保持并释放一次(因此被按下了多次)->在上次按下的位置绘制圆圈
  2. LMB并非在waitKey的入口处持有,而是在出口处持有
    • 按住LMB->在最后报告的鼠标位置绘制一个圆
  3. LMB在waitKey的入口处保留,但不保留
    • LMB至少按住一次并释放一次(然后再按下一次)->在最后按下的位置画一个圆
  4. LMB在waitKey的出入境口举行
    • 按住LMB->在最后报告的鼠标位置绘制一个圆

基于此,我们需要跟踪:

  • LMB被压下或释放的最后位置。
  • 当前是否已禁止LMB
  • 是否在先前的迭代中发布了LMB(即,即使不再发布它也被保留了)

我们可以使用struct来保存此信息(由于user data参数,我们可以使用该回调函数):

struct mouse_state
{
    mouse_state()
        : position(-1, -1)
        , left_button_held(false)
        , left_button_clicked(false)
    {}

    void new_iteration()
    {
        left_button_clicked = false;
    }

    cv::Point2i position; // Last position where the LMB was down
    bool left_button_held; // Is the LMB down right now?
    bool left_button_clicked; // Was the LMB down in the last iteration?
};

鼠标回调将负责更新以上struct

  • 当用户按下LMB时,记录位置并按住设置按钮
  • 当用户释放LMB时,记录位置,单击设置按钮,并且不按住设置按钮
  • 移动鼠标并按住LMB时,记录位置

带有一些原始调试跟踪的回调示例实现如下:

void mouse_callback(int event, int x, int y, int flag, void* param)
{
    mouse_state* state(static_cast<mouse_state*>(param));

    if (event == cv::EVENT_LBUTTONDOWN) {
        std::cout << "LMB down @ (" << x << "," << y << ")\n";
        state->position = cv::Point2i(x, y);
        state->left_button_held = true;
    } else if (event == cv::EVENT_LBUTTONUP) {
        std::cout << "LMB up @(" << x << "," << y << ")\n";
        state->position = cv::Point2i(x, y);
        state->left_button_held = false;
        state->left_button_clicked = true;
    } else if ((flag == cv::EVENT_FLAG_LBUTTON) && (event == cv::EVENT_MOUSEMOVE)) {
        std::cout << "LMB held, mouse moved to (" << x << "," << y << ")\n";
        state->position = cv::Point2i(x, y);
    }
}

然后可以通过以下方式注册回调:

mouse_state ms;
cv::setMouseCallback(WINDOW_NAME, mouse_callback, &ms);

处理循环将非常简单:

  1. 读取新框架,失败时退出循环
  2. 检查鼠标的最后状态。如果按住或单击了LMB,请在记录的位置绘制圆圈。
  3. 重置每次迭代鼠标状态标志
  4. 显示图像,更新鼠标状态信息

如下所示:

for (;;) {
    // We can have `image` here, since `cap.read` always gives us
    // a reference to its internal buffer
    cv::Mat image;
    if (!cap.read(image)) {
        std::cerr << "Failed to read image, exiting...\n";
        break; // Failed to read image, nothing else to do
    }

    if (ms.left_button_clicked || ms.left_button_held) {
        std::cout << "Current position: " << ms.position << "\n";
        cv::circle(image, ms.position, 3, cv::Scalar(0, 0, 255));
        cv::circle(image, ms.position, 10, cv::Scalar(0, 0, 255), 2);
    }

    ms.new_iteration();

    cv::imshow(WINDOW_NAME, image);
    if (cv::waitKey(30) == 27) {
        std::cout << "Esc pressed, exiting...\n";
        break;
    }
}

示例程序

#include <opencv2/opencv.hpp>
#include <iostream>

struct mouse_state
{
    mouse_state()
        : position(-1, -1)
        , left_button_held(false)
        , left_button_clicked(false)
    {}

    void new_iteration()
    {
        left_button_clicked = false;
    }

    cv::Point2i position; // Last position where the LMB was down
    bool left_button_held; // Is the LMB down right now?
    bool left_button_clicked; // Was the LMB down in the last iteration?
};

void mouse_callback(int event, int x, int y, int flag, void* param)
{
    mouse_state* state(static_cast<mouse_state*>(param));

    if (event == cv::EVENT_LBUTTONDOWN) {
        std::cout << "LMB down @ (" << x << "," << y << ")\n";
        state->position = cv::Point2i(x, y);
        state->left_button_held = true;
    } else if (event == cv::EVENT_LBUTTONUP) {
        std::cout << "LMB up @(" << x << "," << y << ")\n";
        state->position = cv::Point2i(x, y);
        state->left_button_held = false;
        state->left_button_clicked = true;
    } else if ((flag == cv::EVENT_FLAG_LBUTTON) && (event == cv::EVENT_MOUSEMOVE)) {
        std::cout << "LMB held, mouse moved to (" << x << "," << y << ")\n";
        state->position = cv::Point2i(x, y);
    }
}

int main(int argc, char** argv)
{
    cv::String const WINDOW_NAME("Original Feed");
    cv::namedWindow(WINDOW_NAME, CV_WINDOW_AUTOSIZE);

    mouse_state ms;
    cv::setMouseCallback(WINDOW_NAME, mouse_callback, &ms);

    int const FRAME_WIDTH(720);
    int const FRAME_HEIGHT(540);

    cv::VideoCapture cap(0);
    if (!cap.isOpened()) {
        std::cerr << "Unable to open camera, exiting...\n";
        return -1;
    }

    cap.set(CV_CAP_PROP_FRAME_WIDTH, FRAME_WIDTH);
    cap.set(CV_CAP_PROP_FRAME_HEIGHT, FRAME_HEIGHT);

    for (;;) {
        // We can have `image` here, since `cap.read` always gives us
        // a reference to its internal buffer
        cv::Mat image;
        if (!cap.read(image)) {
            std::cerr << "Failed to read image, exiting...\n";
            break; // Failed to read image, nothing else to do
        }

        if (ms.left_button_clicked || ms.left_button_held) {
            std::cout << "Current position: " << ms.position << "\n";
            cv::circle(image, ms.position, 3, cv::Scalar(0, 0, 255));
            cv::circle(image, ms.position, 10, cv::Scalar(0, 0, 255), 2);
        }

        ms.new_iteration();

        cv::imshow(WINDOW_NAME, image);
        if (cv::waitKey(30) == 27) {
            std::cout << "Esc pressed, exiting...\n";
            break;
        }
    }
    return 0;
}