简体   繁体   English

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

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

Hi friends i just now installed opencv and checking the basic code but it results in error.嗨,朋友们,我刚刚安装了 opencv 并检查了基本代码,但导致错误。 The code is代码是

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提前致谢

Most likely, the imread call didn't succeed.最有可能的是, imread 调用没有成功。 Make sure the image "C:\\Users\\Pravin\\Desktop\\a.jpeg" exists.确保图像“C:\\Users\\Pravin\\Desktop\\a.jpeg”存在。 (The extension .jpeg seems unusual, maybe it has to be .jpg?) (扩展名 .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"

The error says that the image you opened doesn't satisfy the condition height > 0 and width > 0 .该错误表示您打开的图像不满足条件height > 0width > 0 This may have several reasons.这可能有几个原因。

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 ,则编译器可能无法加载它。

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

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

It is because, python compiler cannot find the image in the place.这是因为,python 编译器无法在该位置找到图像。 if you copy the image in the python working directory and do this.如果您将图像复制到 python 工作目录中并执行此操作。 it worked for me.它对我有用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM