Tensorflow—利用Tensorflow对图像进行处理
读取图像数据
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
def show_image(image):
shape = np.shape(image)
if len(shape) == 3 and shape[2] == 1:
plt.imshow(image[:, :, 0], cmap='gray')
plt.show()
elif len(shape) == 3:
plt.imshow(image)
plt.show()
sess = tf.InteractiveSession()
image_path = "./xiaoren.png"
file_contents = tf.read_file(image_path)
print(file_contents.eval())
"""
def decode_image(contents, channels=None, name=None):
对图像数据做一个转换,对图像数据的类型没有要求;转换的最终结果为:[height, width, num_channels]; 如果是gif的图像返回的是: [num_frames, height, width, num_channels]
contents: 给定具体的数据对象
channels: 给定读取数据形成的image tensor是几个通道的,可选值为:0 1 3 4,一般建议选择:0 1 3
0表示使用默认通道,默认值为0
1表示使用灰度图像通道
3表示使用RGB图像通道
4表示RGBA图像通道
"""
image_tensor = tf.image.decode_png(contents=file_contents, channels=0, dtype=tf.uint8)
图像大小的重置
"""
def resize_images(images,
size,
method=ResizeMethod.BILINEAR,
align_corners=False):
重置大小,放大或者缩小
images: 给定需要进行大小重置的tensor对象,shape要求为: [batch_size, height, width, channel] 或者 [height, width, channel]; 表示可以一次对很多图像做大小重置,也可以仅仅对一个图像做一个大小重置操作;
size:给定一个二元组,也就是(new_height, new_width)
method: 做一个放大和缩小的时候,采用什么方式放大缩小;如何产生新的像素点的值
class ResizeMethod(object):
BILINEAR = 0 # 默认值,二次插值
NEAREST_NEIGHBOR = 1 # 使用邻居的像素值作为新的像素值
BICUBIC = 2 # 三次插值,一般建议使用BICUBIC,但是运行速度相当比较慢。
AREA = 3 # 使用一个区域的所有颜色的均值作为新的像素值
返回的数据类型和输入的images的数据shape格式一致
"""
resize_image_tensor = tf.image.resize_images(images=image_tensor, size=(128, 80),
method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
图像的剪切和填充
"""
def resize_image_with_crop_or_pad(image, target_height, target_width):
image:需要进行操作的图像tensor对象
target_height, target_width: 新图像的高度和宽度
做填充和剪切的时候,是从中心位置开始计算
"""
crop_or_pad_image_tensor = tf.image.resize_image_with_crop_or_pad(image_tensor,
target_height=800, target_width=200)
print("新的数据形状:{}".format(np.shape(crop_or_pad_image_tensor.eval())))
show_image(crop_or_pad_image_tensor.eval())
central_crop_image_tensor = tf.image.central_crop(image_tensor, central_fraction=0.5)
"""
def crop_to_bounding_box(image, offset_height, offset_width, target_height,
target_width):
offset_height:给定从高度那个位置进行剪切,其实给定的是剪切的左上角的像素下标
offset_width: 给定从宽度那个维度进行剪切,其实给定的是剪切的左上角的像素下标
"""
crop_to_bounding_box_image_tensor = tf.image.crop_to_bounding_box(image_tensor, 100, 20, 500, 490)
"""
def pad_to_bounding_box(image, offset_height, offset_width, target_height,
target_width):
"""
pad_to_bounding_box_image_tensor = tf.image.pad_to_bounding_box(image_tensor, 200, 100, 1000, 1000)
旋转
flip_up_down_image_tensor = tf.image.flip_up_down(image_tensor)
flip_left_right_image_tensor = tf.image.flip_left_right(image_tensor)
transpose_image_tensor = tf.image.transpose_image(image_tensor)
rot90_image_tensor = tf.image.rot90(image_tensor, k=3)
颜色空间的转换
float_image_tensor = tf.image.convert_image_dtype(image_tensor, dtype=tf.float32)
gray_image_tensor = tf.image.rgb_to_grayscale(float_image_tensor)
hsv_image_tensor = tf.image.rgb_to_hsv(float_image_tensor)
rgb_image_tensor = tf.image.hsv_to_rgb(hsv_image_tensor)
rgb_image_tensor = tf.image.grayscale_to_rgb(gray_image_tensor)
a = gray_image_tensor
b = tf.less_equal(a, 0.9)
"""
def where(condition, x=None, y=None, name=None):
condition: 给定一个bool数据组成的tensor对象
x:当condition中的值为true的时候,返回的值
y:当condition中的值为false的时候,返回的值
NOTE: 要求condition、x、y的数据形状是一致的
"""
c = tf.where(condition=b, x=a, y=a - a)
d = tf.where(condition=b, x=tf.ones_like(c), y=c)
图像的调整
"""
def adjust_brightness(image, delta):
image: 需要调整的图像tensor对象
delta:调整的参数值,取值范围:(-1,1); 该值表示亮度增加或者减少的值。
底层是将image转换为hsv格式的数据,然后再进行处理。# rgb -> hsv -> h,s,v+delta -> rgb
"""
adjust_brightness_image_tensor = tf.image.adjust_brightness(image_tensor, delta=-0.5)
adjust_hue_image_tensor = tf.image.adjust_hue(image_tensor, delta=-0.8)
adjust_saturation_image_tensor = tf.image.adjust_saturation(image_tensor, saturation_factor=20)
adjust_contrast_image_tensor = tf.image.adjust_contrast(image_tensor, contrast_factor=20)
adjust_gamma_image_tensor = tf.image.adjust_gamma(float_image_tensor, gamma=100)
per_image_standardization_image_tensor = tf.image.per_image_standardization(image_tensor)
noisy_image_tensor = image_tensor + tf.cast(5 * tf.random_normal(shape=[600, 510, 3], mean=0, stddev=0.1), tf.uint8)
writer = tf.summary.FileWriter("./model/test03", sess.graph)
writer.close()