tensorflow图像裁剪进行数据增强操作图像裁剪进行数据增强操作
主要介绍了tensorflow图像裁剪进行数据增强操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编
过来看看吧
我就废话不多说了,大家还是直接看代码吧~
#!/usr/bin/env python
# encoding: utf-8
'''
@author: lele Ye
@contact: 1750112338@qq.com
@software: pycharm 2018.2
@file: 13mnist.py
@time: 2018/12/17 10:23
@desc:
'''
import tensorflow as tf
import scipy.misc
import matplotlib.pyplot as plt
import random
# 读取图像可任意大小
filenames = ['./tianchi.jpg']
# 创建文件读取队列
filename_queue = tf.train.string_input_producer(filenames)
# 一个阅读器,读取整个文件,返回文件名称key,以及文件中所有的内容value
reader = tf.WholeFileReader()
# Returns the next record (key, value) pair produced by a reader
key, value = reader.read(filename_queue)
images = tf.image.decode_jpeg(value) # tf.image.decode_png(value)
target_width = target_height = 224
# 裁切图片
with tf.Session() as sess:
# Coordinator的使用,用于多线程的协调
coord = tf.train.Coordinator()
# 启动所有graph收集到的队列运行器(queuerunners)
threads = tf.train.start_queue_runners(coord=coord)
height,width,channels = sess.run(tf.shape(images))
offset_height = random.randint(0,height-target_height)
offset_width = random.randint(0,width-target_width)
reshapeimg = tf.image.crop_to_bounding_box(images, offset_height=offset_height, offset_width=offset_width,
target_height=target_height,target_width=target_width)
print(type(reshapeimg)) # <class 'tensorflow.python.framework.ops.Tensor'>
reimg1 = reshapeimg.eval() # reimg1的类型是<class 'numpy.ndarray'>
scipy.misc.imsave('./crop.jpg', reimg1)
plt.imshow(reimg1)
plt.axis("off")
plt.show()
# 请求线程结束
coord.request_stop()
# 等待线程终止
coord.join(threads)
原始图像480x320x3:
裁剪后224x224x3:
评论0