createTrackbar()创建轨迹条

函数原型如下:


[__Qt Backend Only__] winname can be empty if the trackbar should be attached to the
control panel.

Clicking the label of each trackbar enables editing the trackbar values manually.

@param trackbarname Name of the created trackbar.
@param winname Name of the window that will be used as a parent of the created trackbar.
@param 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.
@param count Maximal position of the slider. The minimal position is always 0.
@param 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.
@param userdata User data that is passed as is to the callback. It can be used to handle trackbar
events without using global variables.
 */
CV_EXPORTS int createTrackbar(const String& trackbarname, const String& winname,
                              int* value, int count,
                              TrackbarCallback onChange = 0,
                              void* userdata = 0);

createTrackbar()函数用于创建一个调整数值的滑动条,并将滑动条附加在指定的窗口上,createTrackbar()通常需要配合一个回调函数;
第一个参数,const string &类型的trackbarname,轨迹条的名字,用来代表我们创建的轨迹条。
第二参数,const string &类型的winname,窗口的名字,表示这个轨迹条会依附到那个窗口,即对应的nameWindow()创建的窗口时填的窗口名;
第三个参数,int类型的value,一个指向整型的指针,表示滑块的位置。创建时,滑块的初始位置就是改变当前的值;
第四个参数,int类型的count表示滑块可以到达的最大位置的值。滑块最小位置的值始终为0;
第五个参数TrackbarCallback类型的onChange,默认值为0,这是一个指向回调函数的指针,每次滑块改变位置时,这个函数都会进行回调。并且这个函数的原型必须为void functionName(int,void
);其中第一个参数是轨迹条的位置,第二参数是用户数据(第六参数)。如果回调是NULL指针,表示没有回调函数调用,仅第三个参数value有变化。
第六个参数,void*的userdata默认值0,这个参数是用户传给调用函数的数据,用来处理轨迹条事件。如果使用的第三个参数value是全局变量时,则完全可以不用去管这个userdata
creatTrackbar()函数为我们创建了一个具有特定名称和范围的轨迹条(Trackbar,或者说范围控制工具),指定一个和轨迹条位置同步的变量,而其要指定回调函数onChang(第五个参数),在轨迹条位置改变的时候,来调用这个回调函数,并且,创建轨迹条显示指定winname所代表的的函数窗口上;
回调函数,就是通过函数指针调用的函数。如果我们把函数的指针(地址)作为参数传递给另一函数,当这个指针被用来调用其所指向的函数时,就成为回调函数。回调函数不由该函数的实现方直接调用,而是在特定的实践或条件发生时由另外的一方调用,用于对该事件或条件的响应;

#include<iostream>
#include<opencv2/opencv.hpp>
#include<opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;
#define WINDOW_NAME "【线性混合示例】"
const int g_nMaxAlphaValue = 200;//定义AlphaValue的最大值,即滑条的最大值count;
int g_nAlphaValueSlider;/*滑动条对应位置变量,将其设置成全局变量后就可以不用考虑
						createTrackbar()最后一个参数*/
double g_dAlphaValue;//Alpha值
double g_dBetaValue;//Beta值
Mat g_scrImage1;//第一张图片
Mat g_scrImage2;//第二章图片
Mat g_dstImage;//存放融合后的图片;
void on_Trackbar(int, void*)//回调函数
{
	g_dAlphaValue = (double)g_nAlphaValueSlider / g_nMaxAlphaValue;//计算当前alpha值相对最大值得比例;
	g_dBetaValue = (1.0 - g_dAlphaValue);//Beta 值为1-aipha
	addWeighted(g_scrImage1, g_dAlphaValue, g_scrImage2, g_dBetaValue, 0.0, g_dstImage);//线性融合
	imshow(WINDOW_NAME, g_dstImage);
}
int main(int agrc, char **argv)
{
	g_scrImage1 = imread("1.jpg");
	g_scrImage2 = imread("2.png");
	if (!g_scrImage1.data&&!g_scrImage2.data)
	{
		cout << "open Error" << endl;
		return -1;
	}
	g_nAlphaValueSlider = 100;//设滑条初始值为100;
	namedWindow(WINDOW_NAME, 1);
	char Trackbarname[50];
	sprintf(Trackbarname, "透明值%d", g_nMaxAlphaValue);
	createTrackbar(Trackbarname, WINDOW_NAME, &g_nAlphaValueSlider, g_nMaxAlphaValue, on_Trackbar);
	/*creatTrackbar()的作用就是建立联系,即将名为Trackbarname的轨迹条依附到WINDOW_NAME上,同设置滑动条
	的初始位置和最大位置,最后将该轨迹条与名为on_Trackbar的函数关联起来*/
	on_Trackbar(g_nAlphaValueSlider, 0);//调用回调函数,显示融合图像;
	waitKey(0);
}

在这里插入图片描述

获取轨迹条位置

[__Qt Backend Only__] winname can be empty if the trackbar is attached to the control
panel.

@param trackbarname Name of the trackbar.
@param winname Name of the window that is the parent of the trackbar.
 */
CV_EXPORTS_W int getTrackbarPos(const String& trackbarname, const String& winname);

设置轨迹条位置

[__Qt Backend Only__] winname can be empty if the trackbar is attached to the control
panel.

@param trackbarname Name of the trackbar.
@param winname Name of the window that is the parent of trackbar.
@param pos New position.
 */
CV_EXPORTS_W void setTrackbarPos(const String& trackbarname, const String& winname, int pos);

设置轨迹条最大位置

[__Qt Backend Only__] winname can be empty if the trackbar is attached to the control
panel.

@param trackbarname Name of the trackbar.
@param winname Name of the window that is the parent of trackbar.
@param maxval New maximum position.
 */
CV_EXPORTS_W void setTrackbarMax(const String& trackbarname, const String& winname, int maxval);

设置轨迹条最小位置

[__Qt Backend Only__] winname can be empty if the trackbar is attached to the control
panel.

@param trackbarname Name of the trackbar.
@param winname Name of the window that is the parent of trackbar.
@param minval New minimum position.
 */
CV_EXPORTS_W void setTrackbarMin(const String& trackbarname, const String& winname, int minval);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值