2016-06-30 4 views
0

小さな黒い円の入ったシンプルな黒い画像が入ったシンプルなウィンドウがあります。私はこのサークルをドラッグアンドドロップできる簡単なコードを書いています。私はそれを正しく行うことができた。マウスを動かしながらOpenCVレンダリングを停止する

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 
    } 
} 

主な機能::MOUSE_EVENT関数内

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

問題は、私が円をドラッグして、高速に移動するために開始した場合、私は停止するまで、画像が変更されないことです。しかし、私がゆっくり進むと、動きに応じて変化します。この問題の原因は何ですか? P.S私は、遅いハードウェアの疑いが全くありません。私はワークステーションで作業していますが、プロセッサ使用率を指導しており、そのコア・リーチは約8%です。メモリはほぼ無料です。

私はWindows 10を使用しています。

答えて

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; 
} 
関連する問題