OpenCV Basic Tutorial (11) - HighGUI GUI

11. HighGUI GUI

Creation and use of 11.1 slider bar

11.1,1 Create slider: createTrackbar() function

The createTrackbar function creates a slider bar that can adjust 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 used to represent the track bar we created.

winname: The name of the window to which this track bar will be attached, that is, a window name that is filled in when the 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 minimum position of the slider is always zero.

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 location of the track bar, and the second parameter is the 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, only the third parameter value changes.

userdata: This parameter is the data passed to the callback function by the user to handle track bar events, with a default value of 0. If the third argument used, the value argument, is a global variable, you can leave the userdata parameter alone.

Use as follows (mix two input pictures):

//Macro defined for window title
#define WINDOW_NAME "[Linear Mixing Example]

//Maximum Alpha Value
const int Max_AlphaValue = 100;
//Variables corresponding to slider bar
int Slider_AlphaValue;
//Define values for Alpha and Beta
double AplhaValue;
double BetaValue;
//Declare variables to store images
Mat srcImage1;
Mat srcImage2;
Mat dstImage;

//Callback function for response slider
void on_Trackbar(int, void*)
{
    //Find 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 mixing based on Alpha and Beta
    addWeighted(srcImage1, AplhaValue, srcImage2, BetaValue, 0.0, dstImage);
    //Display Effect Graph
    imshow(WINDOW_NAME, dstImage);
}

int main()  
{  
    //Load images (both images need to be the same size)
    srcImage1 = imread("../1.jpg");
    srcImage2 = imread("../2.jpg");
    //Set initial slider value to 70
    Slider_AlphaValue = 70;
    //Create Form
    namedWindow(WINDOW_NAME, 1);
    //Create a slider control in the created form
    char TrackbarName[50];
    sprintf(TrackbarName, "Transparency 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);
    waitKey(0);
    destroyAllWindows();
} 

11.1.2 Get the location of the current track bar: getTrackbarPos() function

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

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

trackbarname: the name of the track bar

winname: The name of the parent window representing the track bar

11.2 Mouse Action

The mouse action in OpenCV is similar to the message mapping of slider bars, which is achieved through a mediation function combined with a callback function. The function that creates and specifies the slider callback function is createTrackbar, and the function that specifies the mouse action 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 each time the mouse time occurs in the window.

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

Use the following (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);

//Declarations of global variables
Rect rect = Rect(-1, -1, 0, 0);
bool flag_DrawingBox = false;   //Whether to draw
RNG rand_num(12345);   //Generate Random Numbers


int main()  
{
    //Prepare parameters
    Mat srcImage(600, 800, CV_8UC3);
    Mat tempImage;
    srcImage.copyTo(tempImage);
    srcImage = Scalar::all(0);
    //Set Mouse Action Callback Function
    namedWindow(WINDOW_NAME);
    setMouseCallback(WINDOW_NAME, on_MouseHandle, (void*)&srcImage);
    //The main loop of the program that draws when the identifier being drawn is true
    while(1)
    {
        //Copy Source Diagram to Temporary Variable
        srcImage.copyTo(tempImage); 
        //Draw when the identifier being drawn is true
        if(flag_DrawingBox)
        {
            DrawRectangle(tempImage, rect);
        }
        imshow(WINDOW_NAME, tempImage);
        //Press ESC key, program exit
        if(waitKey(10)==27)
        {
            break;
        }
    }
    return 0;
} 

//Mouse callback function to perform different actions according to different mouse events
void on_MouseHandle(int event, int x, int y, int flags, void* param)
{
    Mat& image = *(Mat*) param;
    switch(event)
    {
        //Mouse Move Message
        case EVENT_MOUSEMOVE:
            {
                //If the identifier being drawn is true, record the length and width to the RECT variable
                if(flag_DrawingBox)
                {
                    rect.width = x - rect.x;
                    rect.height = y - rect.y;
                }
            }
            break;
        //Left Press Message
        case EVENT_LBUTTONDOWN:
            {
                flag_DrawingBox = true;
                rect = Rect(x, y, 0, 0); //Recording Start Point
            }
            break;
        //Left Button Raise Message
        case EVENT_LBUTTONUP:
            {
                flag_DrawingBox = false;  //Set identifier to false
                //Handling of widths less than 0
                if(rect.width < 0)
                {
                    rect.x += rect.width;
                    rect.width *= -1;
                }
                //Handling of less than 0
                if(rect.height < 0)
                {
                    rect.y += rect.height;
                    rect.height *= -1;
                }
                //Call function to draw
                DrawRectangle(image, rect);
            }
            break;
    }
}

//Custom Rectangle Drawing Function
void DrawRectangle(Mat& img, Rect box)
{
    //Draw rectangle with random colors
    rectangle(img, box.tl(), box.br(), Scalar(rand_num.uniform(0,255), rand_num.uniform(0,255), rand_num.uniform(0,255)));
}

Keywords: C++ OpenCV image processing

Added by feckless on Tue, 09 Nov 2021 19:07:13 +0200