Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mouse event handling with cvSetMouseCallback

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

}
like image 718
CCS Avatar asked Feb 15 '11 06:02

CCS


1 Answers

try to remove this line:

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

from the loop and place it immediately after:

cvNamedWindow( "video", 1 );

Regards !

like image 171
patxiska Avatar answered Oct 13 '22 10:10

patxiska