【问题标题】:opencv mouse handling on windows form application vc++windows窗体应用程序vc++上的opencv鼠标处理
【发布时间】:2012-04-11 21:04:08
【问题描述】:

我无法在 vc++ windows 窗体应用程序中执行 opencv 鼠标处理。我收到以下错误

错误 29 错误 C3867: 'touch_gui_trial1::Form1::mouseHandler': 函数调用缺少参数列表;使用 '&touch_gui_trial1::Form1::mouseHandler' 创建指向成员 c:\users\mridul\documents\visual studio 2010\projects\touch_gui_trial1\Form1.h 104 1 touch_gui_trial1

的指针

代码sn-p如下

            public ref class Form1 : public System::Windows::Forms::Form
        {
    public:

    int i,cntr2,count,camno,cntr,ch,prev,flag_camno,hand_thresh_area;
    static int handthresharea=0,flagroi=0,drag; 
    static Point sz,point;

            void mouseHandler(int event, int x, int y, int flags, void* param)
    {
        IplImage* img0;

        img0=(IplImage *)param;
            /* user press left button */
        if (event == CV_EVENT_LBUTTONDOWN && !drag)
        {
            point = Point(x, y);
            drag  = 1;
        }

        /* user drag the mouse */
        if (event == CV_EVENT_MOUSEMOVE && drag)
        {
            img1 = cvCloneImage (img0);

            cvRectangle(
                img1,
                point,
                cvPoint(x, y),
                CV_RGB(255, 0, 0),
                1, 8, 0
            );

                        cvShowImage("Image taken", img1);
        }

        /* user release left button */
        if (event == CV_EVENT_LBUTTONUP && drag)
        {
            img1 = cvCloneImage(img0);

            cvSetImageROI(img1,cvRect(point.x,point.y,x - point.x,y - point.y));
            sz.x=x - point.x;
            sz.y=y - point.y;
            //cvNot(img1, img1);    // or do whatever with the ROI

            //cvResetImageROI(img1);
            cvNamedWindow("the roi",1); cvShowImage("the roi", img1);
            flagroi=1;
            drag = 0;
        }

        /* user click right button: reset all */
        if (event == CV_EVENT_RBUTTONUP)
        {
            //cvShowImage("Image taken", img0);
            drag = 0;
        }
    }
    int select_roi()
    {
        CvCapture *frame;
        IplImage* img0;
        IplImage* img1;

        frame=cvCaptureFromCAM(0);
        cvNamedWindow( "ROI Selection", CV_WINDOW_AUTOSIZE );
        if ( !frame ) 
        {
            fprintf( stderr, "ERROR: capture is NULL \n" );
            getchar();
            return -1;
        }
        img0= cvQueryFrame(frame);
        cvShowImage("ROI Selection", img0);
        cvSetMouseCallback("ROI Selection", ::mouseHandler, img0);
                    **//error is in the above line**

        return 0;
    }

// rest 是调用 select roi 的 GUI 部分。

【问题讨论】:

    标签: visual-c++ opencv opencvdotnet


    【解决方案1】:

    您的代码有 2 个问题:

    • 您似乎有 2 个签名完全相同的 mouseHandler() 方法;

    • 当使用方法作为回调时,你需要确保它们是static方法:

      static void mouseHandler(int event, int x, int y, int flags, void* param) { ... }
      

    当您指定回调到cvSetMouseCallback() 时,您应该这样做:

    cvSetMouseCallback("ROI Selection", &Form1::mouseHandler, img0);
    

    【讨论】:

    • 当我尝试将其设为静态时,出现如下错误:-----------Error 28 error C2664: 'cvSetMouseCallback' : cannot convert parameter 2 from 'void (__clrcall touch_gui_trial1 ::Form1::* )(int,int,int,int,void *)' 到 'CvMouseCallback' c:\users\mridul\documents\visual studio 2010\projects\touch_gui_trial1\Form1.h 104 1 touch_gui_trial1
    • 当您不共享应用程序的完整源代码时会发生这种情况。 Form1 似乎是另一个类中的对象,这是您未在代码中显示的内容。这里有几个选项,其中之一是将回调方法从Form1 移动到touch_gui_trial1,然后将其作为&touch_gui_trial1::mousehandler 传递给cvSetMouseBacllback()。从现在开始,我只能在您共享一个能够重现您所面临问题的最小应用程序时为您提供帮助。我要退役我的水晶球了。
    • link 这是我项目的链接
    • 同时,您的其他问题之一已成功回答。我建议你接受解决它的答案。
    • 我这台电脑没装VS2010,只有2008,以后再看看。
    猜你喜欢
    • 2011-09-23
    • 2018-07-12
    • 2021-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多