Tensorflow图像增强的几个方法

在Tensorflow中提供了一些图像增强的方法,比如:缩放、裁剪、反转、光照对比度等。

# resize 缩放
# crop 裁剪
# flip 反转
# brightness && contrast 改变光照、对比度

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

name = './fengjing.jpg'
img_string = tf.read_file(name) # 读进来的是字符串
img_decoded = tf.image.decode_image(img_string) # 将字符串解析成图片

sess = tf.Session()
img_decode_val = sess.run(img_decoded)
print(img_decode_val.shape) # (546, 820, 3)

# resize

# tf.image.resize_area

# tf.image.resize_bicubic 二次线性插值法进行图片的缩放
# 当图片缩小时是没有损失的,当图像放大时是如何插入的
# tf.image.resize_nearest_neighbor
name = './fengjing.jpg'
img_string = tf.read_file(name) # 读进来的是字符串
img_decoded = tf.image.decode_image(img_string) # 将字符串解析成图片
# 因为在tf中,样本都是处理的4维图像,所以需要变形
img_decoded = tf.reshape(img_decoded, [1, 546, 820, 3])
resize_img = tf.image.resize_bicubic(img_decoded, [1092, 1640])

sess = tf.Session()
img_decode_val = sess.run(resize_img)
img_decode_val = img_decode_val.reshape((1092, 1640, 3)) # 已经变成float类型
# 需要转变成 int 类型
img_decode_val = np.array(img_decode_val, np.uint8)
print(img_decode_val.shape) # (546, 820, 3)
imshow(img_decode_val)
plt.show()

# crop
# tf.image.pad_to_bounding_box 图像四周可以填充
# tf.image.crop_to_bounding_box 裁剪
# tf.random_crop 随机裁剪
name = './fengjing.jpg'
img_string = tf.read_file(name) # 读进来的是字符串
img_decoded = tf.image.decode_image(img_string) # 将字符串解析成图片
img_decoded = tf.reshape(img_decoded, [1, 546, 820, 3])
#
padded_img = tf.image.pad_to_bounding_box(img_decoded,
                                          50, # 新图像在画布上的位置
                                          100,
                                          1000, # 画布大小
                                          1000
                                          )

sess = tf.Session()
img_decode_val = sess.run(padded_img)
img_decode_val = img_decode_val.reshape((1000, 1000, 3))
img_decode_val = np.array(img_decode_val, np.uint8)
print(img_decode_val.shape) # (546, 820, 3)
imshow(img_decode_val)
plt.show()

# flip 反转
# tf.image.flip_up_down
# tf.image.flip_left_right
# tf.image.random_flip_up_down
# tf.image.random_flip_left_right
name = './fengjing.jpg'
img_string = tf.read_file(name) # 读进来的是字符串
img_decoded = tf.image.decode_image(img_string) # 将字符串解析成图片
img_decoded = tf.reshape(img_decoded, [546, 820, 3])
flipped_img = tf.image.flip_up_down(img_decoded)

sess = tf.Session()
img_decode_val = sess.run(flipped_img)
img_decode_val = img_decode_val.reshape((546, 820, 3))
# img_decode_val = np.array(img_decode_val, np.uint8)
print(img_decode_val.shape) # (546, 820, 3)
imshow(img_decode_val)
plt.show()

# brightness 光照度
# tf.image.adjust_brightness
# tf.image.random_brightness
# tf.image.adjust_constrast
# tf.image.random_constrast
name = './fengjing.jpg'
img_string = tf.read_file(name) # 读进来的是字符串
img_decoded = tf.image.decode_image(img_string) # 将字符串解析成图片
img_decoded = tf.reshape(img_decoded, [546, 820, 3])
new_img = tf.image.adjust_brightness(img_decoded, 0.5) # 参数2, 控制增强/减弱 百分比

sess = tf.Session()
img_decode_val = sess.run(new_img)
img_decode_val = img_decode_val.reshape((546, 820, 3))
#img_decode_val = np.array(img_decode_val, np.uint8)
print(img_decode_val.shape) # (546, 820, 3)
print(img_decode_val)
imshow(img_decode_val)
plt.show()

你可能感兴趣的:(tensorflow,Tensorflow学习笔记)