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

徘徊边缘 提交于 2020-05-30 07:53:13

问题


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():

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):

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

回答1:


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.




回答2:


In Python, you can pass the cv2.WINDOW_GUI_NORMAL flag to namedWindow() 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.



来源:https://stackoverflow.com/questions/56623487/why-does-a-right-click-open-a-drop-down-menu-in-my-opencv-imshow-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!