tensorflow里面給出了一個函數用來讀取圖像,不過得到的結果是最原始的圖像,是咩有經過解碼的圖像,這個函數為tf.gfile.FastGFile(‘path', ‘r').read()。如果要顯示讀入的圖像,那就需要經過解碼過程,tensorflow里面提供解碼的函數有兩個,tf.image.decode_jepg和tf.image.decode_png分別用于解碼jpg格式和png格式的圖像進行解碼,得到圖像的像素值,這個像素值可以用于顯示圖像。如果乜有解碼,讀取的圖像是一個字符串,沒法顯示。
例如:
import matplotlib.pyplot as plt; import tensorflow as tf; image_raw_data_jpg = tf.gfile.FastGFile('11.jpg', 'r').read() image_raw_data_png = tf.gfile.FastGFile('1.png', 'r').read() with tf.Session() as sess: img_data_jpg = tf.image.decode_jpeg(image_raw_data_jpg) #圖像解碼 img_data_jpg = tf.image.convert_image_dtype(img_data_jpg, dtype=tf.uint8) #改變圖像數據的類型 img_data_png = tf.image.decode_png(image_raw_data_png) img_data_png = tf.image.convert_image_dtype(img_data_png, dtype=tf.uint8) plt.figure(1) #圖像顯示 plt.imshow(img_data_jpg.eval()) plt.figure(2) plt.imshow(img_data_png.eval()) plt.show() 結果:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林站長站。
新聞熱點
疑難解答