我正在测试对三个轨迹栏使用一个回调,而不使用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;
}