Ask Your Question
0

Trackbar pass variable

asked 2016-03-31 04:27:08 -0600

Nbb gravatar image

updated 2016-03-31 05:00:01 -0600

Hello,

How do I pass a variable using a trackbar ? I do not want it to be a global variable like http://docs.opencv.org/2.4/doc/tutori... Id like it to be something like below

createTrackbar( TrackbarName, "Linear  Blend", &alpha_slider, alpha_slider_max, on_trackbar);
createTrackbar( TrackbarName, "Linear  Blend", &beta_slider, alpha_slider_max, on_trackbar);


void on_trackbar(int in, void*) {

int slider_val= *((int*)&in);

//How do I print the current slider value of alpha and beta ?
//cout << alpha_slider 
//cout << beta_slider
}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2016-03-31 04:37:43 -0600

updated 2016-04-01 04:05:53 -0600

If you take a look at the most recent documentation you can see that you can supply a user defined pointer to an object, defined as a void pointer.

If it handles a single value, you can pass that around using its pointer. If you want to pass multiple data, then you will need to construct a struct and pass all the data with a pointer to the struct. An example is below:

int ref = 1;
createTrackbar("Name","Window",0, 1, myFunc, &ref);

void myFunc(int value, void *userdata)
{
    // Retrieve the data
    // Needs explicit type declaration
    int val = *((int*)&userdata);
}

And more general you can do it as below

cv:createTrackbar("Label", "Window" &variable, MAX_VAL, &MyClass::func, this);

static void MyClass:func(int newValue, void * object) {
    MyClass* myClass = (MyClass*) object;
    // ...do stuff.
}

To add multiple variables you will need to create a struct and provide a pointer to that

struct myData{
   int variable1 = 0;
   int variable2 = 0;
}

void myFunc(int value, void *userdata)
{
     // Retrieve the data
     // Needs explicit type declaration
     myData pushed_data = *((myData*)&userdata);
}

myData test;
test.variable1 = 5;
test.variable2 = 17;

createTrackbar("Name","Window",0, 1, myFunc, &test);
edit flag offensive delete link more

Comments

Thanks ! The line int val = ((int)&userdata); worked

May I know how i can instead have 2 trackbars that will pass their variables into the function ? I am only able to print the value that was last changed by one of the two trackbars.

Nbb gravatar imageNbb ( 2016-03-31 04:58:24 -0600 )edit

Updated the answers with a possible solution! Simply add the same function then to both trackbars and look for changes in the data of the struct.

StevenPuttemans gravatar imageStevenPuttemans ( 2016-03-31 06:26:53 -0600 )edit

in the class example, it should be static void MyClass:func(int newValue, void * object) .

berak gravatar imageberak ( 2016-04-01 02:06:06 -0600 )edit

Why should I define it as static? Any reason for that?

StevenPuttemans gravatar imageStevenPuttemans ( 2016-04-01 03:44:05 -0600 )edit
1

unless it's static, you need the this pointer to call MyClass::func. (and the compiler does not know, where "this" is coming from. just try to compile it ;)

berak gravatar imageberak ( 2016-04-01 03:56:01 -0600 )edit

in this line myData pushed_data = *((myData*)&userdata);

should change to myData pushed_data = *((myData*)userdata);

This pointer value save your data address. We're not going to use this pointer address. so chage this line. Code will work.

Q36061355 gravatar imageQ36061355 ( 2019-04-20 01:37:31 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-03-31 04:27:08 -0600

Seen: 2,543 times

Last updated: Apr 01 '16