Computer Vision (two) -opencv of createTrackbar () Detailed

Summary:

I learn openCV3 see is "learning openCV3" This book is very thick one, because they do not know is not a lot to see, personally feel that some important function inside the talking is not very detailed, such as createTrackbar () This function, which appears in the book of the third sample program, the book only shows that this is a program to create a scroll bar, but on the inside as well as explain the parameters corresponding with him to explain the callback function is not perfect, So I opened it and the definition of the internet to find some bloggers articles to learn, but the feeling talking about are not very full, here I combine their experiments with their own understanding, explain my views on this function .

Function Description:

createTrackbar () function is the function prototype:

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

trackbarname: This parameter is used to scroll bar a name;

winname: This parameter is used to specify that you want it to use the scroll bar on the window;

value: This parameter is used to set the initial value of the position of the slider, while the slide position after recording;

count: This parameter is used to specify the maximum value of the scroll bar can be scrolled;

onChange: This parameter can be understood as a function of a variable type (of course, say a little strange feeling), for receiving the callback function of the function name, the default value is 0;

userdata: This variable parameter is the user data passed to the callback function for processing events track bar, the default value is 0.

There is a total of six parameters, wherein the parameter value deviates readily appreciated, onchange, userdata maybe parameters may be difficult to understand;

Let me talk about my views on the following value of this parameter:

This parameter value must first know that it is used to give an initial value of the position of the slider, the slider is, tell us first position in which, instead of the slider to the minimum value, the range slider can slide sliding range of always [0, count], count is the fourth parameter, and also a concept appreciated that, during the operation of the code, not affect the value of the value of the position of the slider, but because the user changes the value of the value of the movement of the slider , that value is passive.

Then said onChange understanding of the parameters:

First we look at the function prototype for the callback:

void (*TrackbarCallback)(int pos, void* userdata);

When we did not see a callback function prototype when it may be difficult to understand why those two parameters must be some code defined callback function when the callback function prototype after reading I believe C ++ based people can understand, and then I come talk about the callback function and createTrackbar (relations) function; first of all, we see that the callback function and createTrackbar function has a parameter called userdata, that this is not a coincidence? The answer, of course not, you can understand, the callback function is a function createTrackbar must rely on the use of function (), he can not be used alone, and that his two parameters is how to use it? First POS first parameter, which indicates the current position of the slider is located, its value is createTrackbar () presented to him, that is createTrackbar () value of the parameter values, the transmission process is createTrackbar () internal implementation without having to get to the bottom, then the value of the parameter userdata callback function is through createTrackbar () the parameter userdata directly, so createTrackbar () the parameter userdata is actually dedicated to the callback function to prepare.

Issues on use of functions:

createTrackbar () will be more likely in the use of magic, for example, you might see the following procedures:

#include <the iostream> 
 #include <opencv2 / Core / core.hpp> 
 #include <opencv2 / HighGUI / highgui.hpp> 
 #include <imgproc.hpp> the using namespace STD;
  the using namespace CV; // the TrackBar changes of callback void onChangeTrackBar ( int POS, void * UserData); // main function int main () 
 { // value of trackbar int posTrackBar = 0 ;
      // trackbar maximum int maxValue = 255 ; // read image to grayscale FIG form read

  






     
    
    

    
     Img = imread MAT ( " F: \\ timg.jpg picture \\ " , 0 ); 

    // New Window 
    namedWindow ( " binarization " ); 
     imshow ( " binarization " , img); 

    // create trackbar, we img as the data transfer into the callback 
    createTrackbar ( " POS " , " binarization " , & posTrackBar, maxValue, onChangeTrackBar, & img); 

    waitKey (); 

    return  0 ; 
 } 

// callback 
void onChangeTrackBar ( int POS, void *usrdata) 
 { 
     // cast 
    Mat the src = * (Mat * ) (usrdata); 
     Mat DST; 

    // binarization 
    threshold (the src, DST, POS, 255 , 0 ); 
     imshow ( " binarization " , DST ); 
 }

 The above is a very simple code to binary image processing, and when you run it you'll find that magic, there is not a loop, but you can always slide the scroll bar slider, and the image will appear corresponding changes as the slider of the effect of different positions:

In fact, here is the deal obtained by createTrackbar event, I learned a little computer people can understand that the function has actually been running in the background to detect whether there is such an interrupt is generated to move the slider, and then when you move the slider will trigger an interrupt, then the callback function is equivalent to the interrupt service routine to handle interrupts (not entirely accurate, but can be understood, just as Newton's classical laws, is not entirely accurate description of the world, but at the macro level, it is used to understand the world good law).

 

Conclusion:

 

All of the above is my personal experimentally validated, and if there is something wrong, we welcome the comments section pointed out. Finally, attach some tests with the code:

Continuous video playback, schedule adjustable:

#include <opencv.hpp>
#include<iostream>

using namespace std;

int g_slider_position = 0;
int g_run = 1, g_dontset = 0;
cv::VideoCapture g_cap;

void onTrackbarSlide(int pos, void *) {
    g_cap.set(cv::CAP_PROP_POS_FRAMES, pos);
    if (!g_dontset) {
        g_run = 1;
    }
    
    g_dontset = 0;
}

int main() {
    cv::namedWindow("show_video", 0);
    g_cap.open("F:\\图片\\123.mp4");
    int frames = (int)g_cap.get(cv::CAP_PROP_FRAME_COUNT);
    int tmpw = (int)g_cap.get(cv::CAP_PROP_FRAME_WIDTH);
    int tmph = (int)g_cap.get(cv::CAP_PROP_FRAME_HEIGHT);
    cout << "Video has" << frames << "frames of dimensions(" << tmpw << "," << tmph << ")." << endl;
    cv::createTrackbar("Position", "show_video", &g_slider_position, frames, onTrackbarSlide);
    cv::Mat frame;
    while (1) {
        if (g_run != 0) {
            g_cap >> frame;
            if (frame.empty()) {
                break;
            }
            int current_pos = (int)g_cap.get(cv::CAP_PROP_POS_FRAMES);
            g_dontset = 1;

            cv::setTrackbarPos("Position", "show_video", current_pos);
            cv::imshow("show_video", frame);

            g_run = -1;
        }
        char c = (char)cv::waitKey(33);
        if (c == 's') {
            g_run = 1;
            cout << "Single step,run = " << g_run << endl;
        }
        if (c == 'r') {
            g_run = -1;
            cout << "Run mode,run" << g_run << endl;
        }
        if (c == 27) {
            cv::destroyWindow("show_video");
            break;
        }
    }
    cv::destroyWindow("show_video");
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/geek-hao/p/11668248.html