为什么右键单击在OpenCV imshow()窗口中打开一个下拉菜单? [英] Why does a right click open a drop down menu in my OpenCV imshow() window?

查看:503
本文介绍了为什么右键单击在OpenCV imshow()窗口中打开一个下拉菜单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行 OpenCV Grabcut示例在我的系统上:

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

  • OpenCV版本4.1.0
  • Python版本3.6.8
  • IDLE版本3.6.8
  • Ubuntu 18.04.2

这是来自cv2.getBuildInformation()的构建信息:

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

在Grabcut示例脚本中,我需要使用右键鼠标按钮在对象周围绘制一个矩形".出于某种原因,当我单击鼠标右键(即单击并按住鼠标右键)时,会出现一个下拉菜单:

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

以前从未发生过,但是由于我重新格式化了计算机并重新安装了OpenCV,因此获得了此下拉菜单. imshow窗口看起来也不同.我尝试安装许多视频编解码器软件包(

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?

我使用命令pip3 install opencv-contrib-python安装了OpenCV.我知道我缺少一些软件包,所以我尝试安装(但失败-'通过regex无法找到任何软件包...')来自

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

推荐答案

您正在使用Qt highgui后端,它看起来像在不重新编译opencv的情况下就禁用了右键单击上下文菜单而无法禁用它.如果您以前没有看过,则可能是您使用了其他后端.

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.

如果您更喜欢使用Qt,并且不介意稍微更改opencv源并重建,则看起来像更改文件modules/highgui/src/window_QT.cpp中的DefaultViewPort::contextMenuEvent()方法以跳过构建菜单,只是返回可能会起作用(否则返回由于您添加了一些标志,因此可以选择构建菜单).当前,Qt highgui后端会使用常规菜单中可用的任何操作来自动创建菜单.

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.

以下是截至2019-06-18的当前opencv master分支中的方法的链接:

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

具有以下代码:

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());
    }
}

一种无需重新编译就可以工作的替代方法可能是在检查是否按住了其他修饰键(例如shift或ctrl)的同时使用向左拖动来进行选择.

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

更新: 如果您仍然希望使用Qt,但又不需要花哨的菜单选项和其他行为,那么看起来您可以在创建窗口时添加CV_GUI_NORMAL标志以禁用CV_GUI_EXPANDED Qt功能.

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.

这篇关于为什么右键单击在OpenCV imshow()窗口中打开一个下拉菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆