通读 skimage.io.imread 的图像形状可疑

image read through skimage.io.imread have suspicious shape

我正在尝试使用 skimage.io.imread 读取 RGB 图像。但是看了图片后发现图片形状不对,print(img.shape)表示 图像形状为 (2,)。显示问题的完整代码是:

from skimage import io
img = io.imread(path/to/the/image)
print(img.shape)

我也尝试使用opencv的python包读取图像,返回的形状是正确的(高*宽*3)。

使用的 skimage 版本是 0.12.3,有人可以解释一下我使用这个包的方式有什么问题吗?或者这真的是一个错误吗?

单击 link 以获得 测试图像

编辑1

测试图片在上传时被修改了,未修改的版本在skimage github repo 上是here. I have also opened an issue,结果测试图片是 双帧图像,但第二帧是空的。你可以考虑这张图片 "corrupted" 图片。

为了读取正确的图像,您可以使用此解决方法,img = io.imread(/path/to/the/image, img_num=0)

检查您上传的图片类型。

如果您上传彩色图片,您将获得图片的大小以及通道数 (1920, 2560, 3)

只要上传的图片是彩色图片,您就会收到 3.

否则,如果图像是灰度或二值图像,您将获得图像的大小 (1920, 2560)

您可以通过强制 skimage.io.imread() 使用 matplotlib 来解决这个问题:

In [131]: from skimage import io

In [132]: img = io.imread('156.jpg', plugin='matplotlib')

In [133]: img.shape
Out[133]: (978L, 2000L, 3L)

您的图片很可能是多对象 JPG。如果您尝试使用 PIL(这是默认插件)读取它,您会得到一个由两个对象组成的 NumPy 数组。第一个对象是图像本身,第二个对象可能是缩略图,但 PIL 没有正确处理它:

In [157]: img = io.imread('156.jpg', plugin='pil')

In [158]: img.dtype
Out[158]: dtype('O')

In [159]: img.shape
Out[159]: (2L,)

In [160]: img[0].shape
Out[160]: (978L, 2000L, 3L)

In [161]: img[1]
Out[161]: array(<PIL.MpoImagePlugin.MpoImageFile image mode=RGB size=2000x978 at 0x111DBCF8>, dtype=object)

查看 this thread 以了解有关此问题的更多信息。