学习OpenCV:createTrackbar

基于OpenCV 2.3.2 documentation,createTrackbar官方文档说明:

createTrackbar
Creates a trackbar and attaches it to the specified window.
C++: int createTrackbar(const string& trackbarname, const string& winname, int* value, int count, TrackbarCallback onChange=0, void* userdata=0)
Parameters:
trackbarname – Name of the created trackbar.
winname – Name of the window that will be used as a parent of the created trackbar.
value – Optional pointer to an integer variable whose value reflects the position of the slider. Upon creation, the slider position is defined by this variable.
count – Maximal position of the slider. The minimal position is always 0.
onChange – Pointer to the function to be called every time the slider changes position. This function should be prototyped as void Foo(int,void*); , where the first parameter is the trackbar position and the second parameter is the user data (see the next parameter). If the callback is the NULL pointer, no callbacks are called, but only value is updated.
userdata – User data that is passed as is to the callback. It can be used to handle trackbar events without using global variables.
The function createTrackbar creates a trackbar (a slider or range control) with the specified name and range, assigns a variable value to be a position syncronized with the trackbar and specifies the callback function onChange to be called on the trackbar position change. The created trackbar is displayed in the specified window winname.

翻译如下:
createTrackbar
创建一个跟踪条(轨迹条),并将跟踪条附到制定的窗口上。
C++: int createTrackbar(const string& trackbarname, const string& winname, int* value, int count, TrackbarCallback onChange=0, void* userdata=0)
参数:
trackbarname=》所创建的跟踪条的名字
winname=》跟踪条所依附的窗口的名字
value=》可选的指向整型变量的指针,整型变量的值对应于滑动条的位置。初始创建时,滑动条的值就是这个整型变量的值。
count=》滑动条最大的值。最小值总是为0。
onChange=》指向回调函数的指针,每次滚动条改变位置时,这个函数就会被调用。这个函数的原型应该为:void Foo(int, void*);其中第一个参数是跟踪条的位置,第二个参数是用户数据(见下一个参数)。如果回调为空,表示没有回调函数被调用,仅仅value会有变化。
userdata=》通过回调函数传递的用户数据。它可以控制跟踪条事件而不需要使用全局变量。
这个createTrackbar函数创建一个具有特定名称和范围的轨迹条(滚动条,或者说是滑块范围控制工具),指定一个和轨迹条位置同步的变量。而且要指定回调函数,在轨迹条位置改变的时候来调用这个回调函数。创建的轨迹条显示在指定的winname所代表的窗口上。

一般情况下,很少使用userdata这个参数,今天就以使用userdata这个参数为例,详见如下代码:

#include <iostream>
#include <opencv/cv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace std;
using namespace cv;

//定义了窗体名
string winName="二值化";

//TrackBar发生改变的回调函数
void onChangeTrackBar(int pos,void* userdata);

//主函数
int main ()
{
	//trackbar名
	string trackBarName="pos";
	//图像文件名
	string imgName="Lena.jpg";
	//trackbar的值
	int posTrackBar=0;
	//trackbar的最大值
	int maxValue=255;

	Mat img;
	//读入图像,以灰度图形式读入
	img = imread (imgName, 0);   
	//新建窗口
	namedWindow(winName);      
	imshow (winName,img); 
	//创建trackbar,我们把img作为数据传进回调函数中
	createTrackbar (trackBarName,winName,&posTrackBar,maxValue, onChangeTrackBar ,&img);

	waitKey ();
	return 0;
}

// 回调函数
void onChangeTrackBar (int pos,void* usrdata)
{
	// 强制类型转换
	Mat src = *(Mat*)(usrdata);   
	
	Mat dst;
	// 二值化
	threshold(src, dst, pos, 255, 0);
	
	imshow (winName,dst);
}

测试原图如下所示:


运行结果如下图所示:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值