標籤:style io ar os sp for strong on div
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 valuesalpha_slider = 0;/// Create WindowsnamedWindow("Linear Blend", 1);/// Create Trackbarschar TrackbarName[50];sprintf( TrackbarName, "Alpha x %d", alpha_slider_max );createTrackbar( TrackbarName, "Linear Blend", &alpha_slider, alpha_slider_max, on_trackbar );/// Show some stuffon_trackbar( alpha_slider, 0 );/// Wait until user press some keywaitKey(0); return 0;}
OpenCV Tutorials —— Adding a Trackbar to our applications