TensorFlow图像预处理函数

预处理图像

 

 

 

文件名:       cat.jpg

 

 

读取、打印图片

 

import matplotlib.pyplot as plt
import tensorflow as tf   
import numpy as np


image_raw_data = tf.gfile.FastGFile("./cat.jpg",'rb').read()

with tf.Session() as sess:
    img_data = tf.image.decode_jpeg(image_raw_data)
    
    # 输出解码之后的三维矩阵。
    #print(img_data.eval())

    #print(img_data.get_shape())
    img_data.set_shape([1797, 2673, 3])
    print(img_data.get_shape())


with tf.Session() as sess:
    plt.imshow(img_data.eval())
    #print(img_data.get_shape().as_list())
    plt.show()

 

 

 

 

 

 

 

 

调整图片大小

 

tf.image.convert_image_dtype  

image_float = tf.image.convert_image_dtype(img_data, tf.float32) 等价于 image_float=tf.cast(img_data, tf.float32)/255
 

tf.image.resize_images 调整图像大小
with tf.Session() as sess:
    # 如果直接以0-255范围的整数数据输入resize_images,那么输出将是0-255之间的实数,不利于后续处理。
    #如果直接以0-1之间的实数数据输入resize_images,那么输出将是0-1之间的实数。

    #建议在调整图片大小前,先将图片转为0-1范围的实数。
    #tf.image.convert_image_dtype 
    #image_float=tf.cast(img_data, tf.float32)/255
    image_float = tf.image.convert_image_dtype(img_data, tf.float32)

    resized = tf.image.resize_images(image_float, [300, 300], method=0)
    #print(resized.eval())
    plt.imshow(resized.eval())
    plt.show()

 

 

 

 

 

 

 

 

 

 

 

裁剪和填充图片

tf.image.resize_image_with_crop_or_pad
# 裁剪、填充图像
with tf.Session() as sess:    
    #tf.image.resize_image_with_crop_or_pad 函数对原图像裁剪或填充。第一个参数为原始图像,后面两个参数为图像裁剪或填充后的大小。
    # 如果原始图像的尺寸大于目标图像,则自动截取原图像居中部分;如果原图像的尺寸大于目标图像,则自动在原始图像四周填充0为背景。
    croped = tf.image.resize_image_with_crop_or_pad(img_data, 1000, 1000) #截取
    padded = tf.image.resize_image_with_crop_or_pad(img_data, 3000, 3000) #填充
    #plt.imshow(croped.eval())
    #plt.show()
    plt.imshow(padded.eval())
    plt.show()

 

 

 

 

 

 

 

通过比例裁剪图像大小
# 通过比例裁剪图像大小
# tf.image.central_crop 第一个参数为原始图像,第二个为调整比例,该比例为 (0,1] 的实数。
with tf.Session() as sess:    
    central_crop = tf.image.central_crop(img_data, 0.5) 
    plt.imshow(central_crop.eval())
    plt.show()

 

 

 

 

 

 

 

 

 

 

图像翻转
# 图像翻转
with tf.Session() as sess: 
    # 上下翻转
    flipped1 = tf.image.flip_up_down(img_data)
    plt.imshow(flipped1.eval())
    plt.show()


    # 左右翻转
    flipped2 = tf.image.flip_left_right(img_data)
    plt.imshow(flipped2.eval())
    plt.show()

   
    #对角线翻转
    transposed = tf.image.transpose_image(img_data)
    plt.imshow(transposed.eval())
    plt.show()


    # 以一定概率上下翻转图片。
    # 以50%概率上下翻转图片
    flipped1 = tf.image.random_flip_up_down(img_data)
    plt.imshow(flipped1.eval())
    plt.show()


    # 以一定概率左右翻转图片。
    # 以50%概率左右翻转图片
    flipped2 = tf.image.random_flip_left_right(img_data)
    plt.imshow(flipped2.eval())
    plt.show()

 

 

 

 

 

 

 

 

 

 

图像色彩调整
# 图像色彩调整
with tf.Session() as sess:
    # 在进行一系列图片调整前,先将图片转换为实数形式,有利于保持计算精度。
    image_float = tf.image.convert_image_dtype(img_data, tf.float32)







# 亮度调整############################## # 将图片的亮度+0.5。 #adjusted = tf.image.adjust_brightness(image_float, 0.5) # 将图片的亮度-0.5 #adjusted = tf.image.adjust_brightness(image_float, -0.5) # 随机亮度调整 # 在[-max_delta, max_delta)的范围随机调整图片的亮度。 #adjusted = tf.image.random_brightness(image_float, max_delta=0.5)










# 对比度调整############################## # (x-mean)*delta+mean # 将图片的对比度+5 #adjusted = tf.image.adjust_contrast(image_float, 5) # 将图片的对比度-0.5 #adjusted = tf.image.adjust_contrast(image_float, -0.5) # 在[lower, upper]的范围随机调整图的对比度。 # upper >= lower >= 0 lower=0.5 upper=5 #adjusted = tf.image.random_contrast(image_float, lower, upper)







# 色相调整############################## # delta 范围:[-1, +1] #adjusted = tf.image.adjust_hue(image_float, -0.1) #adjusted = tf.image.adjust_hue(image_float, -0.3) #adjusted = tf.image.adjust_hue(image_float, -0.6) #adjusted = tf.image.adjust_hue(image_float, -0.9) #adjusted = tf.image.adjust_hue(image_float, 0.1) #adjusted = tf.image.adjust_hue(image_float, 0.3) #adjusted = tf.image.adjust_hue(image_float, 0.6) #adjusted = tf.image.adjust_hue(image_float, 0.9) # 在[-max_delta, max_delta]的范围随机调整图片的色相。max_delta的取值在[0, 0.5]之间。 max_delta=0.3 #adjusted = tf.image.random_hue(image_float, max_delta)







# 饱和度调整############################## # 将图片的饱和度-5。 #adjusted = tf.image.adjust_saturation(image_float, -5) # 将图片的饱和度+5。 #adjusted = tf.image.adjust_saturation(image_float, 5) # 在[lower, upper]的范围随机调整图的饱和度。 lower=0 # lower>=0 upper=5 #adjusted = tf.image.random_saturation(image_float, lower, upper)




# 将代表一张图片的三维矩阵中的数字均值变为0,方差变为1。 adjusted = tf.image.per_image_standardization(image_float)




# 在最终输出前,将实数取值截取到0-1范围内。 adjusted = tf.clip_by_value(adjusted, 0.0, 1.0) plt.imshow(adjusted.eval()) plt.show()

 

posted on 2020-04-07 10:20  Angry_Panda  阅读(700)  评论(0)    收藏  举报

导航