Opencv 具有一个回调的多个滑块

Opencv 具有一个回调的多个滑块,opencv,callback,slider,trackbar,Opencv,Callback,Slider,Trackbar,是否可以创建一些滑块并对所有滑块进行一次回调 我正在创建一个窗口,我想在其中设置大约10个参数。最好为所有函数都设置一个回调函数,而不是10个函数 目前我创建的轨迹栏如下所示: cvCreateTrackbar("Var1","Window",&global_var1, 250, changing_var1); cvCreateTrackbar("Var2","Window",&global_var2, 250, changing_var2); 然后 void changing

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

我正在创建一个窗口,我想在其中设置大约10个参数。最好为所有函数都设置一个回调函数,而不是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;
}

是否有可能创建一个回调,它将根据参数改变所有参数?

< P>是的,你应该能够做到这一点(至少是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;
    }
}
#包括
#包括
使用名称空间std;
使用名称空间cv;
结构ColorThresholdData
{
int-redHigh;
int-redLow;
};
枚举ColorThresholdType
{
红濸高中,
红鲠低
};
void fooCallback(int值,void*colorThreshold);
结构ColorThresholdData数据;
int main(int argc,字符**argv)
{
...
createTrackbar(“红色高”,windowName,NULL,255,fooCallback,新的ColorThresholdType(红色高));
createTrackbar(“红色低”,windowName,NULL,255,fooCallback,新的ColorThresholdType(红色低));
...
}
void fooCallback(int值,void*colorThreshold)
{
ColorThresholdType*ct=重新解释投射(colorThreshold);
开关(*ct)
{
案件红色_偏高:

cout我对三个轨迹条使用一个回调进行测试,而不使用
userData
字段。它似乎有效

看看我的代码(与轨迹栏相关的部分,但有足够的复制粘贴编译,以便其他人可以轻松测试):

#包括
#包括
使用名称空间cv;
//轨迹栏的全局变量:初始和最大位置
常数int alpha_sl_max=1000;
int alpha_sl=500;
常数int beta_sl_max=500;
int beta_sl=250;
常数int伽马值=1000;
int gamma_sl=500;
double alpha,beta,gamma;//我将把滑块值转换成有用的值
//控制窗口(显示轨迹栏和文本)和文本选项
Mat img_ctrl;//我将在其中打印文本的空白图像
int ctrl_w=640,ctrl_h=150;
int font=font\u HERSHEY\u SIMPLEX;
int txt_size=1;
标量txt_颜色=(0,0,255);
int-txt_-thick=2;
int linetype=LINE_AA;
#定义TXT\U配置字体、TXT\U大小、TXT\U颜色、TXT\U粗细、线型
//来自三个轨迹栏的相同回调
_轨迹栏上的静态空白(int,void*){
alpha=(双)alpha_sl/500.0;
β=(双)β-250;
伽马=功率((双)伽马/500.0),4;
img\u ctrl=Mat::零(ctrl\u h、ctrl\u w、CV\u 8U);
putText(img\u ctrl,std::to\u string(alpha),Point(10,30),TXT\u CONFIG);
putText(img\u ctrl,std::to\u string(beta),Point(10,60),TXT\u CONFIG);
putText(img\u ctrl,std::to\u字符串(gamma),点(10,90),TXT\u配置);
imshow(“控制”,img_ctrl);
}
int main(){
//创建轨迹栏和窗口
namedWindow(“控件”);//创建窗口
调整窗口大小(“控件”,ctrl\u w,ctrl\u h);
createTrackbar(“Alpha”、“控件”和Alpha_sl,Alpha_sl_max,在_轨迹栏上);
createTrackbar(“Beta”、“Controls”和Beta_sl、Beta_sl_max,在轨迹栏上);
createTrackbar(“Gamma”、“控件”和Gamma_sl、Gamma_sl_max,在轨迹栏上);
//第一次调用,显示图像(查看回调函数)
在轨迹栏上(alpha_sl,0);
waitKey();
返回0;
}

无需为全局变量赋值。该函数已将值放入全局变量中,这就是它使用指针的原因。
#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;
}