1 TFrecord输入数据格式
2 图像处理函数
2.1 图像的解码与编码
import matplotlib.pyplot as plt
import tensorflow as tf
image_raw_data = tf.gfile.GFile("pictures/123.jpg", 'rb').read()
with tf.Session() as sess:
# 对图像进行jpge的格式解码从而得到图像对应的三维矩阵
img_data = tf.image.decode_jpeg(image_raw_data)
print(img_data.eval())
# 使用pyplot工具可视化得到的图像
plt.imshow(img_data.eval())
plt.show()
# 将图像的三维矩阵重新按照jpeg格式编码
encoded_image = tf.image.encode_jpeg(img_data)
with tf.gfile.GFile("pictures/output.jpg", "wb") as f:
f.write(encoded_image.eval())
2.2 图像大小调整
import matplotlib.pyplot as plt
import tensorflow as tf
image_raw_data = tf.gfile.GFile("pictures/123.jpg", 'rb').read()
with tf.Session() as sess:
image_data = tf.image.decode_jpeg(image_raw_data)
# 将图像数据转化为实数类型,大多数api使用实数进行处理
image_data = tf.image.convert_image_dtype(image_data, dtype=tf.float32)
# 通过tf.image.resize_images 函数调整图像的大小
resized = tf.image.resize_images(image_data, [300, 300], method=0)
plt.imshow(resized.eval())
plt.show()
tf.image.resize_images函数中method参数取值:(以下超链接为维基百科)
0 双线性插值
1 最近邻居法
2 双三次插值法
3 面积插值法
使用 tf.image.resize_image_with_crop_or_pad 裁剪或者扩充图像(居中裁剪、周边填零)
croped = tf.image.resize_image_with_crop_or_pad(image_data, 600, 600)
padded = tf.image.resize_image_with_crop_or_pad(image_data, 1200, 1200)
也可以使用 tf.image.crop_to_bounding_box
函数和 tf.image.pad_to _bounding_box
函数来裁剪或者填充给定区域的图像。
2.3 图像翻转
import matplotlib.pyplot as plt
import tensorflow as tf
image_raw_data = tf.gfile.GFile("pictures/123.jpg", 'rb').read()
with tf.Session() as sess:
image_data = tf.image.decode_jpeg(image_raw_data)
# 将图像数据转化为实数类型,大多数api使用实数进行处理
image_data = tf.image.convert_image_dtype(image_data, dtype=tf.float32)
# 通过tf.image.resize_images 函数调整图像的大小
# resized = tf.image.resize_images(image_data, [300, 300], method=0)
# croped = tf.image.resize_image_with_crop_or_pad(image_data, 600, 600)
# padded = tf.image.resize_image_with_crop_or_pad(image_data, 1200, 1200)
flipped1 = tf.image.flip_up_down(image_data)
flipped2 = tf.image.flip_left_right(image_data)
transposed = tf.image.transpose_image(image_data)
plt.subplot(2, 2, 1)
plt.