how to use cv::setMouseCallback

后端 未结 2 861
情歌与酒
情歌与酒 2021-01-19 16:36

I\'m trying to use cv::setMouseCallback in my c++ project. I just don\'t get it. let that I habe a class Stuff how can tell this class you got a frame and run the cv::se

相关标签:
2条回答
  • 2021-01-19 17:22

    First of all you need to create a named window in the main function. namedWindow( "image", 0 ); or something similar will do the job.

    The mouse callback function is not associated to the frame variable but it is associated to the window. In your case it would be:

    char* name = "image";
    cv::namedWindow( name, 0 );
    cv::setMousCallback(name, obj.mouse,&frame);
    

    The callbacks are functions that call other functions when an event happens on a window. For the mouse, an event can be the mouse movement, the left, right or middle clicks. Here you can find a list of them, as well as good explanations.

    So when this "event" takes place in the window, opencv calls the function whose name was specified in the setMouseCallback as an argument, in your case Stuff::mouse. if you define the function like this:

    Stuff::mouse( int event, int x, int y, int flags, void* params )
    

    when it is called the event variable will be filled with the value of the trigger, the x and y with the positions off the mouse on the image etc. If you want to pass the frame in the mouse function you use it as in this question, if you consider the correction of patxiska's answer.

    So with a switch you can find out what kind of event it was:

    switch( event ){
        case CV_EVENT_LBUTTONDOWN:
            //...
            break;
    
        case CV_EVENT_RBUTTONDOWN:
            //...
            break;
    
        case CV_EVENT_FLAG_CTRLKEY:
            //...
            break;
    }
    

    and take your frame typecasting it from void* back to a cv::Mat.

    Here you can find another example of Opencv's site on how to use a mouse callback.

    Hope I helped, I haven't used opencv for a while and I don't have my sample source files now. Callbacks are simplified in the Opencv GUI but that's the logic of working with any GUI. Input such as mouse and keyboard trigger events and the callback functions pass the events to the functions of your implementation.

    0 讨论(0)
  • 2021-01-19 17:26

    you must declare a mouse handler as static inside your class. For instance, I have a dragger with a member mouser, that I want to be called. I declare an helper static void mouser, that cast the void* received and calls the member:

    class dragger {
    
    void mouser(int event, int x, int y) {
      current_img = original_img.clone();
      Point P(x, y);
      ...
    }
    static void mouser(int event, int x, int y, int, void* this_) {
      static_cast<dragger*>(this_)->mouser(event, x, y);
    }
    

    and instance in dragger constructor in this way

    dragger(string w, Mat m) :
        window_id(w), status(0), original_img(m), /*black(0, 0, 0),*/ K(5, 5)
    {
       ...
       setMouseCallback(w, mouser, this);
    }
    
    ...
    }
    
    0 讨论(0)
提交回复
热议问题