How to use cv :: createButton prototype in OpenCV

I would like to understand how to use cv :: createButton, which is defined in the OpenCV documentation:

http://opencv.jp/opencv-2svn_org/cpp/highgui_qt_new_functions.html#cv-createbutton

It says the prototype:

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 ButtonCallback function to catch the button event.

I do:

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

to declare a button and

void callbackButton2(int state, void *pointer){

    printf("ok");

}

but that will not work.

I do not know the value of the third parameter "void * userdata".

Can someone help me please?

Thank.

+3
source share
2 answers

We still don’t know what doesn’t work for you, but I will provide some information on how to use the callback and what user data.

, void* userdata - , / . , , , NULL.

userdata . , , state OpenCV. main().

main(). int state, , , / ( 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;
}
+3

, , , cvCreateButton , - :

https://code.ros.org/trac/opencv/ticket/786

, . .

0

All Articles