示例代码
回调函数方式
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
void onTrackbarClicked(int value, void* image)
{
imshow("原图", *(Mat*)image);
std::cout << "value = " << value << std::endl;
}
int main(int argc, char const *argv[])
{
Mat image = imread("1.jpg");
namedWindow("原图");
// 创建滑动条,会根据窗口名称自动匹配
createTrackbar("大小", "原图", 0, 300, onTrackbarClicked, &image);
waitKey(0);
return 0;
}
非回调函数方式
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
int main(int argc, char const *argv[])
{
Mat image = imread("1.jpg");
namedWindow("原图");
createTrackbar("大小", "原图", 0, 300, 0);
while(waitKey(100) != 27) {
// 获取滑块位置值
int value = getTrackbarPos("大小", "原图");
imshow("原图", image);
std::cout << "value = " << value << std::endl;
}
return 0;
}
运行结果
image.png
网友评论