4

I'm looking to remove the default zoom behavior while scrolling up and down with the mouse wheel on OpenCV. I set a mouse callback method (defined below) in order to override this behavior without success :

import cv2

def mouse_callback(event, x, y, flags, param):
    global index

    if event == cv2.EVENT_MOUSEWHEEL :
        if flags > 0:
            index += 1
        elif flags < 0:
            index -= 1
        index %= length

cv2.namedWindow('window', cv2.WINDOW_NORMAL)
cv2.setMouseCallback('window', mouse_callback)
cv2.imshow('window', path)

Has anyone succeeded in removing that default behavior ?

orious
  • 41
  • 1
  • 4

2 Answers2

2

Ok, it doesn't seem to be possible the easy way because opencv-python uses the HighGui by default which was/is mainly intended for debugging.

Solution 1

That's what I did and it works. You need to rebuild the opencv-python project yourself and make some changes in the included opencv in the file window_QT.cpp. In there you find the function DefaultViewPort::wheelEvent in which you can remove the scaleView function call.

Solution 2

You need to rebuild opencv-python as well and choose the GTK instead of the QT ui. I got that suggestion from here.

How to build opencv-python?

How to build the opencv-python project is described here. You mainly need to git clone and run python setup.py build and python setup.py install. Be aware that solutions 1 and 2 only work on the machines having the altered opencv library (unless you look into packaging).

Solution 3

You use a completely other library for displaying your images/frames, like tkinter or PyGame.

Solution 4

This depends on your use case but with python-evdev you can "Get exclusive access to a device". Meaning, only your application can receive events from a specific device like mouse. There might be an option as well to only ego-grab certain events if you dig deeper.

Kim
  • 1,757
  • 1
  • 17
  • 32
0

This worked for me:

pip install opencv-contrib-python

Got the idea from another discussion regarding cv2 window

I'm not sure what it did, but maybe it forced cv2 into using the GTK backend instead of QT.

ishahak
  • 6,585
  • 5
  • 38
  • 56