首页 > 代码库 > OpenCV Tutorials —— Adding a Trackbar to our applications

OpenCV Tutorials —— Adding a Trackbar to our applications

int createTrackbar(const string& trackbarname, const string& winname, int* value, int count, TrackbarCallbackonChange=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. 向回调函数传递的参数

Code

#include "stdafx.h"#include <cv.h>#include <highgui.h>using namespace cv;/// Global Variablesconst int alpha_slider_max = 100;int alpha_slider;double alpha;double beta;/// Matrices to store imagesMat src1;Mat src2;Mat dst;/** * @function on_trackbar * @brief Callback for trackbar */void on_trackbar( int, void* ){	alpha = (double) alpha_slider/alpha_slider_max ;	beta = ( 1.0 - alpha );	addWeighted( src1, alpha, src2, beta, 0.0, dst);	imshow( "Linear Blend", dst );}int main( int argc, char** argv ){	/// Read image ( same size, same type )	src1 = imread("img1.jpg");	src2 = imread("you.jpg");	int min_c;	int min_r;	min_c = src1.cols>src2.cols?src2.cols:src1.cols;	min_r = src1.rows>src2.rows?src2.rows:src1.rows;	src1=src1(Range(0,min_r),Range(0,min_c));	src2=src2(Range(0,min_r),Range(0,min_c));	if( !src1.data ) { printf("Error loading src1 \n"); return -1; }	if( !src2.data ) { printf("Error loading src2 \n"); return -1; }	/// Initialize values	alpha_slider = 0;	/// Create Windows	namedWindow("Linear Blend", 1);	/// Create Trackbars	char TrackbarName[50];	sprintf( TrackbarName, "Alpha x %d", alpha_slider_max );	createTrackbar( TrackbarName, "Linear Blend", &alpha_slider, alpha_slider_max, on_trackbar );	/// Show some stuff	on_trackbar( alpha_slider, 0 );	/// Wait until user press some key	waitKey(0); return 0;}

OpenCV Tutorials —— Adding a Trackbar to our applications