略图rgb2gray降低了一维

2024-06-17 12:45:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将多个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)。在

我遗漏了什么?在


Tags: ofinfrom图像animgisrgb
1条回答
网友
1楼 · 发布于 2024-06-17 12:45:03

这个function假设输入是一个dims3或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])])

相关问题 更多 >