Use the slider] [Opencv

Opencv slider in use

The slider is a control Opencv can use the mouse to control the position of the slider, so as to interact with the picture program. Slider content consists of two parts, namely the creation of sliders and a callback function.

The callback function is generally not directly call the main program, but when there are special events will trigger this function, like on the scroll bar when we change the value of time, can trigger a callback function to produce some results.

The slider function definition


int cv::createTrackbar	(	const String & 	trackbarname,
const String & 	winname,
int * 	value,
int 	count,
TrackbarCallback 	onChange = 0,
void * 	userdata = 0 
)	
  • The first argument: trackbarname is a variable of type string, meaning the slider's name appears on the left of the slider
  • The second parameter: winname is a variable of type string, for explaining the slider adsorbed on which window
  • The third parameter: the type is an int pointer records for the current value of the slider, this value will be passed to the callback
  • The fourth parameter: count variable of type int is the maximum value for explaining the slider
  • The fifth parameter: is a function pointer, which used to illustrate the operation when the function is called as the callback
  • Sixth parameter: a pointer of type void *, may represent any type of pointer, its role is to pass data to the callback function. If you use variables are global variables, you can not use this argument, specifically how to pass data, it will be described later.

Callback function definition

The callback function is a function of our custom, but must meet the requirements of format, is a void return type, and the belt and void int two types of variables, such a format is required. int the position of the slider for receiving the third parameter function, void position for receiving sliders of the sixth parameter, the data transfer between the slider and the callback function can be achieved by these two parameters.

void MyFunction(int , void*)
{
}

Examples of the use of the slider

Examples of this combination Mao Xingyun "Opencv3 programming entry" slightly changed a bit, for instructions on how to make the callback function can receive the data slider. This example uses void * inside some of the relevant knowledge, can be found https://blog.csdn.net/Lee_Shuai/article/details/53193436 this article to understand the relevant knowledge.

//这个例子的功能是,我们要将两幅图片进行线性混合,通过滑动条来控制线性混合的比例
//这里要注意的是,用于线性混合的两张图片大小必须是一样的,否则无法成功
#include<iostream>
#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;

class MyPicture
    //定义了一个类,用来作为滑动条的第六个参数进行传递数据,而没有使用全局变量的方式进行
{
public:
    double alpha;//线性混合的alpha值
    double beta;//线性混合的beta值
    Mat m1, m2;//用于存放用于线性混合的两张图片
    const int Max=100;//滑动条的最大值

    MyPicture(double aplha, double beta, Mat m1, Mat m2) :alpha(alpha), beta(beta), m1(m1), m2(m2) {}
    //构造函数,并且使用初始化列表的方法进行变量的初始化

};
void Track(int nowValue, void* p)//回调函数
{
    MyPicture mp = *(MyPicture*)p; //将void*类型的指针变成我们需要的类型,接受参数
    Mat m3;//线性混合的最后的输出结果
    double gamma = 0.0;//线性混合的参数
    mp.alpha = 1.0 * nowValue / mp.Max;
    mp.beta = 1 - mp.alpha;
    addWeighted(mp.m1, mp.alpha, mp.m2, mp.beta, gamma, m3);//线性混合
     //线性混合的公式为 输出图片m3 = m1*alpha + m2*beta + gamma; 
    imshow("window", m3);
}

int main()
{
    int nowValue = 100; //用于记录滑动条的当前数值
    Mat m1 = imread("1.jpg");//用于线性混合的第一张图
    Mat m2 = imread("2.jpg");//用于线性混合的第二张图
    MyPicture mp(1.0, 0.0, m1, m2);//初始化函数
    Track(nowValue, &mp);//初始化图片显示
    string TrackName = "透明值100";
    namedWindow("window", WINDOW_NORMAL);
    createTrackbar(TrackName, "window", &nowValue, mp.Max, Track, &mp);//创建滑动条
    waitKey(0);



    return 0;
}

Of course, you can also use a global variable method according to the book method, no need to construct a new pointer to pass parameters.

Examples of the use of global variables method to achieve the slider is also written here, you can view together

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

//全局变量,滑动条
const int Max = 100;
int g_nowValue;
double alpha;
double beta;

//全局变量,图片
Mat m1, m2, m3;

void Track(int, void*)
{
	alpha = (double)g_nowValue / Max;
	beta = 1 - alpha;
	addWeighted(m1, alpha, m2, beta, 0.0, m3);
	imshow("1", m3);
}
int main()
{
	m1 = imread("1.jpg");
	m2 = imread("2.jpg");
	g_nowValue = 70;
	Track(g_nowValue,0);

	namedWindow("1");
	string TrackName = "透明度100";
		
	createTrackbar(TrackName, "1", &g_nowValue, Max, Track);
	waitKey(0);


	return 0;
}

Published 14 original articles · won praise 1 · views 506

Guess you like

Origin blog.csdn.net/qq_41741344/article/details/104319793