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 影像或張量