鼠标移动时OpenCV停止渲染

时间:2016-06-30 20:56:27

标签: c++ 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;
    }
}

问题是,如果我拖动圆圈并开始快速移动,图像将不会改变,直到我停止。但是,如果我慢慢走,它将根据移动而改变。这个问题的原因是什么? P.S我根本不怀疑硬件速度慢。我正在工作站工作,我正在指导处理器利用率,它的8核心范围之一只有50%左右,内存几乎是免费的。

如果有帮助,我正在使用Windows 10.

1 个答案:

答案 0 :(得分:1)

您可以测试以下代码。(改编自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;
}