tensorflow2.x image filling type without distortion resize

During image preprocessing, it is often necessary to ensure that the length and width dimensions of the original image remain unchanged to achieve the effect of no distortion before and after resize. However, it is difficult to ensure that the length and width ratio of each sample image is equal in the data set. In order to ensure that there is no distortion, it is a good choice to do 0 padding around the image.

tensorflow2.x provides a lot of image preprocessing APIs, we use the filling type 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, height, width, number of channels] or 3D [height, width, number of channels]] tensor,
target_height Target height. The height of the      target image
target_width Target width. The width of the         target image
method Method to use for resizing image. See image.resize()          resize的方法,例如 最近邻插值法, 双线性插值法 等
antialias Whether to use anti-aliasing when resizing. See'image.resize()'. Whether to use anti-aliasing            filter when downsampling, the content of this filter is specifically self-examined

Raises

ValueError if target_height or target_width are zero or negative.

Returns

Resized and padded image. If images was 4-D, a 4-D float Tensor of shape [batch, new_height, new_width, channels]. If images was 3-D, a 3-D float Tensor of shape [new_height, new_width, channels].        简单啦~~~~~~

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方法竟然可以接受一个张量 哈哈

Display the image before resizing:

new_img = tf.image.resize_with_pad(img, 512, 512, 'nearest', antialias=True)    # 需要注意的是,这里resize时如果想看原始图像的效果,则需要设置插值方式为最近邻插值法,这样保证像素灰度数据在0-255范围,且为整数
plt.imshow(new_img)

The result after resize:

Guess you like

Origin blog.csdn.net/qq_39197555/article/details/112258368