为什么 cv2.imshow() 在我的 python 编译器中导致错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22829210/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:46:27  来源:igfitidea点击:

why cv2.imshow() results in error in my python compiler?

pythonopencv

提问by praveen

Hi friends i just now installed opencv and checking the basic code but it results in error. The code is

嗨,朋友们,我刚刚安装了 opencv 并检查了基本代码,但导致错误。代码是

import numpy as np
import cv2
img=cv2.imread('C:\Users\Pravin\Desktop\a.jpeg',1)
cv2.namedWindow('img',cv2.WINDOW_NORMAL)
cv2.Waitkey(10000)
cv2.imshow('cv2.WINDOW_NORMAL',img)
cv2.destoryAllWindows()

The error for cv2.imshow() is

cv2.imshow() 的错误是

Traceback (most recent call last):
 File "<pyshell#3>", line 1, in <module>
cv2.imshow('image',img)
error: ..\..\..\src\opencv\modules\highgui\src\window.cpp:261: error: (-215)
size.width>0 && size.height>0

It was very helpful to me with your answer. Thanks in advance

你的回答对我很有帮助。提前致谢

回答by Sergei Nosov

Most likely, the imread call didn't succeed. Make sure the image "C:\Users\Pravin\Desktop\a.jpeg" exists. (The extension .jpeg seems unusual, maybe it has to be .jpg?)

最有可能的是, imread 调用没有成功。确保图像“C:\Users\Pravin\Desktop\a.jpeg”存在。(扩展名 .jpeg 似乎不寻常,也许它必须是 .jpg?)

Also, as Hyperboreus suggests, please, try using forward slashes in the filename "C:/Users/Pravin/Desktop/a.jpg", or escape backslashes

另外,正如 Hyperboreus 所建议的,请尝试在文件名“C:/Users/Pravin/Desktop/a.jpg”中使用正斜杠,或转义反斜杠

"C:\Users\Pravin\Desktop\a.jpg"

回答by Pranav Totla

The error says that the image you opened doesn't satisfy the condition height > 0and width > 0. This may have several reasons.

错误表示您打开的图像不满足条件height > 0width > 0。这可能有几个原因。

Most of the times, it is due to an inexistent image address given in imread.

大多数情况下,这是由于imread.

Sometimes it may be also because the complier failed to load the image. For example, if you write some random strings in notepad and save the file as a.jpg, the compiler may not be able to load it.

有时也可能是因为编译器加载图像失败。例如,如果您在记事本中写入一些随机字符串并将文件另存为a.jpg,则编译器可能无法加载它。

回答by Sundu_Rapid

Try this...

尝试这个...

import numpy as np
import cv2
img = cv2.imread('E:/Images/ece/1.png',1)
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

回答by M Sudhakar

It is because, python compiler cannot find the image in the place. if you copy the image in the python working directory and do this. it worked for me.

这是因为,python 编译器无法在该位置找到图像。如果您将图像复制到 python 工作目录中并执行此操作。它对我有用。

    # keep image in the current working directory
    img=cv2.imread('roi.jpg',1) 
    cv2.imshow('image',img)

回答by Vikas Gautam

For me it worked when i just changed jpeg to jpg

对我来说,当我将 jpeg 更改为 jpg 时它起作用了

Try this, may be it will work

试试这个,可能会起作用

import numpy as np
import cv2
img=cv2.imread('C:\Users\Pravin\Desktop\a.jpg',1)    #changed image format to jpg
cv2.namedWindow('img',cv2.WINDOW_NORMAL)
cv2.Waitkey(10000)
cv2.imshow('cv2.WINDOW_NORMAL',img)
cv2.destoryAllWindows()