用鼠标作为画笔

时间:2017-11-11 06:50:16

标签: c++ opencv

所以这是我尝试在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;
        }

为什么不在我的图像上画圆圈或线条?谢谢!

1 个答案:

答案 0 :(得分:0)

您可能需要强制重绘窗口以反映更改。即用以下内容替换你的waitKey(0):

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

或者,您也可以将一个imshow调用添加到drawCircle回调中。