rgb_to_grayscale¶
- torchvision.transforms.functional.rgb_to_grayscale(img: Tensor, num_output_channels: int = 1) Tensor [source]¶
将 RGB 图像转换为灰度版本。如果图像是 torch Tensor,则期望其形状为 […, 3, H, W],其中 … 表示任意数量的前导维度
注意
请注意,此方法仅支持 RGB 图像作为输入。对于其他颜色空间的输入,请考虑使用
to_grayscale()
配合 PIL Image 使用。- 参数:
img (PIL Image 或 Tensor) – 要转换为灰度的 RGB 图像。
num_output_channels (int) – 输出图像的通道数。值可以是 1 或 3。默认为 1。
- 返回:
图像的灰度版本。
如果 num_output_channels = 1:返回的图像是单通道
如果 num_output_channels = 3:返回的图像是 3 通道的,其中 r = g = b
- 返回类型:
PIL 图像或张量