概述
好久之前的代码,有些乱。
import tensorflow as tf
import matplotlib.pyplot as plt
from PIL import Image
from numpy import uint8
with tf.Session() as sess:
# 读取原图像并显示
original_image = Image.open("C:/Users/ccc/test/3.jpg")
plt.title('original_image')
plt.imshow(original_image)
plt.show()
# 将图像缩小至224X224
reduced_image = tf.image.resize_images(original_image, [10, 10])
# 将图像扩大至1024X768
expanded_image = tf.image.resize_images(original_image, [1024, 768])
sess = tf.Session()
sess.run(tf.global_variables_initializer())
# 将tensor转换为numpy数组
reduced_image = sess.run(reduced_image)
# 将numpy数组转换为图像
reduced_image=Image.fromarray(uint8(reduced_image))
# 显示图像
plt.title('reduced_image')
plt.imshow(reduced_image)
plt.show()
# 保存图像
#reduced_image.save('D:/date_1/haha/0_Label_0-reduce.jpg')
expanded_image = sess.run(expanded_image)
expanded_image=Image.fromarray(uint8(expanded_image))
plt.title('expanded_image')
plt.imshow(expanded_image)
plt.show()
#expanded_image.save('D:/date_1/haha/0_Label_0-expand.jpg')
# 随机裁剪图像64x64
crop_image = tf.random_crop(original_image,[20,20,3])
sess = tf.Session()
sess.run(tf.global_variables_initializer())
# 将tensor转换为numpy数组
crop_image = sess.run(crop_image)
# 将numpy数组转换为图像
crop_image=Image.fromarray(uint8(crop_image))
# 显示图像
plt.title('crop_image')
plt.imshow(crop_image)
plt.show()
# 保存图像
#crop_image.save('D:/date_1/haha/0_Label_0-cai.jpg')
#水平上下翻转
# 对图像进行左右翻转
horizontal_image = tf.image.flip_left_right(original_image)
# 对图像进行上下翻转
vertical_image = tf.image.flip_up_down(original_image)
horizontal_image = sess.run(horizontal_image)
horizontal_image=Image.fromarray(uint8(horizontal_image))
plt.title('horizontal_image')
plt.imshow(horizontal_image)
plt.show()
#horizontal_image.save('D:/date_1/haha/0_Label_0-shui.jpg')
vertical_image = sess.run(vertical_image)
vertical_image=Image.fromarray(uint8(vertical_image))
plt.title('vertical_image')
plt.imshow(vertical_image)
plt.show()
#vertical_image.save('D:/date_1/haha/0_Label_0-fan.jpg')
#颜色变换
# 通过随机因子调整图像的亮度
random_brightness_image = tf.image.random_brightness(original_image, 0.5)
# 通过随机因子调整图像的对比度
random_contrast_image = tf.image.random_contrast(original_image, 0.1, 0.5 )
# 通过随机因子调整RGB图像的色调
random_hue_image = tf.image.random_hue(original_image, 0.5)
# 通过随机因子调整RGB图像的饱和度
random_saturation_image = tf.image.random_saturation(original_image, 0.3, 0.5)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
random_brightness_image = sess.run(random_brightness_image)
random_brightness_image=Image.fromarray(uint8(random_brightness_image))
plt.title('random_brightness_image')
plt.imshow(random_brightness_image)
plt.show()
#random_brightness_image.save('D:/date_1/haha/0_Label_0-brighness.jpg')
random_contrast_image = sess.run(random_contrast_image)
random_contrast_image=Image.fromarray(uint8(random_contrast_image))
plt.title('random_contrast_image')
plt.imshow(random_contrast_image)
plt.show()
#random_contrast_image.save('D:/date_1/haha/0_Label_0-contrast.jpg')
random_hue_image = sess.run(random_hue_image)
random_hue_image=Image.fromarray(uint8(random_hue_image))
plt.title('random_hue_image')
plt.imshow(random_hue_image)
plt.show()
#random_hue_image.save('D:/date_1/haha/0_Label_0-hue.jpg')
random_saturation_image = sess.run(random_saturation_image)
random_saturation_image=Image.fromarray(uint8(random_saturation_image))
plt.title('random_saturation_image')
plt.imshow(random_saturation_image)
plt.show()
#random_saturation_image.save('D:/date_1/haha/0_Label_0-saturation.jpg')
最后
以上就是顺利老鼠为你收集整理的Tensorflow的几种数据增强的方式:缩放、扩大、翻转、调整亮度、调整对比度等的全部内容,希望文章能够帮你解决Tensorflow的几种数据增强的方式:缩放、扩大、翻转、调整亮度、调整对比度等所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复