think more
think more

Reputation: 136

OpenCV mouse event always receive event 10 and *flag* 0 on mouse scroll?

The problem is that on older version of Ubuntu 16.04 and OpenCV 3.4 getMouseWheelDelta(flag) always equals to zero, no matter the scroll wheel direction.

On my newer machine on Ubuntu 18.04 and never OpenCV 4 this function works perfectly, and getMouseWheelDelta(flag) return -1 or 1 depending on scroll direction:

#include "opencv2/core.hpp"
#include <iostream>
using namespace std;
using namespace cv;

void on_mouse(int event, int x, int y, int flag, void* userdata)
{
    printf("event = %d, %d\n", event, getMouseWheelDelta(flag));
    if (event==EVENT_MOUSEWHEEL)
    {
        if (getMouseWheelDelta(flags) > 0)
            zoom += 0.1f; // this newer gets executed on Ubuntu 16.04 and OpenCV 3.4
        else
            zoom -= 0.1f;
    }
}

int main()
{
    Mat mSrc = imread("xxxxx.jpg");
    imshow("src", mSrc); 
    setMouseCallback( "src", on_mouse, NULL );

    waitKey(0);

    return 0;
}

Get mouse wheel delta returns positive and negative values depending on scroll direction.

The problem is when I try the same code on older Ubuntu 16.04, on Open CV 3.4 I always receive event 10 and 0, no matter if I scroll up or down? How should I get scroll wheel direction as I use this info for zooming in to the photo?

In OpenCV documentation it says:

getMouseWheelDelta()

Note:
Mouse-wheel events are currently supported only on Windows.

But then why does it work on my Ubuntu 18.04 machine and how could I implement it for older versions of Linux?

Upvotes: 2

Views: 4692

Answers (2)

think more
think more

Reputation: 136

I checked other events and it appears, that scrolling in ubuntu 16.04 and OpenCV 3.4 is somehow different from newer versions and is event 11 instead of 10, which is horizontal mouse scrolling cv::EVENT_MOUSEHWHEEL. If I check this value, than it gives max 16 bit negative value if i scroll up and positive if I scroll down. This is strange but at least now I can know if user is scrolling with the mouse wheel.

cv::EVENT_MOUSEWHEEL = 10, // on newer versions returns +-1

cv::EVENT_MOUSEHWHEEL = 11 // on older versions returns +- max 16bit value

Upvotes: 0

J.D.
J.D.

Reputation: 4561

I just ran into a similar issue. In python the getMouseWheelDelta() function doesn't appear to exist, but I found a way to process the event. Maybe a similar implementation works in c++ for ubuntu as well.

def ProcessMouseEvent(event,x,y,flags, params):
    print(event)
    print(flags)
    # mousewheel event code is 10
    if event == 10:
            #sign of the flag shows direction of mousewheel
            if flags > 0:
                #scroll up
            else:
                #scroll down

Python version 3.7.1
OpenCV version 3.4.3

Upvotes: 5

Related Questions