Mouse event handling with cvSetMouseCallback

◇◆丶佛笑我妖孽 提交于 2019-12-01 03:13:30

问题


I am writing a code for eye tracker using OS X / X Code / OpenCV 2.2. As part of the Eye Tracker training process, I am using cvSetMouseCallback to capture the data as per following: Right click for right eye; Left click for left eye.

However, I found that the program could only work with left click (CV_EVENT_LBUTTONDOWN) while it does not work with right click (CV_EVENT_RBUTTONDOWN). At first, I thought it was a trackpad and mouse setting issues, however, it turns out that I have already set both Secondary Click as "Right" in the machine. Appreciate if someone could shed some light on this? Thank you for your time to look into this.

For those interested, I have a simple code snippet for cvSetMouseCallback:

#include <cv.h>
#include <cxcore.h>
#include <highgui.h>

void my_mouse_callback( int event, int x, int y, int flags, void* param );

int main (int argc, const char * argv[]) 
{

CvCapture *capture;
IplImage  *img;
int       key = 0;

// initialize camera
capture = cvCaptureFromCAM( 0 );

// always check
assert( capture );

// create a window
cvNamedWindow( "video", 1 );

while( key != 'q' ) {
    // get a frame
    img = cvQueryFrame( capture );

// set the mouse callback function. 
cvSetMouseCallback( "video", my_mouse_callback, (void*) img);

    // always check
    if( !img ) break;

// 'fix' frame
    cvFlip( img, img, 1 );
    img->origin = 0;

cvShowImage("video", img );

    // quit if user press 'q'
    key = cvWaitKey( 5 );

}

// free memory
cvReleaseCapture( &capture );
cvDestroyWindow( "video" );

return 0;

}

void my_mouse_callback( int event, int x, int y, int flags, void* param ){
//IplImage* image = (IplImage*) param;


switch( event ){
    case CV_EVENT_LBUTTONDOWN:
        printf("LBUTTONDOWN\n");
        break;

    case CV_EVENT_RBUTTONDOWN:
        printf("RBUTTONDOWN\n");
        break;

    case CV_EVENT_FLAG_CTRLKEY:
        printf("FLAG_LBUTTONDBLCLK\n");
        break;
}

}

回答1:


try to remove this line:

cvSetMouseCallback( "video", my_mouse_callback, (void*) img);

from the loop and place it immediately after:

cvNamedWindow( "video", 1 );

Regards !




回答2:


I can see that this is an old post, but for people looking for help in the future: I had a similar problem using opencv (in python) and this answer helped me out. In short, the flags value returned from right clicking did not match the value returned from CV_EVENT_RBUTTONDOWN; printing the flags value that you get when you right click and comparing it to the flags value stored in CV_EVENT_RBUTTONDOWN to see if they're the same may help.



来源:https://stackoverflow.com/questions/5000721/mouse-event-handling-with-cvsetmousecallback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!