我的代码工作正常,但是当仍然按下鼠标左键时,它不能连续传递坐标。鼠标移动时确实会传递坐标。
对不起,我没有添加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;
}
答案 0 :(得分:1)
每当OS / GUI生成鼠标事件时,都会调用回调。只要鼠标的状态(按钮,滚轮,位置等)发生变化,就会生成此类事件-如果没有任何变化,则您已经知道(可以知道)当前状态。
如果您想以某种方式响应鼠标的稳定状态(例如,静止时按住按钮),则必须自己处理。
我了解您的问题的方式如下:只要按住鼠标左键,就在显示的每帧上的光标的最新位置绘制一个圆圈。
由于我们以适当的帧速率运行,因此我们有能力保持简单,并且仅根据前一帧的点击/位置信息更新当前帧。用户将无法观察到这种轻微的延迟。
仅出于后代考虑:正在显示图像,并且仅在cv::waitKey
函数运行时调用鼠标回调。
以下列举了每次迭代中可能遇到的情况以及在以下框架中我们应如何应对它们:
waitKey
的入口和出口被保留
waitKey
的入口处持有,而是在出口处持有
waitKey
的入口处保留,但不保留
waitKey
的出入境口举行
基于此,我们需要跟踪:
我们可以使用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
。
带有一些原始调试跟踪的回调示例实现如下:
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);
处理循环将非常简单:
如下所示:
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;
}