图像处理
1.图像数字化3要素:[长度,宽度,通道数]
如何用张量表示[height,winth,hwo]
2. 图像的基本操作:
- 所有图片统一
- 所有图片转换成指定大小
- 缩小图片数据量,防止开销
tf.image.resize_images(images,size)
import tensorflow as tf
import os
def picread(bag):
"""
读取图片并转换成张量
:param bag: 文件路径+ 名字列表
:return:每张图片的张量
"""
# 1.构造文件队列
file_queue = tf.train.string_input_producer(filelist)
# 2.构造阅读器去读取图片的内容
reader = tf.WholeFileReader()
key, value = reader.read(file_queue) # value 是一张图片
# 3.对图片解码
image = tf.image.decode_jpeg(value)
# 5.处理图片的大小
image_reize = tf.image.resize_images(image, [200, 200])
# 5.1 处理图片的大小,因为是彩色图片,3通道所有先设置好
image_reize.set_shape([200,200,3])
# 6. 批处理
image_batch = tf.train.batch([image_reize],batch_size=20, num_threads=1, capacity=20)
return image_batch
if __name__ == '__main__':
# 1.找到文件放入列表中
file_name = os.listdir('./data/anglababy')
filelist = [os.path.join('./data/anglababy',file) for file in file_name]
image_batch = picread(filelist)
# 开始绘画
with tf.Session() as sess:
# 定义线程协调器
coord = tf.train.Coordinator()
# 开启读文件的线程
th = tf.train.start_queue_runners(sess, coord=coord)
print(sess.run([image_batch]))
# 回收子线程
coord.request_stop()
coord.join(th)