【问题标题】:Many sliders with one callback一个回调的多个滑块
【发布时间】:2012-10-21 11:37:29
【问题描述】:

是否可以创建一些滑块并为所有滑块进行一次回调?

我正在创建一个窗口,我想在其中设置大约 10 个参数。最好为所有这些函数设置 1 个回调函数,而不是 10 个函数。

目前我创建这样的轨迹栏:

cvCreateTrackbar("Var1","Window",&global_var1, 250, changing_var1);
cvCreateTrackbar("Var2","Window",&global_var2, 250, changing_var2);

然后

void changing_var1(int pos) {
    global_var1 = pos;
}    

void changing_var2(int pos) {
    global_var2 = pos;
}

是否可以创建一个回调来根据我要更改的参数更改所有参数?

【问题讨论】:

  • 不需要为全局变量赋值。该函数已经将值放在 global_var1 中,这就是它需要指针的原因。

标签: opencv callback slider trackbar


【解决方案1】:

是的,您应该能够做到这一点(至少使用 C++ 接口)。您将需要使用可选的userData 字段。下面是一个小例子,说明如何做到这一点:

#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;
    }
}

希望这就是您想要的:)

【讨论】:

    【解决方案2】:

    我正在测试对三个轨迹栏使用一个回调,而不使用userData 字段。它似乎有效。

    看我的代码(跟trackbars相关的部分,不过复制粘贴编译够了,其他人可以轻松测试):

    #include <opencv2/imgproc.hpp>
    #include <opencv2/highgui.hpp>
    using namespace cv;
    
    // global variables for trackbars: initial and max positions
    const int alpha_sl_max = 1000;
    int alpha_sl = 500;
    const int beta_sl_max = 500;
    int beta_sl = 250;
    const int gamma_sl_max = 1000;
    int gamma_sl = 500;
    double alpha, beta, gamma; // I will convert slider value to something useful
    
    // Control window (to show trackbars and text) and text options
    Mat img_ctrl; // blank image where I will print the text
    int ctrl_w = 640, ctrl_h = 150;
    int font = FONT_HERSHEY_SIMPLEX;
    int txt_size = 1;
    Scalar txt_color = (0, 0, 255);
    int txt_thick = 2;
    int linetype = LINE_AA;
    #define TXT_CONFIG font, txt_size, txt_color, txt_thick, linetype
    
    // same callback from three trackbars
    static void on_trackbar(int, void*) {
        alpha = (double)alpha_sl / 500.0;
        beta = (double)beta_sl - 250;
        gamma = pow(((double)gamma_sl / 500.0), 4);
        img_ctrl = Mat::zeros(ctrl_h, ctrl_w, CV_8U);
    
        putText(img_ctrl, std::to_string(alpha), Point(10, 30), TXT_CONFIG);
        putText(img_ctrl, std::to_string(beta), Point(10, 60), TXT_CONFIG);
        putText(img_ctrl, std::to_string(gamma), Point(10, 90), TXT_CONFIG);
        imshow("Controls", img_ctrl);
    }
    
    int main() {
        // Create trackbars and window
        namedWindow("Controls"); // Create Window
        resizeWindow("Controls", ctrl_w, ctrl_h);
        createTrackbar("Alpha", "Controls", &alpha_sl, alpha_sl_max, on_trackbar);
        createTrackbar("Beta", "Controls", &beta_sl, beta_sl_max, on_trackbar);
        createTrackbar("Gamma", "Controls", &gamma_sl, gamma_sl_max, on_trackbar);
    
        // call first time, to show image (view callback function)
        on_trackbar(alpha_sl, 0);
    
        waitKey();
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2021-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多