在图片预处理的时候往往需要保证原始图像的长宽尺寸保持不变,达到resize前后不失真的效果,但是数据集中很难保证每个样本图像长宽比例相等。为了保证不失真,在图像周围做0填充是不错的选择。
tensorflow2.x提供了很多图片预处理的API,实现填充式resize我们使用的是
tf.image.resize_with_pad(
image, target_height, target_width, method=ResizeMethod.BILINEAR,
antialias=False
)
Args | |
---|---|
image | 4-D Tensor of shape [batch, height, width, channels] or 3-D Tensor of shape [height, width, channels] . 4D [batchsize, 高, 宽, 通道数] 或者3D [高, 宽, 通道数] ] 的张量, |
target_height | Target height. 目标图像的高 |
target_width | Target width. 目标图像的宽 |
method | Method to use for resizing image. See image.resize() resize的方法,例如 最近邻插值法, 双线性插值法 等 |
antialias | Whether to use anti-aliasing when resizing. See 'image.resize()'. 下采样时是否使用抗混叠滤波器 这个滤波器的内容具体自查 |
Raises | |
---|---|
ValueError | if target_height or target_width are zero or negative. |
Returns | |
---|---|
Resized and padded image. If |
import tensorflow as tf
import matplotlib.pyplot as plt
img = tf.io.read_file('./test.jpg')
img = tf.io.decode_jpeg(img, channels=3)
print(img.shape) # (579, 727, 3)
plt.imshow(img) # 今天突然发现 plt的imshow方法竟然可以接受一个张量 哈哈
显示未resize前的图像:
new_img = tf.image.resize_with_pad(img, 512, 512, 'nearest', antialias=True) # 需要注意的是,这里resize时如果想看原始图像的效果,则需要设置插值方式为最近邻插值法,这样保证像素灰度数据在0-255范围,且为整数
plt.imshow(new_img)
resize后的结果: