滚动条操作

版权声明:本文为QMec原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_41995541/article/details/89844173

摘要

配置环境为OpenCV3.4.1+VS2017的关于滚动条的创建的实验

第三章

单独创建滚动条

#include <opencv2/opencv.hpp>
using namespace cv;

#define WINDOW_NAME "【视频滑动条的创建】"  //为窗口标题定义的宏 

//全局变量声明
const int g_nMaxFrame = 100;//视频帧数的最大值
int g_nValueSlider;			//滑动条对应的变量

int main(int argc, char* argv)
{
	//设置滚动条初值
	g_nValueSlider = 25;
	//创建一个窗口
	namedWindow(WINDOW_NAME);
	//设置滚动条名称
	char TrackbarName[50] = "当前帧";
	//创建滚动条
	createTrackbar(TrackbarName,WINDOW_NAME, &g_nValueSlider, g_nMaxFrame,NULL);//无回调函数的使用
	waitKey();
	return 0; 
}
  • 使用回调函数的效果
//添加一个回调函数
void on_Trackbar(int, void*)
{
	printf("%d\t", g_nValueSlider);
}

回调函数

图片滑动条的创建和使用

  • P73 创建滑动条:createTrackbar()函数
//--------------------------------------【程序说明】-------------------------------------------
//		程序说明:《OpenCV3编程入门》OpenCV2版书本配套示例程序17
//		程序描述:为程序界面添加滑动条
//		开发测试所用IDE版本:Visual Studio 2010
//		开发测试所用OpenCV版本:	3.0 beta
//		2014年11月 Created by @浅墨_毛星云
//		2014年12月 Revised by @浅墨_毛星云
//------------------------------------------------------------------------------------------------


//---------------------------------【头文件、命名空间包含部分】-------------------------------
//		描述:包含程序所使用的头文件和命名空间
//-------------------------------------------------------------------------------------------------
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;

//-----------------------------------【宏定义部分】-------------------------------------------- 
//  描述:定义一些辅助宏 
//------------------------------------------------------------------------------------------------ 
#define WINDOW_NAME "【滑动条的创建&线性混合示例】"        //为窗口标题定义的宏 


//-----------------------------------【全局变量声明部分】--------------------------------------
//		描述:全局变量声明
//-----------------------------------------------------------------------------------------------
const int g_nMaxAlphaValue = 100;//Alpha值的最大值
int g_nAlphaValueSlider;//滑动条对应的变量
double g_dAlphaValue;
double g_dBetaValue;

//声明存储图像的变量
Mat g_srcImage1;
Mat g_srcImage2;
Mat g_dstImage;


//-----------------------------------【on_Trackbar( )函数】--------------------------------
//		描述:响应滑动条的回调函数
//------------------------------------------------------------------------------------------
void on_Trackbar(int, void*)
{
	//求出当前alpha值相对于最大值的比例
	g_dAlphaValue = (double)g_nAlphaValueSlider / g_nMaxAlphaValue;
	//则beta值为1减去alpha值
	g_dBetaValue = (1.0 - g_dAlphaValue);

	//根据alpha和beta值进行线性混合
	addWeighted(g_srcImage1, g_dAlphaValue, g_srcImage2, g_dBetaValue, 0.0, g_dstImage);

	//显示效果图
	imshow(WINDOW_NAME, g_dstImage);
}

//-----------------------------------【ShowHelpText( )函数】----------------------------------
//		描述:输出一些帮助信息
//----------------------------------------------------------------------------------------------
void ShowHelpText()
{
	//输出欢迎信息和OpenCV版本
	printf("\n\n\t\t\t非常感谢购买《OpenCV3编程入门》一书!\n");
	printf("\n\n\t\t\t此为本书OpenCV3版的第17个配套示例程序\n");
	printf("\n\n\t\t\t   当前使用的OpenCV版本为:" CV_VERSION);  //输出常量不用加逗号
	printf("\n\n  ----------------------------------------------------------------------------\n");
}


//--------------------------------------【main( )函数】-----------------------------------------
//		描述:控制台应用程序的入口函数,我们的程序从这里开始执行
//-----------------------------------------------------------------------------------------------
int main(int argc, char** argv)
{

	//显示帮助信息
	ShowHelpText();

	//加载图像 (两图像的尺寸需相同)
	g_srcImage1 = imread("1.jpg");
	g_srcImage2 = imread("2.jpg");
	if (!g_srcImage1.data) { printf("读取第一幅图片错误,请确定目录下是否有imread函数指定图片存在~! \n"); return -1; }
	if (!g_srcImage2.data) { printf("读取第二幅图片错误,请确定目录下是否有imread函数指定图片存在~!\n"); return -1; }

	//设置滑动条初值为70
	g_nAlphaValueSlider = 70;

	//创建窗体
	namedWindow(WINDOW_NAME, 1);

	//在创建的窗体中创建一个滑动条控件
	char TrackbarName[50];
	sprintf_s(TrackbarName, "透明值 %d", g_nMaxAlphaValue);

	createTrackbar(TrackbarName, WINDOW_NAME, &g_nAlphaValueSlider, g_nMaxAlphaValue, on_Trackbar);

	//结果在回调函数中显示
	on_Trackbar(g_nAlphaValueSlider, 0);

	//按任意键退出
	waitKey(0);

	return 0;
}

creatTrackbar()函数

//97行
createTrackbar(TrackbarName, WINDOW_NAME, &g_nAlphaValueSlider, g_nMaxAlphaValue, on_Trackbar);
//定义
CV_EXPORTS int createTrackbar(const String& trackbarname, const String& winname,
                              int* value, int count,
                              TrackbarCallback onChange = 0,
                              void* userdata = 0);
  • 参数一:滑块的名字

  • 参数二:依附的窗口的名字

  • 参数三:是滑块的当前位置 **& **用来传参回去

  • 参数四:滚动条最大值,最小位置始终为0

  • 参数五:回调函数,默认值0(NULL),若有,函数原型必须是void XXXX(int , void*)

    • 参数一:轨迹条的位置
    • 参数二:用户数据(看参数六)
    • 回调函数:就是当滚动条的位置改变时调用的事件处理函数(官方定义:
  • 参数六:用户数据,默认值0 ,传回给回调函数的参数,用来处理轨迹条事件,若参数三是全局变量,此参可省略

旧版是cvCreatTrackbar();

/** @brief Creates a trackbar and attaches it to the specified window.

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 synchronized 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.

@note

[__Qt Backend Only__] winname can be empty (or NULL) 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);

addWeighted()函数

//48行
//根据alpha和beta值进行线性混合,要求两幅图片尺寸一致
addWeighted(g_srcImage1, g_dAlphaValue, g_srcImage2, g_dBetaValue, 0.0, g_dstImage);
//定义
CV_EXPORTS_W void addWeighted(InputArray src1, double alpha, InputArray src2,
                              double beta, double gamma, OutputArray dst, int dtype = -1);
  • 参数一:图像一
  • 参数二:图像一的权值(占比)
  • 参数三:图像二
  • 参数四:图像二的权值(占比)
  • 参数五:外加权值
  • 参数六:合成的图像
  • 计算公式:dst = src1alpha + src2beta + gamma;
/** @example AddingImagesTrackbar.cpp
 */
/** @brief Calculates the weighted sum of two arrays.

The function addWeighted calculates the weighted sum of two arrays as follows:
\f[\texttt{dst} (I)= \texttt{saturate} ( \texttt{src1} (I)* \texttt{alpha} +  \texttt{src2} (I)* \texttt{beta} +  \texttt{gamma} )\f]
where I is a multi-dimensional index of array elements. In case of multi-channel arrays, each channel is processed independently.
The function can be replaced with a matrix expression:
@code{.cpp}
    dst = src1*alpha + src2*beta + gamma;
@endcode
@note Saturation is not applied when the output array has the depth CV_32S. You may even get result of an incorrect sign in the case of overflow.
@param src1 first input array.
@param alpha weight of the first array elements.
@param src2 second input array of the same size and channel number as src1.
@param beta weight of the second array elements.
@param gamma scalar added to each sum.
@param dst output array that has the same size and number of channels as the input arrays.
@param dtype optional depth of the output array; when both input arrays have the same depth, dtype
can be set to -1, which will be equivalent to src1.depth().
@sa  add, subtract, scaleAdd, Mat::convertTo
*/
CV_EXPORTS_W void addWeighted(InputArray src1, double alpha, InputArray src2,
                              double beta, double gamma, OutputArray dst, int dtype = -1);

###printf()函数

新增知识点,输出宏定义的常量时不加双引号,也不加逗号。

添加之后输出有误。

//67行
printf("\n\n\t\t\t   当前使用的OpenCV版本为:" CV_VERSION);  //输出常量不用加逗号 

sprintf()函数

//90行	
char TrackbarName[50];
sprintf_s(TrackbarName, "透明值 %d", g_nMaxAlphaValue);

VS2017要求用 sprintf_s();

作用:先将变量g_nMaxAlphaValue传给(%d)再将(“透明值 %d”)字符串传给字符数组TrackbarName

效果展示

  1. 透明值70
    70

  2. 透明值100
    100

  3. 透明值20
    20

视频滑动条的创建与使用

#include <opencv2/opencv.hpp>

using namespace cv;

#define WINDOW_NAME "【视频滑动条的创建】"  //为窗口标题定义的宏 

//全局变量声明
const int g_nMaxFrame = 168;//视频帧数的最大值  //通过读入视频的方法获得(count++)
int g_nValueSlider;			//滑动条对应的变量
VideoCapture capture;		//定义一个对象存放视频
Mat frame;					//定义一个对象存放图片
bool flag = false;			//判断视频是否读取成功

//滚动条事件处理函数
void on_Trackbar(int, void*)
{
	
	for (int i = 0; i < g_nValueSlider; i++)
	{
		//读取视频的一帧放入frame中
		capture >> frame;   //类似 cin>>x;    
	}
	imshow(WINDOW_NAME, frame);
}

int main(int argc, char* argv)
{
	flag = capture.open("123.wmv");
	if (!flag)
	{
		printf("\n读取视频错误,请确定目录下是否有open()函数指定视频存在~! \n");
		getchar(); //使用waitKey(); 闪退???
		return -1;
	}
	g_nValueSlider = 25;     		//设置滚动条初值
	namedWindow(WINDOW_NAME); 		//创建一个窗口
	char TrackbarName[50] = "视频的当前帧";	 //设置滚动条名称
	//创建滚动条
	createTrackbar(TrackbarName, WINDOW_NAME, &g_nValueSlider, g_nMaxFrame, on_Trackbar);

	//cvGetCaptureProperty();函数使用不成功

	waitKey();
	return 0;
}

效果展示

问题代码??

参考资料

【浅墨著作】《OpenCV3编程入门》内容简介&勘误&配套源代码下载

https://blog.csdn.net/poem_qianmo/article/details/44416709

猜你喜欢

转载自blog.csdn.net/weixin_41995541/article/details/89844173
今日推荐