问题:cv2.imshow命令在opencv-python中无法正常工作

我正在使用opencv 2.4.2,python 2.7下面的简单代码创建了一个正确名称的窗口,但其内容为空白,并且不显示图像:

import cv2
img=cv2.imread('C:/Python27/03323_HD.jpg')
cv2.imshow('ImageWindow',img)

有谁知道这个问题吗?

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:

import cv2
img=cv2.imread('C:/Python27/03323_HD.jpg')
cv2.imshow('ImageWindow',img)

does anyone knows about this issue?


回答 0

imshow()仅适用于waitKey()

import cv2
img = cv2.imread('C:/Python27/03323_HD.jpg')
cv2.imshow('ImageWindow', img)
cv2.waitKey()

(更新窗口所需的整个消息循环都隐藏在其中。)

imshow() only works with 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.)


回答 1

我在这里找到了最适合我的答案:http : //txt.arboreus.com/2012/07/11/highgui-opencv-window-from-ipython.html

如果您运行交互式ipython会话,并希望使用highgui窗口,请首先执行cv2.startWindowThread()。

详细而言:HighGUI是简化的界面,用于显示来自OpenCV代码的图像和视频。它应该像这样简单:

import cv2
img = cv2.imread("image.jpg")
cv2.startWindowThread()
cv2.namedWindow("preview")
cv2.imshow("preview", img)

I found the answer that worked for me here: 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:

import cv2
img = cv2.imread("image.jpg")
cv2.startWindowThread()
cv2.namedWindow("preview")
cv2.imshow("preview", img)

回答 2

您必须cv2.waitKey(0)在之后使用cv2.imshow("window",img)。只有这样,它才能起作用。

import cv2
img=cv2.imread('C:/Python27/03323_HD.jpg')
cv2.imshow('Window',img)
cv2.waitKey(0)

You must use cv2.waitKey(0) after cv2.imshow("window",img). Only then will it work.

import cv2
img=cv2.imread('C:/Python27/03323_HD.jpg')
cv2.imshow('Window',img)
cv2.waitKey(0)

回答 3

如果您在Python控制台中运行,请执行以下操作:

img = cv2.imread("yourimage.jpg")

cv2.imshow("img", img); cv2.waitKey(0); cv2.destroyAllWindows()

然后,如果您按Enter图像,它将成功关闭图像,您可以继续运行其他命令。

If you are running inside a Python console, do this:

img = cv2.imread("yourimage.jpg")

cv2.imshow("img", img); cv2.waitKey(0); cv2.destroyAllWindows()

Then if you press Enter on the image, it will successfully close the image and you can proceed running other commands.


回答 4

我遇到了同样的问题。我试图从IDLE中读取图像,并尝试使用显示它cv2.imshow(),但是显示窗口冻结,并且显示pythonw.exe在尝试关闭窗口时无响应。

下面的帖子给出了可能发生这种情况的可能解释

pythonw.exe没有响应

基本上,不要从IDLE中执行此操作。编写脚本并从shell或脚本(如果在Windows中)直接运行它,方法是使用.pyw扩展名命名并双击它。显然,IDLE自己的事件之间存在冲突循环和来自GUI工具包的循环。

当我imshow()在脚本中使用并执行它,而不是直接在IDLE上运行它时,它就起作用了。

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.exe is not responding when trying to close the window.

The post below gives a possible explanation for why this is happening

pythonw.exe is not responding

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.

When I used imshow() in a script and execute it rather than running it directly over IDLE, it worked.


回答 5

最后添加cv2.waitKey(0)

add cv2.waitKey(0) in the end.


回答 6

对我来说,数字大于0的waitKey()有效

    cv2.waitKey(1)

For me waitKey() with number greater than 0 worked

    cv2.waitKey(1)

回答 7

您在此线程中的某处拥有了所有必要的部分:

if cv2.waitKey(): cv2.destroyAllWindows()

在IDLE中对我来说效果很好。

You’ve got all the necessary pieces somewhere in this thread:

if cv2.waitKey(): cv2.destroyAllWindows()

works fine for me in IDLE.


回答 8

如果您没有使它起作用,那么最好放

import cv2
img=cv2.imread('C:/Python27/03323_HD.jpg')
cv2.imshow('Window',img)
cv2.waitKey(0)

成一个文件并运行它。

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.


回答 9

之后不需要任何其他方法waitKey(0)(回复上面的代码)

import cv2
img=cv2.imread('C:/Python27/03323_HD.jpg')
cv2.imshow('ImageWindow',img)
cv2.waitKey(0)

出现窗口->单击窗口,然后单击Enter。窗口将关闭。

Doesn’t need any additional methods after waitKey(0) (reply for above code)

import cv2
img=cv2.imread('C:/Python27/03323_HD.jpg')
cv2.imshow('ImageWindow',img)
cv2.waitKey(0)

Window appears -> Click on the Window & Click on Enter. Window will close.


回答 10

如果选择使用“ cv2.waitKey(0)”,请确保已编写“ cv2.waitKey(0)”而不是“ cv2.waitkey(0)”,因为小写的“ k”也可能冻结程序。

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.


回答 11

我也遇到-215错误。我以为imshow是问题所在,但是当我将imread更改为读取不存在的文件时,那里没有错误。因此,我将图像文件放在工作文件夹中,并添加了cv2.waitKey(0)并成功运行。

I also had a -215 error. I thought imshow was the issue, but when I changed imread to read in a non-existent file I got no error there. So I put the image file in the working folder and added cv2.waitKey(0) and it worked.


回答 12

错误:函数imshow中的(-215)size.width> 0 && size.height> 0

因为找不到图像,所以产生此错误。因此,这不是imshow函数的错误。

error: (-215) size.width>0 && size.height>0 in function imshow

This error is produced because the image is not found. So it’s not an error of imshow function.


回答 13

我遇到了相同的215错误,可以通过提供图像的完整路径来解决该错误,例如在C:\ Folder1 \ Folder2 \ filename.ext中

I had the same 215 error, which I was able to overcome by giving the full path to the image, as in, C:\Folder1\Folder2\filename.ext


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。