Tensorflow图像处理

Tensorflow图像处理主要包括:调整尺寸,图像翻转,调整色彩,处理标注框。

代码如下:

#coding=utf-8
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)
     plt.subplot(331)
     plt.title("Original")

     plt.imshow(img_data.eval())
     #plt.show()

     resized = tf.image.resize_images(img_data, [100, 100], method=0)
     # TensorFlow的函数处理图片后存储的数据是float32格式的,需要转换成uint8才能正确打印图片。
     print("Digital type: ", resized.dtype)
     resized = np.asarray(resized.eval(), dtype='uint8')
     # tf.image.convert_image_dtype(rgb_image, tf.float32)
     plt.subplot(332)
     plt.title("100*100")
     plt.imshow(resized)

     #plt.show()

     croped = tf.image.resize_image_with_crop_or_pad(img_data, 500, 500)
     padded = tf.image.resize_image_with_crop_or_pad(img_data, 1500, 1500)
     plt.subplot(333)
     plt.title("500*500")
     plt.imshow(croped.eval())
    # plt.show()
     plt.subplot(334)
     plt.title("1500*1500")
     plt.imshow(padded.eval())
     #plt.show()

     central_cropped = tf.image.central_crop(img_data, 0.5)
     plt.subplot(335)
     plt.title("*0.5")
     plt.imshow(central_cropped.eval())
#     plt.show()

     # 上下翻转
     flipped1 = tf.image.flip_up_down(img_data)
     plt.subplot(336)
     plt.title("up-down")
     plt.imshow(flipped1.eval())
     #plt.show()
     # 左右翻转
     flipped2 = tf.image.flip_left_right(img_data)
     plt.subplot(337)
     plt.title("left-right")
     plt.imshow(flipped2.eval())
     #plt.show()
     # 对角线翻转
     transposed = tf.image.transpose_image(img_data)
     plt.subplot(338)
     plt.title("transpose")
     plt.imshow(transposed.eval())
    # plt.show()

     flipped3 = tf.image.random_flip_up_down(img_data)
     plt.subplot(339)
     plt.title("flip-up-down")
     plt.imshow(flipped3.eval())
     plt.show()
#————————————————————————————————————————————#
     # 将图片的亮度-0.5。
     adjusted = tf.image.adjust_brightness(img_data, -0.5)
     plt.subplot(331)
     plt.imshow(adjusted.eval())

     plt.title("bright-0.5")
     #plt.show()

     # 将图片的亮度0.5
     adjusted = tf.image.adjust_brightness(img_data, 0.5)
     plt.subplot(332)
     plt.imshow(adjusted.eval())

     plt.title("bright+0.5")
     #plt.show()
     # 在[-max_delta, max_delta)的范围随机调整图片的亮度。
     adjusted = tf.image.random_brightness(img_data, max_delta=0.5)
     plt.subplot(333)
     plt.imshow(adjusted.eval())

     plt.title("bright-random")
     #plt.show()
     # 将图片的对比度-5
     adjusted = tf.image.adjust_contrast(img_data, -5)
     plt.subplot(334)
     plt.imshow(adjusted.eval())
     plt.title("contrast-5")
     #plt.show()
     # 将图片的对比度+5
     adjusted = tf.image.adjust_contrast(img_data, 5)
     plt.subplot(335)
     plt.imshow(adjusted.eval())

     plt.title("contrast+5")
     #plt.show()
     # 在[lower, upper]的范围随机调整图的对比度。
     adjusted = tf.image.random_contrast(img_data, 0.1, 0.6)
     plt.subplot(336)
     plt.imshow(adjusted.eval())
     plt.title("contrast-random")
     #plt.show()

     # 调整图片的色相
     adjusted = tf.image.adjust_hue(img_data, 0.1)
     plt.subplot(337)
     plt.imshow(adjusted.eval())
     plt.title("hue_0.1")
     #plt.show()

     # 在[-max_delta, max_delta]的范围随机调整图片的色相。max_delta的取值在[0, 0.5]之间。
     adjusted = tf.image.random_hue(img_data, 0.5)
     plt.subplot(338)
     plt.imshow(adjusted.eval())
     plt.title("hue-random_0.5")
     #plt.show()

     # 将图片的饱和度-5。
     adjusted = tf.image.adjust_saturation(img_data, -2)
     plt.subplot(339)
     plt.title("saturation-2")
     plt.imshow(adjusted.eval())
     plt.show()

     # 在[lower, upper]的范围随机调整图的饱和度。
     #adjusted = tf.image.random_saturation(img_data, 0, 5)

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

  

效果图:

猜你喜欢

转载自www.cnblogs.com/shanlizi/p/9050536.html