NameError: global name 'CV_GUI_NORMAL' is not defined
NameError: global name 'CV_GUI_NORMAL' is not defined
我在 Ubuntu 14.04 上使用 Python 和 OpenCV 进行编码。当我单击鼠标右键时,关联的鼠标事件 cv2.EVENT_RBUTTONDOWN
不起作用,我宁愿获得上下文菜单 ("actions")。有没有办法禁用上下文菜单弹出窗口?
一位用户给了我提示,我相信解决方案就在那里。他让我添加 CV_GUI_NORMAL
,如 here 所示。
所以我 运行: cv2.namedWindow("Window",CV_GUI_NORMAL)
但是我得到了这个错误:
NameError: global name 'CV_GUI_NORMAL' is not defined
当我按照以下用户的评论尝试 cv2.CV_GUI_NORMAL
时,出现此错误:
AttributeError: 'module' object has no attribute 'CV_GUI_NORMAL'
请注意有人提出了类似的问题here,但我不想更改 OpenCV 代码。
如何解决这个问题?
.
官方documentation说:
Python:
cv.NamedWindow(name, flags=CV_WINDOW_AUTOSIZE) → None
Parameters:
name – Name of the window in the window caption that may be used as a window identifier.
flags –
Flags of the window. The supported flags are:
WINDOW_NORMAL If this is set, the user can resize the window (no constraint).
WINDOW_AUTOSIZE If this is set, the window size is automatically adjusted to fit the displayed image (see imshow() ), and you cannot change the window size manually.
WINDOW_OPENGL If this is set, the window will be created with OpenGL support.
只有一些带有 Qt 后端支持的实现 CV_GUI_NORMAL
。看来您别无选择,只能安装支持 Qt 的 cv2 或使用其他变量。
在这种情况下,您将使用 cv2.CV_WINDOW_NORMAL
。
对于初学者来说,如果您不需要 Qt 支持,您可以在没有 Qt 支持的情况下进行构建。在许多情况下,它似乎弊大于利。所以最好设置标志 WINDOW_OPENGL
:这样你就可以禁用 QT 支持并获得 OpenGL 支持。
cv2
中允许的 windows 值为:
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
您可以按如下方式使用cv2.WINDOW_GUI_NORMAL
:
cv2.namedWindow('desired_name_of_window', flags= cv2.WINDOW_GUI_NORMAL)
我在 Ubuntu 14.04 上使用 Python 和 OpenCV 进行编码。当我单击鼠标右键时,关联的鼠标事件 cv2.EVENT_RBUTTONDOWN
不起作用,我宁愿获得上下文菜单 ("actions")。有没有办法禁用上下文菜单弹出窗口?
一位用户给了我提示,我相信解决方案就在那里。他让我添加 CV_GUI_NORMAL
,如 here 所示。
所以我 运行: cv2.namedWindow("Window",CV_GUI_NORMAL)
但是我得到了这个错误:
NameError: global name 'CV_GUI_NORMAL' is not defined
当我按照以下用户的评论尝试 cv2.CV_GUI_NORMAL
时,出现此错误:
AttributeError: 'module' object has no attribute 'CV_GUI_NORMAL'
请注意有人提出了类似的问题here,但我不想更改 OpenCV 代码。
如何解决这个问题?
.
官方documentation说:
Python: cv.NamedWindow(name, flags=CV_WINDOW_AUTOSIZE) → None Parameters: name – Name of the window in the window caption that may be used as a window identifier. flags – Flags of the window. The supported flags are:
WINDOW_NORMAL If this is set, the user can resize the window (no constraint).
WINDOW_AUTOSIZE If this is set, the window size is automatically adjusted to fit the displayed image (see imshow() ), and you cannot change the window size manually.
WINDOW_OPENGL If this is set, the window will be created with OpenGL support.
只有一些带有 Qt 后端支持的实现 CV_GUI_NORMAL
。看来您别无选择,只能安装支持 Qt 的 cv2 或使用其他变量。
在这种情况下,您将使用 cv2.CV_WINDOW_NORMAL
。
对于初学者来说,如果您不需要 Qt 支持,您可以在没有 Qt 支持的情况下进行构建。在许多情况下,它似乎弊大于利。所以最好设置标志 WINDOW_OPENGL
:这样你就可以禁用 QT 支持并获得 OpenGL 支持。
cv2
中允许的 windows 值为:
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
您可以按如下方式使用cv2.WINDOW_GUI_NORMAL
:
cv2.namedWindow('desired_name_of_window', flags= cv2.WINDOW_GUI_NORMAL)