Python openCV:无法显示图像

时间:2015-09-07 13:23:14

标签: python python-2.7 opencv

我正在使用python opencv,我正在尝试导入图像并显示它但是我收到以下错误:

import numpy as np
import cv2
img = cv2.imread('messi5.jpg',0)
cv2.imshow('image',img)
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in cv::imshow, fi
le C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv\modules\highgui\src\
window.cpp, line 271
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
cv2.error: C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv\modules\high
gui\src\window.cpp:271: error: (-215) size.width>0 && size.height>0 in function

enter image description here

2 个答案:

答案 0 :(得分:3)

In [1]: a = None
In [2]: type(a)
Out[2]: NoneType
In [3]: import cv2
In [4]: img = cv2.imread('messi.jpg',0)
In [5]: type(img)
Out[5]: numpy.ndarray
In [6]: img = cv2.imread('messii.jpg',0)
In [7]: type(img)
Out[7]: NoneType

此处您的img类型为NoneType。所以,这是图像未命中或不存在的问题。请检查拼写或文件的存在。

答案 1 :(得分:0)

请确保您的图像文件路径和 img_names(existed) 应该是正确的,并且 imread() 对象结果不应该是 nonetype 然后

cv2.imshow('image', img)  
cv2.waitkey(0)

那么你的图片就可以显示了