簡體   English   中英

調用 tf.image.rgb_to_grayscale 后無法顯示圖像

[英]Can't display an image after calling tf.image.rgb_to_grayscale

我正在研究人臉識別應用程序並准備好讀取 jpg 圖像然后訓練我的神經網絡的數據集。 為了提高准確性,我決定將我的圖像轉換為灰度(內部映射函數)。 這是我將圖像轉換為張量的方法:

from PIL import Image

image_data = tf.io.decode_jpeg(
  image_bytes_string,
  channels=3
)

image_data = tf.reshape(image_data , (277, 370, 3))

label = parsed['label']
label = tf.reshape(label, (1,))

return image_data, label

我如何顯示圖像的代碼:

image_data = dataset_item[0].numpy()
img = Image.fromarray(image_data)
img

更改代碼以轉換為灰度后,顯示圖像的代碼顯示此錯誤:

KeyError: ((1, 1, 1), '|u1')
During handling of the above exception, another exception occurred:
Cannot handle this data type

灰度代碼:

image_data = tf.io.decode_jpeg(
  image_bytes_string,
  channels=3
)

grayscale = tf.image.rgb_to_grayscale(
  image_data
)

image_data = tf.reshape(grayscale, (277, 370, 1))

label = parsed['label']
label = tf.reshape(label, (1,))

return image_data, label

我如何將圖像轉換為灰度或 PIL 庫有問題嗎?

UPD:這是帶有示例的 colab: https ://colab.research.google.com/drive/1v72_C5i8HZzSLEy_p6d45EWq1byoF3fe

我真的陷入了同樣的錯誤,但以下代碼可能會幫助您顯示灰度圖像。

在下面的代碼中,您可以看到指標 [...,0] 被稱為Ellipsis_object

grey_img = tf.image.rgb_to_grayscale(jpeg_image)
_ = plt.imshow(grey_img[...,0], cmap='gray')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM