如何在 OpenCV 中使用 cv::createButton 原型
我想了解如何使用 OpenCV 文档中定义的 cv::createButton:
http://opencv.jp/opencv-2svn_org/cpp/highgui_qt_new_functions.html#cv-createbutton
它说原型是:
createButton(const string& button_name CV_DEFAULT(NULL), ButtonCallback on_change CV_DEFAULT(NULL), void* userdata CV_DEFAULT(NULL), int button_type CV_DEFAULT(CV_PUSH_BUTTON), int initial_button_state CV_DEFAULT(0)
但我不知道如何定义函数 ButtonCallback 以便捕获按钮事件。
我这样做:
cvCreateButton("button6", callbackButton2, pointer, CV_PUSH_BUTTON, 0);
声明按钮,
void callbackButton2(int state, void *pointer){
printf("ok");
}
但它不起作用。
我不知道第三个参数“void * userdata”的含义。
有人可以帮我吗?
谢谢。
I'd like to understand how to use cv::createButton which is defined in OpenCV documentation:
http://opencv.jp/opencv-2svn_org/cpp/highgui_qt_new_functions.html#cv-createbutton
It says that the prototype is:
createButton(const string& button_name CV_DEFAULT(NULL), ButtonCallback on_change CV_DEFAULT(NULL), void* userdata CV_DEFAULT(NULL), int button_type CV_DEFAULT(CV_PUSH_BUTTON), int initial_button_state CV_DEFAULT(0)
but i don't know how to define the function ButtonCallback in order to trap the button event.
I do:
cvCreateButton("button6", callbackButton2, pointer, CV_PUSH_BUTTON, 0);
to declare the button and
void callbackButton2(int state, void *pointer){
printf("ok");
}
but it doesn't work.
I don't know the meaning of the third parameter "void* userdata".
Can someone help me, please?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我们仍然不知道不起作用对您意味着什么,但我将提供一些有关如何使用回调以及用户数据的信息em> 是。
正如签名所示,
void* userdata
是一个可用于向回调发送/接收数据的参数。这纯粹是可选的,因此如果您没有任何用处,只需传递NULL
即可。在下面的示例中,我将使用 userdata 从回调中检索数据。您可能已经注意到回调从 OpenCV 接收
状态
信息。我有兴趣存储这个值并使其可供main()
使用。为此,我定义了一个自定义数据类型,并在
main()
上声明了该类型的变量。自定义类型有一个int
成员来存储回调收到的state
和一个互斥锁,我们将使用该互斥锁来保护自定义类型不被同时读取/写入2 个线程(回调和 main())。We still don't know what doesn't work means to you, but I'll provide some information on how to use the callback and what userdata is.
As the signature suggests,
void* userdata
is a parameter that you can use to send/receive data to the callback. This is purely optional, so if you don't have any use for it just passNULL
.On the example below I'll be using userdata to retrieve data from the callback. You might have noticed that the callback receives a
state
information from OpenCV. I'm interested in storing this value and making it available tomain()
.For this purpose, I define a custom data type and declare a variable of this type on
main()
. The custom type has anint
member to store thestate
received by our callback and a mutex that we are going to use to protect the custom type from being read/written simultaneously by the 2 threads (callback andmain()
).它不起作用的原因是因为
cvCreateButton
实现有一个错误,导致按钮点击被忽略:https://code.ros.org/trac/opencv/ticket/786
现在已在主干中修复,因此如果您单击按钮,现在应该可以再次正常工作使用后备箱。它最终应该会进入正式版本。
The reason it didn't work, is because the
cvCreateButton
implementation had a bug which caused button clicks to be ignored :https://code.ros.org/trac/opencv/ticket/786
This is now fixed in trunk, so a button click should now be working fine again if you use trunk. It should eventually make its way into an official release.