如何在 OpenCV 中使用 cv::createButton 原型

发布于 2024-11-08 13:25:44 字数 864 浏览 0 评论 0原文

我想了解如何使用 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 技术交流群。

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

发布评论

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

评论(2

潦草背影 2024-11-15 13:25:44

我们仍然不知道不起作用对您意味着什么,但我将提供一些有关如何使用回调以及用户数据的信息em> 是。

正如签名所示,void* userdata 是一个可用于向回调发送/接收数据的参数。这纯粹是可选的,因此如果您没有任何用处,只需传递 NULL 即可。

在下面的示例中,我将使用 userdata 从回调中检索数据。您可能已经注意到回调从 OpenCV 接收状态信息。我有兴趣存储这个值并使其可供 main() 使用。

为此,我定义了一个自定义数据类型,并在 main() 上声明了该类型的变量。自定义类型有一个 int 成员来存储回调收到的 state 和一个互斥锁,我们将使用该互斥锁来保护自定义类型不被同时读取/写入2 个线程(回调和 main())。

#include <iostream>
#include <cv.h>
#include <highgui.h>
#include <pthread.h>
#include <string.h>

using namespace cv;

typedef struct custom_data
{
    int state;
    pthread_mutex_t mtx;
} custom_data_t;


void my_button_cb(int state, void* userdata)
{
    std::cout << "@my_button_cb" << std::endl;   

    // convert userdata to the right type
    custom_data_t* ptr = (custom_data_t*)userdata;
    if (!ptr)
    {
        std::cout << "@my_button_cb userdata is empty" << std::endl;
        return;
    }

    // lock mutex to protect data from being modified by the
    // main() thread
    pthread_mutex_lock(&ptr->mtx);

    ptr->state = state;

    // unlock mutex
    pthread_mutex_unlock(&ptr->mtx);
}    

int main()
{
    // declare and initialize our userdata
    custom_data_t my_data = { 0 };

    createButton("dummy_button", my_button_cb, &my_data, CV_PUSH_BUTTON, 0);

    // For testing purposes, go ahead and click the button to activate
    // our callback.

    // waiting for key press <enter> on the console to continue the execution
    getchar(); 

    // At this point the button exists, and our callback was called 
    // (if you clicked the button). In a real application, the button is 
    // probably going to be pressed again, and again, so we need to protect 
    // our data from being modified while we are accessing it. 
    pthread_mutex_lock(&my_data.mtx);

    std::cout << "The state retrieved by the callback is: " << my_data.state << std::endl;

    // unlock mutex    
    pthread_mutex_unlock(&my_data.mtx);

    return 0;
}

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 pass NULL.

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 to main().

For this purpose, I define a custom data type and declare a variable of this type on main(). The custom type has an int member to store the state 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 and main()).

#include <iostream>
#include <cv.h>
#include <highgui.h>
#include <pthread.h>
#include <string.h>

using namespace cv;

typedef struct custom_data
{
    int state;
    pthread_mutex_t mtx;
} custom_data_t;


void my_button_cb(int state, void* userdata)
{
    std::cout << "@my_button_cb" << std::endl;   

    // convert userdata to the right type
    custom_data_t* ptr = (custom_data_t*)userdata;
    if (!ptr)
    {
        std::cout << "@my_button_cb userdata is empty" << std::endl;
        return;
    }

    // lock mutex to protect data from being modified by the
    // main() thread
    pthread_mutex_lock(&ptr->mtx);

    ptr->state = state;

    // unlock mutex
    pthread_mutex_unlock(&ptr->mtx);
}    

int main()
{
    // declare and initialize our userdata
    custom_data_t my_data = { 0 };

    createButton("dummy_button", my_button_cb, &my_data, CV_PUSH_BUTTON, 0);

    // For testing purposes, go ahead and click the button to activate
    // our callback.

    // waiting for key press <enter> on the console to continue the execution
    getchar(); 

    // At this point the button exists, and our callback was called 
    // (if you clicked the button). In a real application, the button is 
    // probably going to be pressed again, and again, so we need to protect 
    // our data from being modified while we are accessing it. 
    pthread_mutex_lock(&my_data.mtx);

    std::cout << "The state retrieved by the callback is: " << my_data.state << std::endl;

    // unlock mutex    
    pthread_mutex_unlock(&my_data.mtx);

    return 0;
}
灵芸 2024-11-15 13:25:44

它不起作用的原因是因为 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.

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