项目已上传至 GitHub —— img-pre
1. 目录结构
images/ 文件夹下存放将被用于处理的图像,img_all.py 示范了 TensorFlow 中图像处理函数的使用方法,img_pre.py 给出了一个对图像进行预处理的程序示例:
img-pre/
images/
1.jpg
img_all.py
img_pre.py
2. 图像处理函数
2.1 编码处理
在使用图像之前需要先对图像进行解码,将图像转为像素矩阵,tensorflow.image 提供了图像解码函数,返回的矩阵数据类型是 uint8:
- decode_jpeg:解码 JPEG 格式图像
- decode_and_crop_jpeg:解码并裁剪 JPEG 格式图像
- decode_png:解码 PNG 格式图像
- decode_gif:解码 GIF 格式图像
- decode_bmp:解码 BMP 格式图像
- decode_image:自动检测图像格式进行解码,注意 GIF 格式图像返回一个四维矩阵
以下代码示范对 JPEG 格式图像的解码处理:
import tensorflow as tf
import matplotlib.pyplot as plt
# 读取图像的原始数据
image_raw_data = tf.gfile.FastGFile('images/1.jpg', 'rb').read()
# 使用pyplot显示图像
def show(img_data):
plt.imshow(img_data.eval())
plt.show()
with tf.Session() as sess:
# 将原始数据解码成多维矩阵
img_data = tf.image.decode_jpeg(image_raw_data)
print(img_data.eval())
show(img_data)
# 将图像的矩阵编码成图像并存入文件
encoded_image = tf.image.encode_jpeg(img_data)
with tf.gfile.GFile('images/output.jpg', 'wb') as f:
f.write(encoded_image.eval())
# 将图像数据的类型转为实数类型,便于对图像进行处理
img_data = tf.image.convert_image_dtype(img_data, dtype=tf.float32)
2.2 大小调整
tf.image.resize_images 函数中有四种调整图像的方法,结果会有细微差别:
method | 图像大小调整算法 |
---|---|
0 | 双线性插值法(Bilinear interpolation) |
1 | 最近邻居法(Nearest neighbor interpolation) |
2 | 双三次插值法(Bicubic interpolation) |
3 | 面积插值法(Area interpolation) |
以下代码都是在图像编码处理代码的基础下运行,省去了加载原始图像,定义会话等过程:
# 用resize_images调整图像大小
# 第一个参数为原始图像
# 第二个参数为调整后的图像大小[new_height,new_width],跟旧版本分为两个参数不一样
# method参数给出了调整图像大小的算法
resized = tf.image.resize_images(img_data, [300, 300], method=0)
print(resized.get_shape()) # 图像深度没有显式指定则为问号
show(resized)
还可以通过 resize_image_with_crop_or_pad 函数对图像进行裁剪或填充:
# 用resize_image_with_crop_or_pad调整图像大小
# 第一个参数为原始图像
# 第二个和第三个参数是调整后的图像大小,大于原图则填充,小于则裁剪居中部分
croped = tf.image.resize_image_with_crop_or_pad(img_data, 200, 200)
show(croped)
padded =<