TensorFlow学习笔记——图像增强

在TensorFlow中提供了一些图像增强的方法,比如放缩、裁剪、翻转、改变光照和对比度等。
下面分别对这几种方法进行介绍:
首先显示原图像,代码如下:

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

name = './0.png'  # 图片名称
img_string = tf.read_file(name)  # 读取图片
img_decode = tf.image.decode_image(img_string)  # 解码

sess = tf.Session()
img_decode_val = sess.run(img_decode)
print(img_decode_val.shape)

%matplotlib inline  # 内嵌绘图
imshow(img_decode_val)  # 显示图片

结果如下:
在这里插入图片描述1. 放缩
TensorFlow中对图像进行缩放的函数有:

方法名 功能
tf.image.resize_area 使用区域插值放缩图像
tf.image.resize_bicubic 使用双线性插值放缩图像
tf.image.resize_nearest_neighbor 使用最近邻插值放缩图像

以双线性插值放缩图像为例,代码如下:

name = './0.png'  # 图片名称
img_string = tf.read_file(name)  # 读取图片
img_decode = tf.image.decode_image(img_string)  # 解码

# 因为tf.image.resize_bicubic是对多张图像进行放缩,所以要对图像进行reshape
img_decode = tf.reshape(img_decode, [1, 719, 1148, 3])  # [719, 1148, 3] -> [1, 719, 1148, 3]
resize_img = tf.image.resize_bicubic(img_decode, [900, 1300])  # 把图像放缩至 900 * 1300  dtype : unit8 -> float32

sess = tf.Session()
img_decode_val = sess.run(resize_img)
img_decode_val0 = img_decode_val[0]  # 获取第一张图片
img_decode_val0 = np.asarray(img_decode_val0, np.uint8)  # dtype : float32 -> unit8
print(img_decode_val0.shape)

imshow(img_decode_val0)  # 显示图片

结果如下:
在这里插入图片描述
2. 裁剪
裁剪的方法有:

方法名 功能
tf.image.pad_to_bounding_box 超出图像部分使用黑色填充
tf.image.crop_to_bounding_box 裁剪
tf.random_crop 随机裁剪

以tf.image.pad_to_bounding_box为例,代码如下:

name = './0.png'  # 图片名称
img_string = tf.read_file(name)  # 读取图片
img_decode = tf.image.decode_image(img_string)  # 解码

# 因为image.pad_to_bounding_box是对多张图像进行放缩,所以要对图像进行reshape
img_decode = tf.reshape(img_decode, [1, 719, 1148, 3])  # [719, 1148, 3] -> [1, 719, 1148, 3]
padded_img = tf.image.pad_to_bounding_box(img_decode, 50, 100, 900, 1500)  # 画布大小为900 * 1500  图片位置 : (50, 100)

sess = tf.Session()
img_decode_val = sess.run(padded_img)
img_decode_val0 = img_decode_val[0]  # 获取第一张图片
print(img_decode_val0.shape)

imshow(img_decode_val0)  # 显示图片

结果如下:
在这里插入图片描述
3. 翻转
翻转的方法有:

方法名 功能
tf.image.flip_up_down 上下翻转
tf.image.flip_left_right 左右翻转
tf.image.random_flip_up_down 随机上下翻转
tf.image.random_flip_left_right 随机左右翻转

以上下翻转为例,代码如下:

name = './0.png'  # 图片名称
img_string = tf.read_file(name)  # 读取图片
img_decode = tf.image.decode_image(img_string)  # 解码

img_decode = tf.reshape(img_decode, [1, 719, 1148, 3])  # [719, 1148, 3] -> [1, 719, 1148, 3]
flipped_img = tf.image.flip_up_down(img_decode)  # 上下翻转

sess = tf.Session()
img_decode_val = sess.run(flipped_img)
img_decode_val0 = img_decode_val[0]  # 获取第一张图片
print(img_decode_val0.shape)

imshow(img_decode_val0)  # 显示图片

结果如下:
在这里插入图片描述
4. 改变光照和对比度
改变光照和对比度的方法有:

方法名 功能
tf.image.adjust_brightness 改变光照
tf.image.random_brightness 随机改变光照
tf.image.adjust_contrast 改变对比度
tf.image.random_contrast 随机改变对比度

以tf.image.adjust_brightness为例,代码如下:

name = './0.png'  # 图片名称
img_string = tf.read_file(name)  # 读取图片
img_decode = tf.image.decode_image(img_string)  # 解码

img_decode = tf.reshape(img_decode, [1, 719, 1148, 3])  # [719, 1148, 3] -> [1, 719, 1148, 3]
new_img = tf.image.adjust_brightness(img_decode, +0.3)  # 改变光照  + : 调亮    - : 调暗

sess = tf.Session()
img_decode_val = sess.run(new_img)
img_decode_val0 = img_decode_val[0]  # 获取第一张图片
print(img_decode_val0.shape)

imshow(img_decode_val0)  # 显示图片

结果如下:
在这里插入图片描述
提醒:上述所有方法并不是各种图像增强技术的所有方法。如:在图像裁剪方法中,并不只有那三种。

原创文章 14 获赞 16 访问量 6870

猜你喜欢

转载自blog.csdn.net/qq_37638909/article/details/105656037