快捷方式

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 ImageTensor) – 要轉換為灰度的 RGB 影像。

  • num_output_channels (int) – 輸出影像的通道數。值可為 1 或 3。預設值為 1。

返回:

影像的灰度版本。

  • 如果 num_output_channels = 1:返回影像為單通道

  • 如果 num_output_channels = 3:返回影像為 3 通道,其中 r = g = b

返回型別:

PIL Image 或 Tensor

文件

訪問 PyTorch 的全面開發者文件

檢視文件

教程

獲取面向初學者和高階開發者的深度教程

檢視教程

資源

查詢開發資源並獲得問題解答

檢視資源