rgb_to_grayscale¶
- torchvision.transforms.functional.rgb_to_grayscale(img: Tensor, num_output_channels: int = 1) Tensor [源代码]¶
将 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 Image 或 Tensor