如何使用CV :: createButton原型的OpenCV [英] How to use cv::createButton prototype in OpenCV

查看:4092
本文介绍了如何使用CV :: createButton原型的OpenCV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解如何使用CV :: createButton这是OpenCV的文档中定义的:

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

它说,该原型是:

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套住按钮事件。

but i don't know how to define the function ButtonCallback in order to trap the button event.

我做的:

cvCreateButton("button6", callbackButton2, pointer, CV_PUSH_BUTTON, 0);

声明按钮以及

void callbackButton2(int state, void *pointer){

    printf("ok");

}

,但它不工作

我不知道第三个参数的含义是无效*用户数据。

I don't know the meaning of the third parameter "void* userdata".

有人可以帮助我,好吗?

Can someone help me, please?

感谢。

推荐答案

我们仍然不知道是什么的不工作的对你意味着什么,但我会提供有关如何使用一些信息在回调的什么用户数据的是

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.

作为签名表明, void *的用户数据是你可以用它来发送/接收数据的回调的参数。这纯粹是可选的,所以如果你没有任何使用它只是通过 NULL

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.

在下面的例子中我将使用的用户数据的检索回调的数据。您可能已经注意到,回调收到状态从OpenCV的信息。我感兴趣的是存储该值,使之适用于的main()

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

为此,我定义自定义数据类型,并宣布对这种类型的变量的main()。自定义类型有一个 INT 成员,以存储状态通过我们的回调收到,我们将使用互斥以保护自定义类型被读取/由2个线程(回调和的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;
}

这篇关于如何使用CV :: createButton原型的OpenCV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆