Handling mouse events with cvSetMouseCallback

I am writing eye tracking code using OS X / X Code / OpenCV 2.2. As part of the Eye Tracker training process, I use cvSetMouseCallback to collect data according to the following: Right-click for the right eye; Left click for the left eye.

However, I found that the program can only work with a left click (CV_EVENT_LBUTTONDOWN), while it does not work with a right click (CV_EVENT_RBUTTONDOWN). At first I thought that these were problems with the trackpad and mouse, but it turned out that I had already set Secondary Click as β€œRight” in the car. Appreciate if anyone can shed light on this? Thanks for your time to look at 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; } } 
+6
opencv mouseevent
source share
2 answers

try deleting this line:

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

out of the loop and put it right after:

 cvNamedWindow( "video", 1 ); 

Yours faithfully!

+2
source share

I 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. In short, the value of the flags returned from the right click did not match the value returned from CV_EVENT_RBUTTONDOWN; printing the value of the flags that you get when you right-click and comparing it with the value of the flags stored in CV_EVENT_RBUTTONDOWN to find out if they can equally help.

0
source share

All Articles