這裏寫自定義目錄標題
關於讀取圖片
通過tf.read_file()函數讀取圖片
必須要通過tf.image.decode_image()函數或者tf.image.decode_* 函數解碼(*包括:jpeg, png, bmp, gif), 其中
(1)當用tf.image.decode_image()函數解碼時,tensor是不帶有shape的,dtype類型是uint8,後面用tf.image.resize_images()對image做resize操作時會報錯 ValueError: ‘images’ contains no shape. 解決方法是在resize之前對image做一個設定shape的操作: image.set_shape([None, None, 3])或者對image做一些帶有指定shape類的操作如:tf.random_crop(image, [112,112,3])
(2) 當用tf.image.decode_*函數解碼時,tensor是帶有shape的,dtype類型是uint8,但缺點是這類函數只能解碼對應格式的圖片。
from __future__ import division, print_function, absolute_import
import cv2
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
JPG_image = './cat.1.jpg'
PNG_image = './0_0.png'
image_content = tf.read_file(PNG_image)
image = tf.image.decode_image(image_content, channels=3)
print(image) # Tensor("decode_image/cond_jpeg/Merge:0", dtype=uint8)
#image = tf.random_crop(image, [112,112,3])
#print(image) # Tensor("random_crop:0", shape=(112, 112, 3), dtype=uint8)
image.set_shape([None, None, 3])
print(image) # Tensor("PyFunc:0", shape=(?, ?, 3), dtype=uint8)
image = tf.image.resize_images(image, [112, 112])
print(image) # Tensor("Squeeze:0", shape=(112, 112, 3), dtype=uint8)
#image = tf.image.per_image_standardization(image)
#print(image) # Tensor("div:0", shape=(112, 112, 3), dtype=float32)
with tf.Session() as sess:
img_rgb = sess.run(image) # RGB通道
print(img_rgb.shape) # (112, 112, 3)
img_rgb = np.asarray(img_rgb[:, :, :], dtype='uint8')
img_bgr = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2BGR)
#cv2.imshow('cv2_cat', img_bgr)
#cv2.waitKey()
plt.imshow(img_rgb)
plt.show()
通過tf.py_func利用opencv讀取圖片
利用opencv讀圖片是讀出來是BGR通道的,與tensorflow的自帶讀圖片的接口讀出來的RGB通道是相反的,需要利用cv2.cvtColor函數轉換通道。它讀出來的tensor也是不帶有shape的,dtype類型是uint8。後續操作同tf.image.decode_image()函數。
from __future__ import division, print_function, absolute_import
import cv2
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
JPG_image = './cat.1.jpg'
PNG_image = './0_0.png'
def read_image_from_cv2(filename):
image = cv2.imread(filename, cv2.IMREAD_COLOR)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
return image
image = tf.py_func(read_image_from_cv2, [PNG_image], tf.uint8)
print(image) # Tensor("PyFunc:0", dtype=uint8)
#image = tf.random_crop(image, [112,112,3])
#print(image) # Tensor("random_crop:0", shape=(112, 112, 3), dtype=uint8)
image.set_shape([None, None, 3])
print(image) # Tensor("PyFunc:0", shape=(?, ?, 3), dtype=uint8)
image = tf.image.resize_images(image, [112, 112])
print(image) # Tensor("Squeeze:0", shape=(112, 112, 3), dtype=uint8)
#image = tf.image.per_image_standardization(image)
#print(image) # Tensor("div:0", shape=(112, 112, 3), dtype=float32)
with tf.Session() as sess:
img_rgb = sess.run(image) # RGB通道
print(img_rgb.shape)
img_rgb = np.asarray(img_rgb[:, :, :], dtype='uint8')
img_bgr = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2BGR)
#cv2.imshow('cv2_cat', img_bgr)
#cv2.waitKey()
plt.imshow(img_rgb)
plt.show()
關於顯示圖片
顯示圖片都需要把dtype的類型轉換爲uint8型,如果是float32直接顯示會失真。
如:
利用opencv顯示圖片
opencv顯示圖片也是需要圖片的通道是BGR型的,不然展示的圖片會偏藍,不正常。
利用matplotlib.pyplot顯示圖片
pyplot顯示圖片是需要圖片通道是RGB的,
pyplot不能繪製像(112, 112, 1)這樣數據格式的灰度圖,它能繪製的灰度圖格式必須是(height,width),但是通過tensorflow接口讀取會得到(height,width,channels),普通的彩色圖就是(height,width,3),灰度圖就是(height,width,1),爲了讓pyplot繪製出來我們需要reshape一下。