形式参数一、trackbarname:滑动空间的名称;
形式参数二、winname:滑动空间用于依附的图像窗口的名称;
形式参数三、value:初始化阈值;
形式参数四、count:滑动控件的刻度范围;
形式参数五、TrackbarCallback是回调函数,其定义如下:
typedef void (CV_CDECL *TrackbarCallback)(int pos, void* userdata);
使用及实现方法:
#include <opencv2/opencv.hpp> #include <iostream> using namespace std; using namespace cv; Mat src, dst; int vale = 0; int max_value = 5; RNG rng(12345); void demo_puttext(int, void*); int main(){ src = imread("D:/photo/5.jpg"); if (src.empty()) { cout << "open no success" << endl; return -1; } //创建窗口 namedWindow("show image", WINDOW_AUTOSIZE); //创建滑块 createTrackbar("value", "show image", &vale, max_value, demo_puttext); //主函数调用 demo_puttext(0,0); //循环显示 waitKey(0); return 0; } void demo_puttext(int, void*){ //模糊处理去噪声 blur(src, dst, Size(3,3), Point(-1,-1)); //定义随机颜色 Scalar color = Scalar(rng.uniform(0,255),rng.uniform(0,255),rng.uniform(0,255)); //进行条件执行 switch(vale){ case 0: putText(dst, "0", Point(50,50), CV_FONT_HERSHEY_COMPLEX, 1, color); break; case 1: putText(dst, "1", Point(50,50), CV_FONT_HERSHEY_COMPLEX, 1, color); break; case 2: putText(dst, "2", Point(50,50), CV_FONT_HERSHEY_COMPLEX, 1, color); break; case 3: putText(dst, "3", Point(50,50), CV_FONT_HERSHEY_COMPLEX, 1, color); break; case 4: putText(dst, "4", Point(50,50), CV_FONT_HERSHEY_COMPLEX, 1, color); break; case 5: putText(dst, "5", Point(50,50), CV_FONT_HERSHEY_COMPLEX, 1, color); break; } //显示 imshow("show image", dst);
}