OpenCV从鼠标回调函数返回值 [英] OpenCV Return value from mouse callback function
本文介绍了OpenCV从鼠标回调函数返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在OpenCV中,我想将Point(x,y)之类的点位置返回给main()函数,该函数在鼠标回调函数中单击图像.除了设置全局变量之外,还有其他方法吗?
In OpenCV I want to return the point position like Point(x,y) to the main() function that I click on the image in the mouse callback function . Is there anyway other than setting a global variable?
我不想在on_mouse()函数中编写所有代码.
I don't want to write all the codes inside the on_mouse() function.
谢谢
推荐答案
通过将指向数据的指针作为参数传递给setMouseCallback()
,可以避免使用全局变量.同意@berek,只是想在下面显示一个完整的示例,以避免混淆全局变量.
You can avoid using global variables by passing a pointer to your data as a parameter to setMouseCallback()
. Agree with @berek, just wanted to show a full example below to avoid confusion about global variables.
using namespace cv;
void on_mouse( int e, int x, int y, int d, void *ptr )
{
Point*p = (Point*)ptr;
p->x = x;
p->y = y;
}
in main() {
Point p;
namedWindow("window");
Mat image = imread("someimage.jpg");
imshow(image);
//pass a pointer to `p` as parameter
setMouseCallback("window",on_mouse, &p );
// p will update with new mouse-click image coordinates
// whenever user clicks on the image window
}
这篇关于OpenCV从鼠标回调函数返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文