OpenCV / C++ – OpenCV detect mouse click position and displays it, but doesn't draw a circle

Issue

I have this OpenCV Code where I want to draw a circle on a image each time i left click my mouse. It detect the position of my mouse at the time I pressed the left button and displays it aswell, but it doesn’t draw a circle at the position.

Here is the code:


#include <iostream>
#include <vector>
#include <string>

#include <opencv2/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/core.hpp>

cv::Mat inputImage;

void Draw(int event, int x, int y, int flags, void* param)
{
    if (event & cv::EVENT_LBUTTONDOWN)
    {
        std::cout << "X " << x << " Y " << y << std::endl;
        cv::circle(inputImage, cv::Point(x, y), 25, cv::Scalar(0, 255, 0), cv::FILLED, cv::LINE_AA);
    }
}

int main(int argc, char** argv)
{
    std::string path = "C:\\Users\\dfad\\Downloads\\dyp.png";
    inputImage = cv::imread(path);

    cv::namedWindow("Image");
    cv::setMouseCallback("Image", Draw, NULL);

    cv::resize(inputImage, inputImage, cv::Size(800, 800), cv::INTER_LINEAR);
    cv::circle(inputImage, cv::Point(130, 130), 10, cv::Scalar(0, 255, 0), cv::FILLED);

    cv::imshow("Image", inputImage);
    cv::waitKey(0);

    return 0;
}


Solution

Simply put:

cv::imshow("Image", inputImage); 

into a while loop, so that it updates the image being displayed after drawing the circle.

Answered By – Ann Zen

Answer Checked By – Gilberto Lyons (AngularFixing Admin)

Leave a Reply

Your email address will not be published.