OPENCV / C++ - OPENCV检测鼠标单击位置并显示它,但没有绘制一个圆圈

发布于 2025-01-27 14:38:16 字数 1165 浏览 4 评论 0原文

我有此OPENCV代码,每次我左键单击鼠标时,我都想在图像上绘制一个圆圈。它在我按下左键并显示同样的鼠标时检测鼠标的位置,但它不会在位置绘制一个圆圈。

这是代码:


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


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


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北斗星光 2025-02-03 14:38:16

简单地说:

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

循环中的中,以便更新绘制圆圈后要显示的图像。

Simply put:

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文