abdfahim
abdfahim

Reputation: 2563

Skimage rgb2gray reduces one dimension

I am trying to convert multiple RGB images to grayscale. However, I am loosing one dimension

# img is an array of 10 images of 32x32 dimensions in RGB
from skimage.color import rgb2gray
print(img.shape) # (10, 32, 32, 3)
img1 = rgb2gray(img)
print(img.shape) # (10, 32, 3)

As you can see, though the shape of img is expected to be (10, 32, 32, 1), it is coming out as (10, 32, 3).

What point am I missing?

Upvotes: 2

Views: 712

Answers (1)

sascha
sascha

Reputation: 33552

This function assumes the input to be one single image of dims 3 or 4 (with alpha).

(As your input has 4 dimensions, it's interpreted as single image of RGB + Alpha; not as N images of 3 dimensions)

If you got multiple images you will need to loop somehow, like this (untested):

import numpy as np
from skimage.color import rgb2gray
print(img.shape) # (10, 32, 32, 3)

img_resized = np.stack([rgb2gray(img[i]) for i in range(img.shape[0])])

Upvotes: 3

Related Questions