Skimage rgb2gray 减少一维

Skimage rgb2gray reduces one dimension

我正在尝试将多个 RGB 图像转换为灰度图像。但是,我失去了一个维度

# 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)

如您所见,虽然 img 的形状预计为 (10, 32, 32, 1),但结果却是 (10, 32, 3)。

我错过了什么?

function 假设输入是一张 dims 3 或 4(带 alpha)的图像。

(由于您的输入有 4 个维度,它被解释为 RGB + Alpha 的单个图像;而不是 3 个维度的 N 个图像)

如果你有多个图像,你将需要以某种方式循环,就像这样(未经测试):

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])])