OpenCV 2.3 with VS 2008 - Mouse Events

前端 未结 2 2011
陌清茗
陌清茗 2021-02-15 14:31

Obligatory - I\'m a newbie. Have a job that involves programming and I\'m teaching myself as I go. Needless to say as a teacher I get things wrong frequently and thoroughly.

2条回答
  •  不要未来只要你来
    2021-02-15 14:56

    Yes callback functions in C++ are a joy, aren't they? You actually have to give OpenCV a function (not a class method) as you've already found out. However, you can hack around this awfulness using the following technique:

    class MyClass
    {
    public:
         void realOnMouse(int event, int x, int y, int flags)
         {
             // Do your real processing here, "this" works fine.
         }
    };
    
    // This is a function, not a class method
    void wrappedOnMouse(int event, int x, int y, int flags, void* ptr)
    {
        MyClass* mcPtr = (MyClass*)ptr;
        if(mcPtr != NULL)
            mcPtr->realOnMouse(event, x, y, flags);
    }
    
    int main(int argv, char** argc)
    {
        // OpenCV setup stuff...
    
        MyClass processor;
        cv::setMouseCallback(windowName, wrappedOnMouse, (void*)&processor);
    
        // Main program logic
    
        return 0;
    }
    

    That last parameter on setMouseCallback is quite useful for overcoming some of the problems you usually encounter like this.

提交回复
热议问题