2017-11-11 63 views
0

所以這是我在openCV中設置MouseEvent的嘗試,我的觀點是根據鼠標事件在給定點繪製一些東西。任何想法有什麼不對? 我跟着這個教程:https://docs.opencv.org/3.3.0/db/d5b/tutorial_py_mouse_handling.html使用鼠標作爲畫筆

 #include<opencv2\core\core.hpp> 
     #include<opencv2\imgproc\imgproc.hpp> 
     #include<opencv2\highgui\highgui.hpp> 
     #include<opencv2\ml\ml.hpp> 
     #include<iostream> 
     #include<conio.h> 
     using namespace std; 
     using namespace cv; 
     int radius = 5; 
     Mat img; 
     int ix = 1; 
     int iy = 1; 
     //mouse callback function 
     void drawCircle(int event, int x, int y, int, void* param) { 
      if (event == CV_EVENT_LBUTTONDOWN) 
      { 
       cout << "x = " << x 
        << "\t y = " << y 
        << endl; 
       circle(img, Point(x, y), 10, Scalar(255,255,255), 10); 
      } 
      //line(img, Point(10, 10), Point(x, y), Scalar(0, 0, 255), 3); 
     } 
     int main() { 
      img = imread("image.png"); 
      if (img.empty()) { 
       cout << "\nerror reading image" << endl; 
       _getch(); 
       return -1; 
      } 
      namedWindow("Image",1); 
      imshow("Image", img); 
      setMouseCallback("Image", drawCircle); 
      waitKey(0); 
      return 0; 
     } 

爲什麼它不畫我的圖像上的圓或線?謝謝!

回答

0

您可能需要強制重畫窗口以反映更改。即通過類似更換您waitKey(0):

while (waitKey(20) != 27) // wait until ESC is pressed 
{ 
    imshow("Image", img); 
} 

或者,您可以只需添加一個調用imshow到畫圓回調。

+0

感謝您的回覆。可悲的是,我沒有足夠的特權來滿足您的答案。我改變了waitKey(20)!= 27(ESC),你的代碼只是讓窗口立即關閉。還是非常感謝這個建議。 –

+0

謝謝,我將它改爲您的修補程序。不要擔心選票:-) –