Python 断言失败:函数 imshow 中的 size.width>0 && size.height>0
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31586385/
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
Assertion failure : size.width>0 && size.height>0 in function imshow
提问by Bibek Ghimire
i am using opencv2 and python on raspberry pi. and i am new with python and opencv. i tried to read a jpeg image and display image it shows the following error:
我在树莓派上使用 opencv2 和 python。我是 python 和 opencv 的新手。我试图读取 jpeg 图像并显示图像,它显示以下错误:
/home/pi/opencv-2.4.9/modules/highgui/src/window.cpp:269: \
error: (-215) size.width>0 && size.height>0 in function imshow.
and the code is:
代码是:
import cv2
# windows to display image
cv2.namedWindow("Image")
# read image
image = cv2.imread('home/pi/bibek/book/test_set/bbb.jpeg')
# show image
cv2.imshow("Image", image)
# exit at closing of window
cv2.waitKey(0)
cv2.destroyAllWindows()
采纳答案by Krumelur
The image fails to load (probably because you forgot the leading /
in the path). imread
then returns None. Passing None
to imshow
causes it to try to create a window of size 0x0, which fails.
图像无法加载(可能是因为您忘记/
了路径中的前导)。imread
然后返回无。传递None
到imshow
导致它尝试创建一个大小为 0x0 的窗口,但失败了。
The poor error handling in cv
probably owes to its quite thin wrapper layer on the C++ implementation (where returning NULL on error is a common practice).
糟糕的错误处理cv
可能是由于它在 C++ 实现上的非常薄的包装层(在错误时返回 NULL 是一种常见的做法)。
回答by Mohammad Yasir K P
While using Raspbian in Rpi 3 I had the same problem when trying to read qrcodes. The error is because cv2 was not able to read the image. If using png image install pypng module.
在 Rpi 3 中使用 Raspbian 时,我在尝试读取二维码时遇到了同样的问题。错误是因为 cv2 无法读取图像。如果使用 png 图像安装 pypng 模块。
sudo pip install pypng
回答by Malware_656
it's the path which is causing the problem, i had the same problem but when i gave the full path of the image it was working perfectly.
这是导致问题的路径,我遇到了同样的问题,但是当我提供图像的完整路径时,它运行良好。
回答by Rahul Singal
One of the reasons, this error is caused is when there is no file at the path specified. So, a good practice will be to verify the path like this ( If you are on a linux based machine ):
导致此错误的原因之一是指定路径中没有文件。因此,一个好的做法是验证这样的路径(如果您使用的是基于 linux 的机器):
ls <path-provided-in-imread-function>
You will get an error if the path is incorrect or the file is missing.
如果路径不正确或文件丢失,您将收到错误消息。
回答by Pe Dro
In my case, I had forgotten to change the working directory of my terminal to that of my code+testImage. Hence, it failed to find the image there.
就我而言,我忘记将终端的工作目录更改为代码+testImage 的工作目录。因此,它未能在那里找到图像。
Finally, this is what worked for me:
最后,这对我有用:
I saved the image and Python file on Desktop. I changed my cmd directory to it,
我将图像和 Python 文件保存在桌面上。我将我的 cmd 目录更改为它,
cd Desktop
And then checked for my file:
然后检查我的文件:
ls
And this was my code that worked:
这是我的代码有效:
import cv2
import numpy as np
im = cv2.imread('unnamed.jpg')
#Display the image
cv2.imshow('im',im)
cv2.waitKey(2000) #Milliseconds