gpt4 book ai didi

opencv - OpenCV C++在图像上绘图

转载 作者:行者123 更新时间:2023-12-02 16:49:51 26 4
gpt4 key购买 nike

我试图在单击鼠标后在图像上绘制一个圆圈。

#include <stdio.h>
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/nonfree/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

using namespace cv;


static Mat img_object;

static void onMouse(int event, int x, int y, int, void*){
Point pt1 = Point(x, y);
circle(img_object, pt1, 1, Scalar(0, 255, 0), 100, 8, 0);
imshow("Display window", img_object);
}

int main(int argc, char** argv)
{
img_object = imread("pic.jpg");

setMouseCallback("Display window", onMouse, 0);

imshow("Display window", img_object);

waitKey(0);

return 0;
}

当我运行它时,显示图像,但是单击图像时什么也没有发生。

最佳答案

您要重新声明本地Mat:

static Mat img_object = imread("pic.jpg");
内部主体,从而掩盖了全局主体。
将该行替换为:
img_object = imread("pic.jpg");
一切都会好起来的。
----
更好的是,跳过全局对象,并将垫子传递给回调函数:
using namespace cv;

static void onMouse(int event, int x, int y, int, void* imgptr){
if ( event != 1 ) return; // only draw on lmouse down
Mat & img = (*(Mat*)imgptr)); // first cast, then deref
Point pt1 = Point(x, y);
circle(img, pt1, 1, Scalar(0, 255, 0), 100, 8, 0);
imshow("Display window", img);
waitKey(1);
}

int main(int argc, char** argv)
{
namedWindow("Display window",0);
Mat img = imread("pic.jpg");
setMouseCallback("Display window", onMouse, &img); // pass ptr to mat here
imshow("Display window", img);
waitKey(0);
return 0;
}

关于opencv - OpenCV C++在图像上绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21079758/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com