//使用滑动条实现一个开关功能,用户可以选择打开或是关闭
//作者:sandy
//时间:2015-10-7
/**
//创建滑动条函数
int cvCreateTrackbar(
const char* trackbar_name,//滑动条的名字
const char* window_name,//滑动条的附属窗口
int* value,//当滑动条拖动时,opencv会自动将当前位置所代表的值传给这个value】】】】】】
int count,//滑动条所能表示的最大值
CvTrackbarCallback on_change //指向回调函数的指针,它不是必须的,可以为NULL。当滑动条被拖动时,回调函数会自动调用
);
//读取滚动条的位置
int cvGetTrackbarPos(
const char* trackbar_name,
const char* window_name
);
//设置滚动条的值
void cvSetTrackbarPos(
const char* trackbar_name,
consr char* window_name,
int pos
);
*/
//用滑动条选择性显示哪一张图片
#include <cv.h>
#include <highgui.h>
//设定全局变量
//
int g_switch_value=0;
IplImage* img0;
IplImage* img1;
//开关函数[在第一调,第二调中调用,也可注释掉,目前不知道它在此处的具体意义]
/**
void switch_function(int position){
cvSetTrackbarPos("Switch","视窗1",position);//设定滑动条的位置
}
*/
//回调函数
//This will be the callback that we give to the trackbar
//
void switch_callback(int position){
if(position == 0){
//switch_function(0);//第一调
cvShowImage("视窗1",img0);
}else{
//switch_function(1);//第二调
cvShowImage("视窗1",img1);
}
}
int main(int argc, char* argv[]){
cvNamedWindow("视窗1",0);//0:可以手动改变视窗大小。1:不能手动改变视窗的大小
img0=cvLoadImage("E:\\picture\\d.jpg");
img1=cvLoadImage("E:\\picture\\g.jpg");
cvShowImage("视窗1",img0);//开始加载img0
//Create the trackbar.we give it a name,
//and tell it the name of the parent window
//
cvCreateTrackbar("Switch","视窗1",&g_switch_value,1,switch_callback);//滑动条的名称为:Switch,滑动条所在的窗口叫:视窗1,滑动条位置初始默认值为:&g_switch_value=0,滑动条的最大值为:1,滑动条的回调函数为:switch_callback
//This will just cause opencv to idle until
//someone hit the "Esc" key
//
while(1){
if(cvWaitKey(15)==27) break;//按Esc退出
}
cvReleaseImage(&img0);
cvReleaseImage(&img1);
cvDestroyWindow("视窗1");
return 0;
}
![](https://images2015.cnblogs.com/blog/597602/201510/597602-20151011212756846-1684326914.png)
![](https://images2015.cnblogs.com/blog/597602/201510/597602-20151011212805534-2076863317.png)