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