OpenCV basic tutorial (11) -- HighGUI graphical user interface

Keywords: C++ OpenCV image processing

11. HighGUI graphical user interface

11.1 creation and use of slider

11.1,1 create slider: createTrackbar() function

The createTrackbar function is used to create a slider with adjustable values:

int cv::createTrackbar(const cv::String &trackbarname, const cv::String &winname, int *value, int count, cv::TrackbarCallback onChange = (cv::TrackbarCallback)0, void *userdata = (void *)0)

trackbarname: the name of the track bar, which represents the track bar we created.

winname: the name of the window, indicating the window to which the track bar will be attached, that is, a window name filled in when the corresponding namedWindow creates the window.

Value: a pointer to an integer indicating the position of the slider. When created, the initial position of the slider is the current value of the variable.

count: the value of the maximum position that the slider can reach. The value of the slider minimum position is always 0.

onChange: This is a pointer to the callback function with a default value of 0. This function calls back every time the slider position changes. And the prototype of this function must be void XXXX(int, void);, The first parameter is the position of the track bar, and the second parameter is user data (see the sixth parameter below). If the callback is a NULL pointer, it means that there is no call to the callback function, and only the third parameter value changes.

userdata: this parameter is the data passed by the user to the callback function to process track bar events. It has a default value of 0. If the value argument of the third parameter used is a global variable, you can completely ignore the userdata parameter.

Use the following method (mix two input pictures):

//Macro defined for window title
#define WINDOW_NAME "[linear blending example]"

//Maximum Alpha value
const int Max_AlphaValue = 100;
//Variables corresponding to the slider
int Slider_AlphaValue;
//Define values for Alpha and Beta
double AplhaValue;
double BetaValue;
//Declare the variable that stores the image
Mat srcImage1;
Mat srcImage2;
Mat dstImage;

//Callback function in response to slider
void on_Trackbar(int, void*)
    //Finds the ratio of the current Alpha value to the maximum value
    AplhaValue = (double) Slider_AlphaValue / Max_AlphaValue;
    //Beta = 1 - Alpha
    BetaValue = (1.0 - AplhaValue);
    //Linear blending based on Alpha and Beta
    addWeighted(srcImage1, AplhaValue, srcImage2, BetaValue, 0.0, dstImage);
    //Display renderings
    imshow(WINDOW_NAME, dstImage);

int main()  
    //Load image (the size of the two images should be the same)
    srcImage1 = imread("../1.jpg");
    srcImage2 = imread("../2.jpg");
    //Set the initial value of the slider to 70
    Slider_AlphaValue = 70;
    //Create form
    namedWindow(WINDOW_NAME, 1);
    //Create a slider control in the created form
    char TrackbarName[50];
    sprintf(TrackbarName, "Transparent value %d", Max_AlphaValue);
    createTrackbar(TrackbarName, WINDOW_NAME, &Slider_AlphaValue, Max_AlphaValue, on_Trackbar);
    //The result is displayed in the callback function
    on_Trackbar(Slider_AlphaValue, 0);

11.1.2 get the position of the current track bar: getTrackbarPos() function

The getTrackbarPos function is used to obtain the position of the current track bar and return:

int cv::getTrackbarPos(const cv::String &trackbarname, const cv::String &winname)

trackbarname: indicates the name of the track bar

winname: indicates the name of the parent window of the track bar

11.2 mouse operation

The mouse operation in OpenCV is very similar to the message mapping of the slider, which is realized through a mediation function and a callback function. The function that creates and specifies the slider callback function is createTrackbar, and the function that specifies the mouse operation message callback function is setMouseCallback.

void cv::setMouseCallback(const cv::String &winname, cv::MouseCallback onMouse, void *userdata = (void *)0)

winname: the name of the window

onMouse: Specifies the function pointer to be called every time the mouse time occurs in the window.

userdata: a user-defined parameter passed to the callback function, with a default value of 0.

Use the following method (draw a rectangle with the mouse):

//Macro defined for window title
#define WINDOW_NAME "[program window]"

//Declaration of global functions
void on_MouseHandle(int event, int x, int y, int flags, void* param);
void DrawRectangle(Mat& img, Rect box);

//Declaration of global variables
Rect rect = Rect(-1, -1, 0, 0);
bool flag_DrawingBox = false;   //Do you want to draw
RNG rand_num(12345);   //Generate random number

int main()  
    //Prepare parameters
    Mat srcImage(600, 800, CV_8UC3);
    Mat tempImage;
    srcImage = Scalar::all(0);
    //Set mouse operation callback function
    setMouseCallback(WINDOW_NAME, on_MouseHandle, (void*)&srcImage);
    //The main loop of the program draws when the identifier for drawing is true
        //Copy source diagram to temporary variable
        //When the identifier for drawing is true, drawing is performed
            DrawRectangle(tempImage, rect);
        imshow(WINDOW_NAME, tempImage);
        //Press ESC to exit the program
    return 0;

//The mouse callback function performs different operations according to different mouse events
void on_MouseHandle(int event, int x, int y, int flags, void* param)
    Mat& image = *(Mat*) param;
        //Mouse movement message
        case EVENT_MOUSEMOVE:
                //If the identifier of whether to draw is true, record the length and width into the RECT type variable
                    rect.width = x - rect.x;
                    rect.height = y - rect.y;
        //Left press message
                flag_DrawingBox = true;
                rect = Rect(x, y, 0, 0); //Record start point
        //Left click to lift the message
        case EVENT_LBUTTONUP:
                flag_DrawingBox = false;  //Set identifier to false
                //Processing of width less than 0
                if(rect.width < 0)
                    rect.x += rect.width;
                    rect.width *= -1;
                //Processing of height less than 0
                if(rect.height < 0)
                    rect.y += rect.height;
                    rect.height *= -1;
                //Call function to draw
                DrawRectangle(image, rect);

//Custom rectangle drawing function
void DrawRectangle(Mat& img, Rect box)
    //Draw a rectangular box with random colors
    rectangle(img,,, Scalar(rand_num.uniform(0,255), rand_num.uniform(0,255), rand_num.uniform(0,255)));

Posted by ckjian on Wed, 10 Nov 2021 14:03:12 -0800