Ask Your Question
1

How to handle mouse wheel event in OpenCV?

asked 2013-10-22 05:48:59 -0600

younesken gravatar image

Hello,

All is in the title: how to manage an event of mouse wheel on OpenCV ?

Here is my task : I would like to write a program that displays an image in OpenCV window. I click anywhere in the image to define the " upper left" corner of a square that I want to draw, and with the mouse wheel , I draw while controlling the size of his side .

It's that simple , but difficult to find out how. For now , the wheel gave me a zoom, so .. Event exists somewhere but I did find the following event:

CV_EVENT_MOUSEMOVE - When the mouse pointer moves over the specified window CV_EVENT_LBUTTONDOWN - When the left button of the mouse is pressed on the specified window CV_EVENT_RBUTTONDOWN - When the right button of the mouse is pressed on the specified window CV_EVENT_MBUTTONDOWN - When the middle button of the mouse is pressed on the specified window CV_EVENT_LBUTTONUP - When the left button of the mouse is released on the specified window CV_EVENT_RBUTTONUP - When the right button of the mouse is released on the specified window CV_EVENT_MBUTTONUP - When the middle button of the mouse is released on the specified window

hoping to find an answer , Thank you for your help,

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2013-10-22 08:21:03 -0600

updated 2013-10-22 08:23:45 -0600

There is no standard event to handle mousewheel down or up on the onMouse method. You can, however, combine more than one event to create a drag mouse effect, which would complete your task.

Something like

void MouseCB::onMouse( int event, int x, int y, int flags, void* userData)
{
    if (event == CV_EVENT_LBUTTONDOWN)
       {
        int leftcornerX = x;
        int leftcornerY = y;

        while( event != CV_EVENT_LBUTTONUP)
             {
            //method to retrieve your originalframe with no rectangles drawn -> either the original frame is a
            //global variable, or you have to pass it using the void* userData input argument.

                Mat copy = originalframe;
            rectangle(copy, Point(leftcornerX, leftcornerY), Point(x, y), Scalar(255, 0, 0), 1, 8, 0);
            imshow("Frame", copy);
            waitKey(10);


           }
      }

}

This draws a rectangle between the point you first clicked with the left mouse button and the current mouse position, until you lift the mouse button.

edit flag offensive delete link more

Comments

Thank You Median !

younesken gravatar imageyounesken ( 2013-10-24 10:52:37 -0600 )edit

Sorry, but how should the while loop ever terminate? I dont think this can work, as the loop exit condition will never be satisfied

AR0x7E7 gravatar imageAR0x7E7 ( 2014-05-01 09:17:31 -0600 )edit

The while loop only verifies if user presses mouse button 1, then the while cycle runs until user lifts mouse button

Pedro Batista gravatar imagePedro Batista ( 2014-05-04 18:04:17 -0600 )edit

Question Tools

Stats

Asked: 2013-10-22 05:48:59 -0600

Seen: 9,028 times

Last updated: Oct 22 '13