openCV HighGUI graphical user interface - to create and use the slider

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_39504764/article/details/100531654

OpenCV provides a tool in a very easy to use - slider (Trackbar), it is attached to the window exists.

createTrackbar () function

The effect of this function: to create for us a track bar with a specific name and scope, specify a location and trajectory synchronization of variables, but also to specify a callback function onChange, when the track bar to change the location to invoke the callback function.

int createTrackbar(const string& trackbarname, const string& winname,
int* value, int count, TrackbarCallback onChange = 0, void* userdata = 0);

Parameter Description

  • Parameter 1: The name of the slider.
  • Parameter 2: The name of the window, the slider that is created on which to be attached to the window.
  • Parameter 3: indicates the position of the slider. When you create the initial position of the slider is the current value of the variable.
  • Parameter 4: represents the maximum slide position of the slider. Minimum slider is always 0.
  • Parameter 5: TrackbarCallback type onChange, it has a default value of 0. This is a pointer to a callback function, every time a change in position of the slider, the callback function will be. And this must be the function prototype void XXXXX (int, void *);
  • Parameter 6: void * type userdata, it also has a default value of 0. This parameter is user data passed to the callback function to handle calls the slider. If you are using a third parameter value argument is a global variable, you can not to manage this userdata parameter.

Callback: function is a function pointer called through. If we take the pointer (address) function is passed as a parameter to another function, when the pointer is used to call the function it points to, it is called a callback function. Help achieve the callback function is called directly side, but by another calling party when a particular event or condition occurs, for response to the event or condition.

#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
#define WINDOW_NAME "example"        //为窗口标题定义的宏

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 );
}


int main( int argc, char** argv )
{
    //加载图像 (两图像的尺寸需相同)
    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( TrackbarName, "value %d", g_nMaxAlphaValue ); //把g_nMax以定义好的格式写到TrackbarName数组中
    
    createTrackbar( TrackbarName, WINDOW_NAME, &g_nAlphaValueSlider, g_nMaxAlphaValue, on_Trackbar );
    //结果在回调函数中显示
    on_Trackbar( g_nAlphaValueSlider,0);
    
    //按任意键退出
    waitKey(0);
    
    return 0;
}

Here Insert Picture Description

Get current track position of the bar: getTrackbarPos () function

printf("获取当前轨迹条的位置:%d\n",getTrackbarPos(TrackbarName, WINDOW_NAME) );

Guess you like

Origin blog.csdn.net/qq_39504764/article/details/100531654