是否可以创建一些滑块并为所有滑块进行一次回调?
我正在创建一个窗口,我想在其中设置大约10个参数。对所有这些函数而不是10个函数都有1个回调函数会好得多。
目前我创建了这样的跟踪栏:
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;
}
是否可以根据我想要更改的参数创建一个回调来改变所有参数?
答案 0 :(得分:4)
是的,您应该能够这样做(至少使用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;
}
}
希望这就是你要找的东西:)
答案 1 :(得分:0)
我正在测试对三个跟踪栏使用一个回调,而不使用userData
字段。似乎可行。
看一下我的代码(与轨迹栏相关的部分,但是有足够的复制粘贴编译功能,因此其他人可以轻松地对其进行测试):
#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;
}