OpenCV 2.3与VS 2008 - 鼠标事件 [英] OpenCV 2.3 with VS 2008 - Mouse Events

查看:144
本文介绍了OpenCV 2.3与VS 2008 - 鼠标事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

义务 - 我是新手。有一个工作,涉及编程,我在教自己,我去。不用说,作为一个老师,我经常和彻底地犯错误。

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.

我现在在哪里:我创建了类Graph,它足够)使图形。但现在我想让它,所以在鼠标点击我修改图。但我似乎不能得到一个鼠标处理程序成为该类的成员函数。

Where I'm at right now: I've created the class "Graph", it (surprisingly enough) makes graphs. But now I want to make it so that on a mouse click I modify the graph. But I can't seem to get a mouse handler to be a member function of the class.

cv::setMouseCallback(windowName, onMouse, 0); // Set mouse handler to be onMouse

不能使用

cv::setMouseCallback(windowName, Graph::onMouse, 0);

它让我缺少参数错误。根据我不能让它成员函数。下面的答案给出,它编译,但我的这个指针是nulled。 ug。

It gives me lack of parameter errors. According to this I can't make it a member function. After following the answer given, it compiles but my this pointer is nulled. Ugh.

OnMouse看起来像这样:

OnMouse looks like this:

void onMouse(int event, int x, int y,int, void*)
{
    if (event == CV_EVENT_LBUTTONDOWN)
    {
        cvMoveWindow("Window", 500, 500); //Just to see if stuff happened
    }
    return;
}



我不在乎移动窗口,我想修改图本身 - 它作为cv :: Mat变量存储在Graph对象中。

I don't care about moving the window, I want to modify the graph itself - which is stored as a cv::Mat variable in a Graph object. And I can't figure out how to do it.

任何帮助将不胜感激,我真的希望这不只是乱码。

Any help would be appreciated, and I really hope this wasn't just gibberish.

推荐答案

是C ++中的回调函数是一种喜悦,不是吗?你实际上必须给OpenCV一个函数(不是类方法),因为你已经发现。但是,你可以使用以下技术来破解这种可怕性:

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;
}

setMouseCallback的最后一个参数对于克服一些常见的问题非常有用遇到这样的人。

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

这篇关于OpenCV 2.3与VS 2008 - 鼠标事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆