图像未加载为灰度(skimage)

Image does not load as grayscale (skimage)

我正在尝试加载灰度图像,如下所示:

from skimage import data
from skimage.viewer import ImageViewer

img = data.imread('my_image.png', as_gray=True)

但是,如果我使用 img.shape 检查它的形状,结果是一个三维数组,而不是二维数组。我究竟做错了什么?

来自scikit-image documentationdata.imread的签名如下:

skimage.data.imread(fname, as_grey=False, plugin=None, flatten=None, **plugin_args)

您的代码无法正常工作,因为关键字参数 as_grey 拼写错误(您输入 as_gray)。

样本运行

In [4]: from skimage import data

In [5]: img_3d = data.imread('my_image.png', as_grey=False)

In [6]: img_3d.dtype
Out[6]: dtype('uint8')

In [7]: img_3d.shape
Out[7]: (256L, 640L, 3L)

In [8]: img_2d = data.imread('my_image.png', as_grey=True)

In [9]: img_2d.dtype
Out[9]: dtype('float64')

In [10]: img_2d.shape
Out[10]: (256L, 640L)