这里写自定义目录标题
关于读取图片
通过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一下。