Many sliders with one callback

callback, opencv, slider, trackbar

Solution

Yes you should be able to do this (at least with the C++ interface). You will want to utilize the optional `userData` field. Below is a small example of how you can accomplish this:

#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace std;
using namespace cv;

struct ColorThresholdData
{
    int redHigh;
    int redLow;
};

enum ColorThresholdType
{
    RED_HIGH,
    RED_LOW
};

void fooCallback(int value, void* colorThreshold);

struct ColorThresholdData data;
int main(int argc, char** argv)
{
    ...
    createTrackbar("red high", windowName, NULL, 255, fooCallback, new ColorThresholdType(RED_HIGH));
    createTrackbar("red low", windowName, NULL, 255, fooCallback, new ColorThresholdType(RED_LOW));
    ...
}

void fooCallback(int value, void* colorThreshold)
{
    ColorThresholdType* ct = reinterpret_cast<ColorThresholdType*>(colorThreshold);
    switch(*ct)
    {
    case RED_HIGH:
        cout << "Got RED_HIGH value" << endl;
        data.redHigh = value;
        break;
    case RED_LOW:
        cout << "Got RED_LOW value" << endl;
        data.redLow = value;
        break;
    }
}

Hope that is what you were looking for :)

Problem

Is it possible to create some sliders and make one callback for all of them? I am creating a window, in which i would like to set about 10 parameters. It would be much better to have 1 callback function for all of them instead of 10 functions. currently i create trackbar like this: ``` cvCreateTrackbar("Var1","Window",&global_var1, 250, changing_var1); cvCreateTrackbar("Var2","Window",&global_var2, 250, changing_var2); ``` and then ``` void changing_var1(int pos) { global_var1 = pos; } void changing_var2(int pos) { global_var2 = pos; } ``` Is it possible to create one callback that would be albe to change all parameters according to which parameter i want to change?

Original source