Skip to content Skip to sidebar Skip to footer

Why Does A Right Click Open A Drop Down Menu In My Opencv Imshow() Window?

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.getB

Solution 1:

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.

Solution 2:

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:

voidDefaultViewPort::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.

Post a Comment for "Why Does A Right Click Open A Drop Down Menu In My Opencv Imshow() Window?"