cv2.imshow 命令在 opencv-python 中无法正常工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/21810452/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
cv2.imshow command doesn't work properly in opencv-python
提问by top.eng
I'm using opencv 2.4.2, python 2.7 The following simple code created a window of the correct name, but its content is just blank and doesn't show the image:
我正在使用 opencv 2.4.2, python 2.7 下面的简单代码创建了一个正确名称的窗口,但它的内容只是空白并且不显示图像:
import cv2
img=cv2.imread('C:/Python27/03323_HD.jpg')
cv2.imshow('ImageWindow',img)
does anyone knows about this issue?
有人知道这个问题吗?
回答by berak
imshow()only works with waitKey():
imshow()仅适用于waitKey():
import cv2
img = cv2.imread('C:/Python27/03323_HD.jpg')
cv2.imshow('ImageWindow', img)
cv2.waitKey()
(The whole message-loop necessary for updating the window is hidden in there.)
(更新窗口所需的整个消息循环隐藏在那里。)
回答by rkdasari
I faced the same issue. I tried to read an image from IDLE and tried to display it using cv2.imshow(), but the display window freezes and shows pythonw.exeis not responding when trying to close the window.
我遇到了同样的问题。我尝试从 IDLE 读取图像并尝试使用 显示它cv2.imshow(),但pythonw.exe在尝试关闭窗口时显示窗口冻结并且显示没有响应。
The post below gives a possible explanation for why this is happening
下面的帖子给出了为什么会发生这种情况的可能解释
"Basically, don't do this from IDLE. Write a script and run it from the shell or the script directly if in windows, by naming it with a .pyw extension and double clicking it. There is apparently a conflict between IDLE's own event loop and the ones from GUI toolkits."
"基本上,不要从 IDLE 执行此操作。编写脚本并从 shell 或直接在 Windows 中运行它,通过使用 .pyw 扩展名命名并双击它。IDLE 自己的事件之间显然存在冲突循环和 GUI 工具包中的那些。”
When I used imshow()in a script and execute it rather than running it directly over IDLE, it worked. 
当我imshow()在脚本中使用并执行它而不是直接在 IDLE 上运行它时,它起作用了。
回答by Akhorus
I found the answer that worked for me here: http://txt.arboreus.com/2012/07/11/highgui-opencv-window-from-ipython.html
我在这里找到了对我有用的答案:http: //txt.arboreus.com/2012/07/11/highgui-opencv-window-from-ipython.html
If you run an interactive ipython session, and want to use highgui windows, do cv2.startWindowThread() first.
In detail: HighGUI is a simplified interface to display images and video from OpenCV code. It should be as easy as:
如果您运行交互式 ipython 会话,并且想要使用 highgui 窗口,请先执行 cv2.startWindowThread()。
详细说明:HighGUI 是一个简化的界面,用于显示来自 OpenCV 代码的图像和视频。它应该很简单:
import cv2
img = cv2.imread("image.jpg")
cv2.startWindowThread()
cv2.namedWindow("preview")
cv2.imshow("preview", img)
回答by AdityaIntwala
You must use cv2.waitKey(0)after cv2.imshow("window",img). Only then will it work.
您必须cv2.waitKey(0)在 之后使用cv2.imshow("window",img)。只有这样才能奏效。
import cv2
img=cv2.imread('C:/Python27/03323_HD.jpg')
cv2.imshow('Window',img)
cv2.waitKey(0)
回答by iamchriskelley
You've got all the necessary pieces somewhere in this thread:
您已在此线程中的某处获得了所有必要的部分:
if cv2.waitKey(): cv2.destroyAllWindows()
works fine for me in IDLE.
在 IDLE 中对我来说很好用。
回答by Joseph
If you have not made this working, you better put
如果你还没有让这个工作,你最好把
import cv2
img=cv2.imread('C:/Python27/03323_HD.jpg')
cv2.imshow('Window',img)
cv2.waitKey(0)
into one file and run it.
成一个文件并运行它。
回答by Guilherme Gir?o
If you choose to use "cv2.waitKey(0)", be sure that you have written "cv2.waitKey(0)" instead of "cv2.waitkey(0)", because that lowercase "k" might freeze your program too.
如果您选择使用“cv2.waitKey(0)”,请确保您写的是“cv2.waitKey(0)”而不是“cv2.waitkey(0)”,因为小写的“k”也可能会冻结您的程序.
回答by Alvaro Fernandez
error: (-215) size.width>0 && size.height>0 in function imshow
错误:(-215) 函数 imshow 中的 size.width>0 && size.height>0
This error is produced because the image is not found. So it's not an error of imshow function.
产生此错误是因为找不到图像。所以这不是 imshow 函数的错误。
回答by Pygirl
add cv2.waitKey(0)in the end.
最后添加cv2.waitKey(0)。
回答by Dharma
If you are running inside a Python console, do this:
如果您在 Python 控制台内运行,请执行以下操作:
img = cv2.imread("yourimage.jpg")
cv2.imshow("img", img); cv2.waitKey(0); cv2.destroyAllWindows()
Then if you press Enteron the image, it will successfully close the image and you can proceed running other commands.
然后,如果您按下Enter图像,它将成功关闭图像,您可以继续运行其他命令。

