Tensorflow图像预处理(3)色彩调整
#图像色彩调整
#调整图片的亮度,对比度,饱和度,色相等
import matplotlib.pyplot as plt
image_raw_data=tf.gfile.GFile("pic/ma.jpg","rb").read()
with tf.Session() as sess:
img_data=tf.image.decode_jpeg(image_raw_data)
img_data=tf.image.convert_image_dtype(img_data,dtype=tf.float32)
#亮度调整
bri_adjust_down=tf.image.adjust_brightness(img_data,-0.5) #降低亮度
bri_adjust_up=tf.image.adjust_brightness(img_data,0.5) #提高亮度
bri_adjust_random=tf.image.random_brightness(img_data,0.9) #在某个范围内随机调整亮度
#对比度调整
contrast_adj_down=tf.image.adjust_contrast(img_data,0.5) #减少对比度为原先0.5倍
contrast_adj_up=tf.image.adjust_contrast(img_data,5) #增加对比度到原来5倍
contrast_adj_ran=tf.image.random_contrast(img_data,lower=0.1,upper=3) #一定范围内随机调整对比度
#色相
adjust_hue_01=tf.image.adjust_hue(img_data,0.1) #色相相加0.1
adjust_hue_07 = tf.image.adjust_hue(img_data, 0.7) #色相相加0.7
adjust_hue_ran = tf.image.random_hue(img_data, 0.5) #色相随机相加 这里加的值在0-0.5之间
adjust_hue_07=tf.clip_by_value(adjust_hue_07,0,1.0) #截断操作保证像素值在0-1之间
#饱和度
sat_adjust_down=tf.image.adjust_saturation(img_data,-5) #降低饱和度
sat_adjust_up = tf.image.adjust_saturation(img_data, 5) #增加饱和度
sat_adjust_ran = tf.image.random_saturation(img_data, lower=0,upper=3) #随机更改饱和度
#图像标准化
std_adjust=tf.image.per_image_standardization(img_data) #操作后,图像中的数字均值变为0,方差变为1
fig=plt.figure(figsize=(10,4))
ax1 = fig.add_subplot(1,3, 1)
ax2 = fig.add_subplot(1, 3, 2)
ax3 = fig.add_subplot(1, 3, 3)
ax1.imshow(std_adjust.eval())
ax2.imshow(sat_adjust_up.eval())
ax3.imshow(sat_adjust_ran.eval())
plt.show()