您的位置:首页 > 其它

深度学习框架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())
显示出来的照片比较难看,就不一一展示了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: