OpenCV中的cvSetMouseCallback 2.1托管C ++(CLI / C ++)

编程入门 行业动态 更新时间:2024-10-28 21:29:49
本文介绍了OpenCV中的cvSetMouseCallback 2.1托管C ++(CLI / C ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的类名是HandMotionRecognition,我在鼠标回调中调用getColorPixel方法。这是使用Visual Studio 2010的OpenCV,项目类型是c ++ - > cli。

My class name is HandMotionRecognition and I'm calling getColorPixel method in mouse callback. This is in OpenCV using Visual Studio 2010 and project type is c++ -> cli.

标准代码(除非我被误认为)处理鼠标事件是

The standard code (unless I'm mistaken) to handle mouse events is

cvSetMouseCallback( "CameraIn", getColorPixel, (void*) frameHSV);

但是当我编译它给出编译时错误

But when I compile it gives a compile time error

错误C3867:'HandMotionRecognition :: getColorPixel':函数调用缺少参数列表;使用'& HandMotionRecognition :: getColorPixel'创建一个指向成员的指针

error C3867: 'HandMotionRecognition::getColorPixel': function call missing argument list; use '&HandMotionRecognition::getColorPixel' to create a pointer to member

然后我按照我的说法, ...

Then I do as I told and put the code like this...

cvSetMouseCallback( "CameraIn", &HandMotionRecognition::getColorPixel, (void*) frameHSV);

但是我再次收到编译错误..

But again I get a compile error..

错误C3374:除了创建委托实例,不能使用HandMotionRecognition :: getColorPixel的地址

error C3374: can't take address of 'HandMotionRecognition::getColorPixel' unless creating delegate instance

所以我创建一个这样的代理... [创建委托...我不知道这是100%正确]

So i create a delegate like this...[creating delegate..I dont know this is 100% correct]

  • 我把委托在HandMotionRecognition.h中的MouseCallbackDelegate(int event,int x,int y,int flags,void * param);

    我把这段代码放在HandMotionRecognition.cpp中,而不是 cvSetMouseCallback(CameraIn,& HandMotionRecognition :: getColorPixel,(void *)frameHSV);

    I put this code in HandMotionRecognition.cpp instead of cvSetMouseCallback( "CameraIn", &HandMotionRecognition::getColorPixel, (void*) frameHSV);

    MouseCallbackDelegate ^StaticDelInst = gcnew MouseCallbackDelegate(this, &HandMotionRecognition::getColorPixel);

    cvSetMouseCallback(CameraIn,StaticDelInst,(void *)frameHSV);

    cvSetMouseCallback( "CameraIn", StaticDelInst, (void*) frameHSV);

    但是它会给出编译错误:(这是我唯一的错误)

    But it then gives the compile error: (this is the only error I get)

    错误C2664:'cvSetMouseCallback':无法将参数2从HandMotionRecognition :: MouseCallbackDelegate ^转换为CvMouseCallback

    error C2664: 'cvSetMouseCallback' : cannot convert parameter 2 from 'HandMotionRecognition::MouseCallbackDelegate ^' to 'CvMouseCallback'

    (至于我可以看到...这是由于使用cli而不是win32)

    (As for as I can see..this is due to using cli instead of win32)

    有没有一个工作,或我是在这里做错事?

    Is there a work-around this or am i doing something wrong here???

    请帮助...

    推荐答案

    p>回调方法必须是您发现的静态(或非成员函数)。在这种情况下,标准成语是将类实例指针传递给 void * param 参数,并使用 static 函数调用成员函数。由于您正在使用 param 来存储 frameHSV ,您需要以其他方式传输(例如,将其存储在你的课堂实例)。

    The callback method has to be static (or a non-member function) as you've discovered. The standard idiom in this case is to pass the class instance pointer in the void* param parameter and use a static function to call the member function. Since you're currently using param to store frameHSV you need to transfer that some other way (e.g. by storing it in your class instance).

    示例:

    class HandMotionRecognition { /* your code */ private: void getPixelColor(int event, int x, int y, int flags, void* param) { } public: static void mouseCallback(int event, int x, int y, int flags, void* param) { static_cast<HandMotionRecognition*>(param)->getPixelColor(event, x, y, flags); } }

    并注册:

    HandMotionRecognition* hmr = /* ... */ hmr->setFrameHSV(frameHSV); cvSetMouseCallback("CameraIn", &HandMotionRecognition::mouseCallback, hmr);
  • 更多推荐

    OpenCV中的cvSetMouseCallback 2.1托管C ++(CLI / C ++)

    本文发布于:2023-11-15 11:59:03,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1594236.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:cvSetMouseCallback   OpenCV   CLI

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!