Right click event acquisition does not work with opencv-python

Contents

As the title says. I was getting mouse events with opencv-python, ** When I tried to get right-click events, the menu was opened with the default function **. I've found that some people are doing well without doing anything, so the opencv version may be the cause.

--Environment Ubuntu 18.04 LTS
opencv install with pip (maybe this is the cause) --Date and time 2019/11/20

Solution

opencv 2.2 documentation »User interface stackoverflow/Why does a right click open a drop down menu in my OpenCV imshow() window?

According to **, right-clicking to open the menu seems to be a feature of this Qt backend **. So it seems that we need to get rid of this feature or use another backend. I'm not sure about the backend, and I don't need this feature, so I'll remove it.

[From the official document above]

--flags Window flags. The currently supported flags are: --CV_WINDOW_NORMAL or CV_WINDOW_AUTOSIZE: Specify CV_WINDOW_NORMAL to allow the user to change the window size. Conversely, if you specify CV_WINDOW_AUTOSIZE, the window size is automatically adjusted to fit the displayed image (see ShowImage), and the user cannot change the window size. --CV_WINDOW_FREERATIO or CV_WINDOW_KEEPRATIO: If CV_WINDOW_FREERATIO is specified, the image size will be adjusted without maintaining the aspect ratio, and conversely, if CV_WINDOW_KEEPRATIO is specified, the aspect ratio will be adjusted. --CV_GUI_NORMAL or CV_GUI_EXPANDED: CV_GUI_NORMAL allows you to display the previous window without the status bar and toolbar. Conversely, if you specify CV_GUI_EXPANDED, a new enhanced GUI will be displayed.

Apparently, you can do this by specifying the flags argument of cv2.namedWindow.

cv2.namedWindow("hoge", CV_GUI_NORMAL)
cv2.namedWindow("hoge", cv2.CV_GUI_NORMAL)
cv2.namedWindow("hoge", cv.CV_GUI_NORMAL)

None was good !!!

I wonder why ...

stackoverflow / NameError: global name'CV_GUI_NORMAL' is not defined Value is written, ...

WINDOW_AUTOSIZE = 1
WINDOW_FREERATIO = 256
WINDOW_FULLSCREEN = 1

WINDOW_GUI_EXPANDED = 0
WINDOW_GUI_NORMAL = 16

WINDOW_KEEPRATIO = 0
WINDOW_NORMAL = 0
WINDOW_OPENGL = 4096

WND_PROP_ASPECT_RATIO = 2

WND_PROP_AUTOSIZE = 1
WND_PROP_FULLSCREEN = 0
WND_PROP_OPENGL = 3
WND_PROP_VISIBLE = 4

Since WINDOW_GUI_NORMAL = 16, ...

cv2.namedWindow("hoge", 16)

It worked !!!

The above numbers are described in detail in High-level GUI.

Recommended Posts

Right click event acquisition does not work with opencv-python
gqlgen command does not work with go gqlgen
When wildcard specification does not work with pylint
Day 65 (Solution) Jupyter notebook does not work with Kernel Not Conected.
LocateCenterOnScreen does not work on PyAutoGui
(Under investigation) USB camera that does not work with WebRTC on RPi4
[VScode] autopep8 format does not work [Python]
Virtualenv does not work on Python3.5 (Windows)
Example of what to do when the sample script does not work (OpenCV-Python)
After installing php7.2, the php command does not work
Jinja2 2.9.6 does not work on Lambda Python 3 series
[Python] Expression (1,2) does not make tuples with parentheses
[systemd] How to deal with the problem that fancontrol does not work after suspending