OpenCV在鼠标移动时停止渲染



我有一个简单的窗口,包含简单的黑色图像,里面有一个小的实心圆。我写了一个简单的代码,可以拖放这个圆圈。我可以做对。mouse_event函数内部:

void on_mouse_event(int event_type, int x, int y, int flags, void*){
    if (event_type == cv::EVENT_RBUTTONDOWN){
        //Catch the circle
    }
    else if (event_type == cv::EVENT_MOUSEMOVE){
        //Release the point
    }
    else if (event_type == cv::EVENT_MOUSEMOVE){
        //Change circle position according to curser moving
        //re draw the circle again
        //show the new image
    }
}

主要功能:

while (true){
    //show image code (simple cv::imshow);
    if (cv::waitKey(1) == 27){
        break;
    }
}

问题是,如果我拖动圆圈并开始快速移动,图像将不会改变,直到我停止。然而,如果我走得慢一点,它会随着移动而变化。这个问题的原因是什么?附言:我一点也不怀疑慢速硬件。我在工作站工作,我正在指导处理器利用率,它的8核中只有一个达到了50%左右,内存几乎是空闲的。

如果有帮助的话,我用的是Windows 10。

您可以测试以下代码。(改编自opencv_annotation.cpp)

#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
using namespace std;
using namespace cv;
// Function prototypes
void on_mouse(int, int, int, int, void*);
// Public parameters
Mat image(600, 800, CV_8UC3, Scalar(220, 220, 220));
Mat current_view;
int circle_center_x = image.cols / 2, circle_center_y = image.rows / 2, radius = 40;
bool dragging = false;
const string window_name = "OpenCV Mouse Event Demo";
void on_mouse(int event, int x, int y, int, void *)
{
    // Action when left button is clicked
    if (event == EVENT_LBUTTONDOWN & (abs(circle_center_x - x) < 20) & (abs(circle_center_y - y) < 20))
    {
        dragging = true;
    }
    if (event == EVENT_LBUTTONUP)
    {
        dragging = false;
    }
    // Action when mouse is moving
    if ((event == EVENT_MOUSEMOVE) && dragging)
    {
        image.copyTo(current_view);
        circle_center_x = x;
        circle_center_y = y;
        circle(current_view, Point(circle_center_x, circle_center_y), radius, Scalar(255, 0, 0), 5);
        imshow(window_name, current_view);
    }
}
int main(int argc, const char** argv)
{
    // Init window interface and couple mouse actions
    namedWindow(window_name, WINDOW_AUTOSIZE);
    setMouseCallback(window_name, on_mouse);
    image.copyTo(current_view);
    circle(current_view, Point(circle_center_x, circle_center_y), radius, Scalar(255, 0, 0), 5);
    imshow(window_name, current_view);
    int key_pressed = 0;
    do
    {
        // Keys for processing
        // Based on the universal ASCII code of the keystroke: http://www.asciitable.com/
        //      <SPACE> = 32    add circle to current image
        //      <ESC> = 27      exit program
        key_pressed = 0xFF & waitKey(0);
        if (key_pressed==32)
        {
            // draw a circle on the image
            circle(image, Point(circle_center_x, circle_center_y), radius, Scalar(0, 0, 255), -1);
            image.copyTo(current_view);
            circle(current_view, Point(circle_center_x, circle_center_y), radius, Scalar(255, 0, 0), 5);
            imshow(window_name, current_view);
        }
    }
    // Continue as long as the <ESC> key has not been pressed
    while (key_pressed != 27);
    // Close down the window
    destroyWindow(window_name);
    return 0;
}

相关内容

  • 没有找到相关文章

最新更新