Opencv 滑动条 createTrackbar

【转载】

基于OpenCV2.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

#include 

#include 

#include 

usingnamespacestd;

usingnamespacecv;

//定义了窗体名

string winName="二值化";

//TrackBar发生改变的回调函数

voidonChangeTrackBar(intpos,void* userdata);

//主函数

intmain ()

{

//trackbar名

string trackBarName="pos";

//图像文件名

string imgName="Lena.jpg";

//trackbar的值

intposTrackBar=0;

//trackbar的最大值

intmaxValue=255;

Mat img;

//读入图像,以灰度图形式读入

img = imread (imgName, 0);

//新建窗口

namedWindow(winName);

imshow (winName,img);

//创建trackbar,我们把img作为数据传进回调函数中

createTrackbar (trackBarName,winName,&posTrackBar,maxValue, onChangeTrackBar ,&img);

waitKey ();

return0;

}

// 回调函数

voidonChangeTrackBar (intpos,void* usrdata)

{

// 强制类型转换

Mat src = *(Mat*)(usrdata);

Mat dst;

// 二值化

threshold(src, dst, pos, 255, 0);

imshow (winName,dst);

}

你可能感兴趣的:(Opencv 滑动条 createTrackbar)