tensorflow图像处理
通过随机调整图像亮度/对比度/饱和度/色相,可以衍生出更多的训练样本,减小识别物体不同的大小/方位/色彩等无关因素对图像识别模型的影响.且调整顺序不同可以得到不同的处理结果.
实例:
#!/usr/bin/python
# coding:utf-8
# 图像预处理
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
# 调整亮度.对比度.饱和度.色相的顺序可以得到不同的结果
# 预处理时随机选择的一种,降低无关因素对模型的影响
def distort_color(image, color_ordering=0):
if color_ordering == 0:
image = tf.image.random_brightness(image, max_delta=32./255)
image = tf.image.random_contrast(image, lower=0.5, upper=1.5)
image = tf.image.random_hue(image, max_delta=0.2)
image = tf.image.random_saturation(image, lower=0.5, upper=1.5)
elif color_ordering ==1:
image = tf.image.random_saturation(image, lower=0.5, upper=1.5)
image = tf.image.random_brightness(image, max_delta=32./255)
image = tf.image.random_contrast(image, lower=0.5, upper=1.5)
image = tf.image.random_hue(image, max_delta=0.2)
elif color_ordering ==2:
image = tf.image.random_brightness(image, max_delta=32./255)
image = tf.image.random_saturation(image, lower=0.5, upper=1.5)
image = tf.image.random_contrast(image, lower=0.5, upper=1.5)
image = tf.image.random_hue(image, max_delta=0.2)
elif color_ordering ==3:
image = tf.image.random_brightness(image, max_delta=32./255)
image = tf.image.random_contrast(image, lower=0.5, upper=1.5)
image = tf.image.random_saturation(image, lower=0.5, upper=1.5)
image = tf.image.random_hue(image, max_delta=0.2)
elif color_ordering ==4:
image = tf.image.random_brightness(image, max_delta=32./255)
image = tf.image.random_contrast(image, lower=0.5, upper=1.5)
image = tf.image.random_saturation(image, lower=0.5, upper=1.5)
image = tf.image.random_hue(image, max_delta=0.2)
elif color_ordering ==5:
image = tf.image.random_contrast(image, lower=0.5, upper=1.5)
image = tf.image.random_brightness(image, max_delta=32./255)
image = tf.image.random_saturation(image, lower=0.5, upper=1.5)
image = tf.image.random_hue(image, max_delta=0.2)
return tf.clip_by_value(image, 0.0, 1.0)
# 给定解码后的图像.目标图像的尺寸以及图像上的标注框
def preprocess(image, height, width, bbox):
# 若没有提供标注框则默认为关注区域为整个图像
if bbox is None:
bbox = tf.constant([0.0, 0.0, 1.0, 1.0], dtype=tf.float32, shape=[1, 1, 4])
# 转换图像数据类型
if image.dtype != tf.float32:
image = tf.image.convert_image_dtype(image, dtype=tf.float32)
# 随机截取图像减小识别物体大小对模型的影响
bbox_begin, bbox_size, _ = tf.image.sample_distorted_bounding_box(tf.shape(image), bounding_boxes=bbox)
distorted_image = tf.slice(image, bbox_begin, bbox_size)
# 随机调整图像的大小
distorted_image = tf.image.resize_images(distorted_image, (height, width), method=np.random.randint(4))
# 随机左右翻转图像
distorted_image = tf.image.random_flip_left_right(distorted_image)
# 使用一种随机的顺序调整图像色彩
distorted_image = distort_color(distorted_image, np.random.randint(2))
return distorted_image
# 获取图像
image_raw_data = tf.gfile.FastGFile('daibola.jpg', 'r').read()
with tf.Session() as sess:
img_data = tf.image.decode_jpeg(image_raw_data)
boxes = tf.constant([[[0.1, 0.32, 0.8, 0.7]]])
# 获得9种不同的图像并显示结果
for i in range(9):
# 将图像大小调整为200*200
result = preprocess(img_data, 200, 200, boxes)
plt.subplot(331+i), plt.imshow(result.eval()), plt.title(str(i+1))
plt.show()
输出:
原图: