深度学习框架Tensorflow学习与应用 图像数据处理之二
2018-03-30 14:02
731 查看
四:图像色彩调整
和图像翻转类似,调整图像的亮度、对比度、饱和度和色相在很多图像识别应用中都不会影响识别结果。所以在训练神经网络模型时,可以随机的调整训练图像的这些属性,从而使训练得到的模型尽可能地受到无关因素的影响。话不多说,上代码了。(一)调整亮度与调整对比度
import matplotlib.pyplot as plt import tensorflow as tf import numpy as np # 读取图像的原始数据 image_raw_data = tf.gfile.FastGFile("timg.jpg", 'rb').read() with tf.Session() as sess: image_data = tf.image.decode_jpeg(image_raw_data) #将图像的亮度-0.5 adjusted = tf.image.adjust_brightness(image_data,-0.5) adjusted = np.asarray(adjusted.eval(),dtype='uint8') encode_image = tf.image.encode_jpeg(adjusted) with tf.gfile.GFile("out./04.adjust_brightness_down.jpg",'wb') as f: f.write(encode_image.eval()) #将图像的亮度+0.5 adjusted = tf.image.adjust_brightness(image_data,0.5) adjusted = np.asarray(adjusted.eval(),dtype='uint8') encode_image = tf.image.encode_jpeg(adjusted) with tf.gfile.GFile("out./04.adjust_brightness_up.jpg",'wb') as f: f.write(encode_image.eval()) #在[-max_delta,max_delta)的范围内随机调整图片的亮度 adjusted = tf.image.random_brightness(image_data,max_delta=0.5) adjusted = np.asarray(adjusted.eval(),dtype='uint8') encode_image = tf.image.encode_jpeg(adjusted) with tf.gfile.GFile("out./04.random_brightness.jpg",'wb') as f: f.write(encode_image.eval()) #将图片对比度-5 adjusted = tf.image.adjust_contrast(image_data, 5) adjusted = np.asarray(adjusted.eval(), dtype='uint8') encode_image = tf.image.encode_jpeg(adjusted) with tf.gfile.GFile("out./04.adjust_contrast_down.jpg", 'wb') as f: f.write(encode_image.eval()) #将图片对比度+5 adjusted = tf.image.adjust_contrast(image_data, 5) adjusted = np.asarray(adjusted.eval(), dtype='uint8') encode_image = tf.image.encode_jpeg(adjusted) with tf.gfile.GFile("out./04.adjust_contrast_up.jpg", 'wb') as f: f.write(encode_image.eval()) #在[lower,upper]的范围随机调整图的对比度 adjusted = tf.image.random_contrast(image_data, lower=0.1,upper=0.5) adjusted = np.asarray(adjusted.eval(), dtype='uint8') encode_image = tf.image.encode_jpeg(adjusted) with tf.gfile.GFile("out./04.random_contrast.jpg", 'wb') as f: f.write(encode_image.eval())
(二)调整色相和饱和度
import matplotlib.pyplot as plt import tensorflow as tf import numpy as np # 读取图像的原始数据 image_raw_data = tf.gfile.FastGFile("timg.jpg", 'rb').read() with tf.Session() as sess: image_data = tf.image.decode_jpeg(image_raw_data) #将色相加0.1 adjusted = tf.image.adjust_hue(image_data,0.1) adjusted = np.asarray(adjusted.eval(), dtype='uint8') plt.imshow(adjusted) plt.show() #将表示一张图像的三维矩阵重新按照jpeg格式编码并存入文件中,打开这张像可以得到和原始图像一样的图像 encode_image = tf.image.encode_jpeg(adjusted) with tf.gfile.GFile("out./05_0.1.jpg",'wb') as f: f.write(encode_image.eval()) #将色相加0.3 adjusted = tf.image.adjust_hue(image_data,0.3) adjusted = np.asarray(adjusted.eval(), dtype='uint8') plt.imshow(adjusted) plt.show() #将表示一张图像的三维矩阵重新按照jpeg格式编码并存入文件中,打开这张像可以得到和原始图像一样的图像 encode_image = tf.image.encode_jpeg(adjusted) with tf.gfile.GFile("out./05_0.3.jpg",'wb') as f: f.write(encode_image.eval()) #将色相加0.1 adjusted = tf.image.adjust_hue(image_data,0.6) adjusted = np.asarray(adjusted.eval(), dtype='uint8') plt.imshow(adjusted) plt.show() #将表示一张图像的三维矩阵重新按照jpeg格式编码并存入文件中,打开这张像可以得到和原始图像一样的图像 encode_image = tf.image.encode_jpeg(adjusted) with tf.gfile.GFile("out./05_0.6.jpg",'wb') as f: f.write(encode_image.eval()) #将色相加0.9 adjusted = tf.image.adjust_hue(image_data,0.9) adjusted = np.asarray(adjusted.eval(), dtype='uint8') plt.imshow(adjusted) plt.show() #将表示一张图像的三维矩阵重新按照jpeg格式编码并存入文件中,打开这张像可以得到和原始图像一样的图像 encode_image = tf.image.encode_jpeg(adjusted) with tf.gfile.GFile("out./05_0.9.jpg",'wb') as f: f.write(encode_image.eval()) #将图像饱和度-5 adjusted = tf.image.adjust_saturation(image_data,-5) adjusted = np.asarray(adjusted.eval(), dtype='uint8') plt.imshow(adjusted) plt.show() #将表示一张图像的三维矩阵重新按照jpeg格式编码并存入文件中,打开这张像可以得到和原始图像一样的图像 encode_image = tf.image.encode_jpeg(adjusted) with tf.gfile.GFile("out./05_saturation_down.jpg",'wb') as f: f.write(encode_image.eval()) # 将图像饱和度+5 adjusted = tf.image.adjust_saturation(image_data, 5) adjusted = np.asarray(adjusted.eval(), dtype='uint8') plt.imshow(adjusted) plt.show() # 将表示一张图像的三维矩阵重新按照jpeg格式编码并存入文件中,打开这张像可以得到和原始图像一样的图像 encode_image = tf.image.encode_jpeg(adjusted) with tf.gfile.GFile("out./05_saturation_up.jpg", 'wb') as f: f.write(encode_image.eval()) # 将一张图像的三维矩阵中的数字均值变为0,方差为1 adjusted = tf.image.per_image_standardization(image_data) adjusted = np.asarray(adjusted.eval(), dtype='uint8') plt.imshow(adjusted) plt.show() # 将表示一张图像的三维矩阵重新按照jpeg格式编码并存入文件中,打开这张像可以得到和原始图像一样的图像 encode_image = tf.image.encode_jpeg(adjusted) with tf.gfile.GFile("out./05_per_image_standardization.jpg", 'wb') as f: f.write(encode_image.eval())显示出来的照片比较难看,就不一一展示了。
相关文章推荐
- 深度学习框架Tensorflow学习与应用 图像数据处理之一
- TensorFlow:实战Google深度学习框架(六)图像数据处理
- 深度学习框架TensorFlow学习与应用(三)——使用交叉熵作为代价函数
- 在TensorFlow中使用深度学习GANs处理图像
- 深度学习——残差神经网络ResNet在分别在Keras和tensorflow框架下的应用案例
- 深度学习框架TensorFlow学习与应用(一)——基本概念与简单示例
- 大数据下基于Tensorflow框架的深度学习示例教程
- 深度学习框架TensorFlow学习与应用(六)——卷积神经网络应用于MNIST数据集分类
- TensorFlow:实战Google深度学习框架(五)图像识别与卷积神经网络
- 深度学习——卷积神经网络在tensorflow框架下的应用案例
- 【BDTC 2015】深度学习分论坛:DL的图像识别、语音识别应用进展及MxNet开源框架设计
- TensorFlow学习系列之二:深度学习框架比较
- python-框架-网页爬虫-文本处理-科学计算-可视化-机器学习-数据挖掘-深度学习
- 深度学习框架Tensorflow学习与应用 第1课
- 【深度学习】TensorFlow图像处理
- 【深度学习框架Caffe学习与应用】第三课 将图片数据转化为LMDB数据``
- 深度学习在医学图像处理中的应用
- 大数据下基于Tensorflow框架的深度学习示例教程
- 寻找下一款Prisma APP:深度学习在图像处理中的应用探讨
- 深度学习框架Tensorflow学习与应用 第2课