Image does not load as grayscale (skimage)
Asked Answered
K

1

9

I'm trying to load an image as grayscale as follows:

from skimage import data
from skimage.viewer import ImageViewer

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

However, if I check for its shape using img.shape it turns out to be a three-dimensional, and not two-dimensional, array. What am I doing wrong?

Kunstlied answered 15/6, 2017 at 8:14 Comment(0)
N
13

From scikit-image documentation, the signature of data.imread is as follows:

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

Your code does not work properly because the keyword argument as_grey is misspelled (you put as_gray).

Sample run

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)
Newsstand answered 19/6, 2017 at 10:9 Comment(5)
Got it! by the way: is using data.imread() to be preferred over rgb2gray() from the rgb2gray package?Kunstlied
I would have done img = skimage.io.imread('my_image.png', as_grey=True) because images that are already in grey-scale format are not converted. If you pass rgb2gray an image which is not 3-D or 4-D, you get a ValueError.Newsstand
Alert! - UserWarning: as_grey has been deprecated in favor of as_grayChildbearing
Fun fact: Grey is the British way of spelling, while Gray is more commonly used among Americans.Buttonhook
In newer versions, this should now be skimage.io.imread.Afterdeck

© 2022 - 2024 — McMap. All rights reserved.