Why does a right click open a drop down menu in my OpenCV imshow() window?

Question:

I am trying to run the OpenCV Grabcut Sample on my system:

  • OpenCV version 4.1.0
  • Python version 3.6.8
  • IDLE version 3.6.8
  • Ubuntu 18.04.2

This is the build information from cv2.getBuildInformation():
build information

In the Grabcut Sample script, I need to ‘draw a rectangle around the object using the right mouse button.’ For some reason, a drop down menu appears when I click the right mouse button (this is me clicking and holding the right mouse button):

drop down

This didn’t happen before, but since I reformatted my computer and reinstalled OpenCV I get this drop down menu. The imshow window looks different too. I tried installing lots of video codec packages (from this tutorial), but that didn’t help.

This drop down menu interferes with the mouse callback functions. How can I get rid of this drop down menu?

I installed OpenCV with the command pip3 install opencv-contrib-python. I knew I was missing some packages so I tried to install (but failed – ‘couldn’t find any package by regex…’) these packages from this tutorial:

sudo apt-get install python-devel numpy
sudo apt-get install gcc gcc-c++
sudo apt-get install gtk2-devel
sudo apt-get install ffmpeg-devel
sudo apt-get install gstreamer-plugins-base-devel
Asked By: Stephen Meschke

||

Answers:

You’re using the Qt highgui backend, which looks like it forces the right-click context menu without the ability to disable it without recompilation of opencv. If you didn’t see it before, it’s likely you were using a different backend.

If you prefer using Qt and don’t mind altering the opencv source slightly and rebuilding, it looks like changing the DefaultViewPort::contextMenuEvent() method in file modules/highgui/src/window_QT.cpp to skip building the menu and just returning will probably work (or else have it optionally build the menu due to some flag that you add). Currently, the Qt highgui backend auto-creates the menu using whatever actions are available in the regular menu.

Here’s a link to the method in the current opencv master branch as of 2019-06-18:

https://github.com/opencv/opencv/blob/1d2ef6b2a14fd5f80277d64b14e4a9a2faddc7d8/modules/highgui/src/window_QT.cpp#L2697

which has this code:

void DefaultViewPort::contextMenuEvent(QContextMenuEvent* evnt)
{
    if (centralWidget->vect_QActions.size() > 0)
    {
        QMenu menu(this);

        foreach (QAction *a, centralWidget->vect_QActions)
            menu.addAction(a);

        menu.exec(evnt->globalPos());
    }
}

An alternative that might work without recompilation might be to use left dragging for selection while checking for an additional modifier key being held down (like shift or ctrl).

I haven’t actually tested either of these approaches BTW, so good luck! 🙂

UPDATE:
If you still want Qt but don’t need the fancy menu options and extra behavior and such, it looks like you can add the CV_GUI_NORMAL flag when creating the window to disable the CV_GUI_EXPANDED Qt features.

Answered By: rob3c

In Python, you can pass the cv2.WINDOW_GUI_NORMAL flag to namedWindow() to disable the dropdown (flag is supported only if you have Qt backend):

cv2.namedWindow("window_name", cv2.WINDOW_GUI_NORMAL)

And then call

cv2.imshow("window_name", img)

Link to documentation of the namedWindow function is here.

Answered By: Kashinath Patekar

One additional nuance to above answers. If i just use cv2.WINDOW_GUI_NORMAL opencv was showing the original image ignoring the resizing code, but I wanted to see the resized images/frames. So, I restored the other default flags as follows:

cv2.namedWindow("window_name", flags=cv2.WINDOW_AUTOSIZE | cv2.WINDOW_KEEPRATIO | cv2.WINDOW_GUI_NORMAL)
Answered By: Abhi25t
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.