tensorflow 旋转图片_tensorflow图片预处理,随机亮度,旋转,剪切,翻转。

本文介绍了如何使用TensorFlow进行图像预处理,包括随机旋转、随机左右翻转、随机改变亮度和随机裁剪图片,以增加训练数据的多样性,提高模型的性能。代码示例详细展示了这些预处理步骤的实现。
摘要由CSDN通过智能技术生成

图像预处理是一个非常简单,通过提高训练数据的多样性,进而对训练模型的召回率,适应性有着非常大的提升作用。

另外在训练时,需要更多的训练次数,比如说我对每张图片进行了一次旋转,那么训练次数就要提高一倍。也就是说训练集多样性增加,同时训练次数也要增加。

代码:

import tensorflow as tf

from scipy import misc

import numpy as np

#随机旋转图片

def random_rotate_image(image_file, num):

with tf.Graph().as_default():

tf.set_random_seed(666)

file_contents = tf.read_file(image_file)

image = tf.image.decode_image(file_contents, channels=3)

image_rotate_en_list = []

def random_rotate_image_func(image):

#旋转角度范围

angle = np.random.uniform(low=-30.0, high=30.0)

return misc.imrotate(image, angle, 'bicubic')

for i in range(num):

image_rotate = tf.py_func(random_rotate_image_func, [image], tf.uint8)

image_rotate_en_list.append(tf.image.encode_png(image_rotate))

with tf.Session() as sess:

sess.run(tf.global_variables_initializer())

sess.run(tf.local_variables_initializer())

results = sess.run(image_rotate_en_list)

for idx,re in enumerate(results):

with open('data/'+str(idx)+'.png','wb') as f:

f.write(re)

#随机左右翻转图片

def random_flip_image(image_file, num):

with tf.Graph().as_default():

tf.set_random_seed(666)

file_contents = tf.read_file(image_file)

image = tf.image.decode_image(file_contents, channels=3)

image_flip_en_list = []

for i in range(num):

image_flip = tf.image.random_flip_left_right(image)

image_flip_en_list.append(tf.image.encode_png(image_flip))

with tf.Session() as sess:

sess.run(tf.global_variables_initializer())

sess.run(tf.local_variables_initializer())

results = sess.run(image_flip_en_list)

for idx,re in enumerate(results):

with open('data/'+str(idx)+'.png','wb') as f:

f.write(re)

#随机变化图片亮度

def random_brightness_image(image_file, num):

with tf.Graph().as_default():

tf.set_random_seed(666)

file_contents = tf.read_file(image_file)

image = tf.image.decode_image(file_contents, channels=3)

image_bright_en_list = []

for i in range(num):

image_bright = tf.image.random_brightness(image, max_delta=0.3)

image_bright_en_list.append(tf.image.encode_png(image_bright))

with tf.Session() as sess:

sess.run(tf.global_variables_initializer())

sess.run(tf.local_variables_initializer())

results = sess.run(image_bright_en_list)

for idx,re in enumerate(results):

with open('data/'+str(idx)+'.png','wb') as f:

f.write(re)

#随机裁剪图片

def random_crop_image(image_file, num):

with tf.Graph().as_default():

tf.set_random_seed(666)

file_contents = tf.read_file(image_file)

image = tf.image.decode_image(file_contents, channels=3)

image_crop_en_list = []

for i in range(num):

#裁剪后图片分辨率保持160x160,3通道

image_crop = tf.random_crop(image, [160, 160, 3])

image_crop_en_list.append(tf.image.encode_png(image_crop))

with tf.Session() as sess:

sess.run(tf.global_variables_initializer())

sess.run(tf.local_variables_initializer())

results = sess.run(image_crop_en_list)

for idx,re in enumerate(results):

with open('data/'+str(idx)+'.png','wb') as f:

f.write(re)

if __name__ == '__main__':

#处理图片,进行20次随机处理,并将处理后的图片保存到输入图片相同的路径下

random_brightness_image('data/test.png', 20)

运行效果:

随机裁剪

随机亮度

随机旋转

随机翻转

更多图像处理操作,请查看tensorflow官方文档http://www.tensorfly.cn/tfdoc/api_docs/python/image.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值