一.方法
图像读取,剪裁、反转、改变颜色,涉及的函数有:
读取:
tf.gfile.FastGFile(path,decodestyle)
剪裁:
tf.image.sample_distorted_bounding_box(image_size,bounding_boxes)
tf.slice(image, bbox_begin, bbox_size)
tf.image.resize_images(image, [height, width], method)
反转:
tf.image.random_flip_left_right(image)
tf.image.random_flip_up_down(image)
颜色改变:
tf.image.random_brightness(image, max_delta) # 在(-max_delta,max_delta)随机调整
tf.image.random_saturation(image, lower, upper) # 在( lower, upper)随机调整
tf.image.random_hue(image, max_delta)
tf.image.random_contrast(mage, lower, upper)
截断:
tf.clip_by_value(image,0.0, 1.0) # 保证图像调整后的像素值在0.0-1.0之间
二. 部分函数讲解
1. tf.gfile.FastGFile(path,decodestyle)
函数功能:实现对图片的读取。
函数参数:(1)path:图片所在路径 (2)decodestyle:图片的解码方式。(‘r’:UTF-8编码; ‘rb’:非UTF-8编码)
2. tf.image.sample_distorted_bounding_box()
此函数为图像生成单个随机变形的边界框。函数输出的是可用于裁剪原始图像的单个边框。返回值为3个张量:begin,size和 bboxes。前2个张量用于 tf.slice 剪裁图像。后者可以用于 tf.image.draw_bounding_boxes 函数来画出边界框。
3. tf.clip_by_value()
tf.clip_by_value(A, min, max):输入一个张量A,把A中的每一个元素的值都压缩在min和max之间。小于min的让它等于min,大于max的元素的值等于max。
三. 具体代码实现
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author:Dr.Shang
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_saturation(image, lower=0.5, upper=1.5)
image = tf.image.random_hue(image, max_delta=0.2)
image = tf.image.random_contrast(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_hue(image, max_delta=0.2)
image = tf.image.random_contrast(image, lower=0.5, upper=1.5)
elif color_ordering == 2:
image = tf.image.random_hue(image, max_delta=0.2)
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)
elif color_ordering == 3:
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)
image = tf.image.random_brightness(image, max_delta=32. / 255)
elif color_ordering == 4:
image = tf.image.random_brightness(image, max_delta=32. / 255)
image = tf.image.random_hue(image, max_delta=0.2)
image = tf.image.random_saturation(image, lower=0.5, upper=1.5)
image = tf.image.random_contrast(image, lower=0.5, upper=1.5)
elif color_ordering == 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)
image = tf.image.random_contrast(image, lower=0.5, upper=1.5)
elif color_ordering == 6:
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)
return tf.clip_by_value(image, 0.0, 1.0)
def prerocess_for_train(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)
# 随机剪裁图片,减小需要关注的物体大小对图像识别算法的影响
"""
此函数为图像生成单个随机变形的边界框。函数输出的是可用于裁剪原始图像的单个边框。返回值为3个张量:begin,size和 bboxes。
前2个张量用于 tf.slice 剪裁图像。后者可以用于 tf.image.draw_bounding_boxes 函数来画出边界框。
"""
bbox_begin, bbox_size, _ = tf.image.sample_distorted_bounding_box(tf.shape(image), bounding_boxes=bbox)
distort_image = tf.slice(image, bbox_begin, bbox_size)
# 将随机剪裁的图片调整为神经网络输入层的大小。大小调整的算法是随机选择的。
distort_image = tf.image.resize_images(distort_image, [height, width], method=np.random.randint(4))
# 随机左右或上下翻转图片
flag = np.random.randint(2)
if flag == 0:
distort_image = tf.image.random_flip_left_right(distort_image)
else:
distort_image = tf.image.random_flip_up_down(distort_image)
# 随机使用一种顺序调整图像色彩
distort_image = distort_color(distort_image, np.random.randint(7))
return distort_image
image_raw_data = tf.gfile.FastGFile("55.jpg", "rb").read()
with tf.Session() as sess:
img_data = tf.image.decode_jpeg(image_raw_data)
boxes = tf.constant([[[0.05, 0.05, 0.9, 0.7], [0.35, 0.47, 0.5, 0.56]]])
for i in range(6):
# 将图像尺寸调整为299*299
result = prerocess_for_train(img_data, 299, 299, boxes)
plt.imshow(result.eval())
plt.show()
四.原图+效果图
原图:
效果图: