TensorFlow学习之 图像预处理

  1 import tensorflow as tf
  2 import matplotlib.pyplot as plt
  3 
  4 image_raw_data = tf.gfile.GFile("./picture.jpg", "rb").read()
  5 with tf.Session() as sess:
  6     """
  7     图像编码解码处理
  8     """
  9     # 解码过程
 10     img_data = tf.image.decode_jpeg(image_raw_data)
 11     print(img_data.eval())
 12 
 13     # 编码过程
 14     encoded_image = tf.image.encode_jpeg(img_data)
 15     with tf.gfile.GFile('./output.jpg', 'wb') as f:
 16         f.write(encoded_image.eval())
 17 
 18     """
 19     图像大小调整
 20     """
 21     # 将图片数据转化为实数类型,将0-255的像素值转化为0.0-1.0之间的实数
 22     img_data = tf.image.convert_image_dtype(img_data, dtype=tf.float32)
 23 
 24     # 第一个参数:原始图像;第二个参数:调整后的图像大小;第三个参数:method参数给出调整图像大小的算法
 25     resized = tf.image.resize_images(img_data, [300, 300], method=0)
 26 
 27     # 目标尺寸小于原始尺寸,居中截取;否者填充
 28     croped = tf.image.resize_image_with_crop_or_pad(img_data, 500, 300)
 29 
 30     plt.figure()
 31     plt.subplot(2, 2, 1)
 32     plt.imshow(img_data.eval())
 33     plt.subplot(2, 2, 2)
 34     plt.imshow(resized.eval())
 35     plt.subplot(2, 1, 2)
 36     plt.imshow(croped.eval())
 37 
 38     """
 39     图像翻转
 40     """
 41     # 上下翻转
 42     flipped_up_down = tf.image.flip_up_down(img_data)
 43     # 50%的概率上下翻转
 44     flipped1 = tf.image.random_flip_up_down(img_data)
 45 
 46     # 左右翻转
 47     flipped_left_right = tf.image.flip_left_right(img_data)
 48     # 50%的概率左右翻转
 49     flipped2 = tf.image.random_flip_left_right(img_data)
 50 
 51     # 沿对角线高翻转
 52     transpose_image = tf.image.transpose_image(img_data)
 53 
 54     plt.figure()
 55     plt.subplot(2, 2, 1)
 56     plt.imshow(flipped_up_down.eval())
 57     plt.subplot(2, 2, 2)
 58     plt.imshow(flipped_left_right.eval())
 59     plt.subplot(2, 2, 3)
 60     plt.imshow(transpose_image.eval())
 61     plt.subplot(2, 2, 4)
 62     plt.imshow(flipped2.eval())
 63 
 64     """
 65     图像色彩调整
 66     """
 67     # 亮度 -0.5
 68     adjusted = tf.image.adjust_brightness(img_data, -0.5)
 69     # 将其值截取在0.0-1.0之间,防止像素实数值超出0.0-1.0的范围
 70     adjusted_down = tf.clip_by_value(adjusted, 0.0, 1.0)
 71     # 亮度 +0.5
 72     adjusted_up = tf.image.adjust_brightness(img_data, 0.5)
 73     # 在[-max_delta, max_delta]的范围之间随机调整图像亮度
 74     adjusted_random = tf.image.random_brightness(img_data, 0.2)
 75 
 76     # 对比度 x0.5
 77     adjusted1 = tf.image.adjust_contrast(img_data, 0.5)
 78     # 对比度 增加5倍
 79     adjusted2 = tf.image.adjust_contrast(img_data, 5)
 80     # 在[lower, upper]的范围内随机调整图像的对比度
 81     adjusted3 = tf.image.random_contrast(img_data, 2, 4)
 82 
 83     # 色相 +0.6
 84     adjusted_hue1 = tf.image.adjust_hue(img_data, 0.6)
 85     # 色相 -0.6
 86     adjusted_hue2 = tf.image.adjust_hue(img_data, -0.6)
 87     # 在[-max_delta, max_delta]的范围内随机调整图像的色相,max_delta取值范围[0, 0.5]
 88     adjusted_hue3 = tf.image.random_hue(img_data, 0.3)
 89 
 90     # 饱和度 -5
 91     adjust_saturation1 = tf.image.adjust_saturation(img_data, -5)
 92     # 饱和度 +5
 93     adjust_saturation2 = tf.image.adjust_saturation(img_data, 5)
 94     # 在[lower, upper]的范围之间随机调整图像的饱和度,lower必须是非负值
 95     adjust_saturation3 = tf.image.random_saturation(img_data, 0, 4)
 96     plt.figure()
 97     plt.subplot(4, 2, 1)
 98     plt.imshow(adjusted_down.eval())
 99     plt.subplot(4, 2, 2)
100     plt.imshow(adjusted_up.eval())
101     plt.subplot(4, 2, 3)
102     plt.imshow(adjusted1.eval())
103     plt.subplot(4, 2, 4)
104     plt.imshow(adjusted2.eval())
105     plt.subplot(4, 2, 5)
106     plt.imshow(adjusted_hue1.eval())
107     plt.subplot(4, 2, 6)
108     plt.imshow(adjusted_hue2.eval())
109     plt.subplot(4, 2, 7)
110     plt.imshow(adjust_saturation1.eval())
111     plt.subplot(4, 2, 8)
112     plt.imshow(adjust_saturation2.eval())
113 
114     # 图像数值标准化,均值为0,方差为1
115     adjust_standardization = tf.image.per_image_standardization(img_data)
116     plt.figure()
117     plt.imshow(adjust_standardization.eval())
118 
119     plt.show()

猜你喜欢

转载自www.cnblogs.com/reaptomorrow-flydream/p/9477447.html