TensorFlow has a number of functions for resizing.
When I looked it up, there are 6 functions for resize in TensorFlow.
When I read the document, the explanation is written, but it doesn't come out very well.
So I would like to try it out and visually understand how it works.
In conclusion, tf.image.resize_images included the following four features:
So, I will try only the following two.
The images used are Lena, who has a size of 256x256, and a cat, which has a size of 256x170.
Let's try them one by one.
tf.image.resize_images(images, new_height, new_width, method=0, align_corners=False) resize_images is a function that resizes images to new_height x new_width by the specified method.
A 4D tensor [batch, height, width, channels] or a 3D tensor [height, width, channels] can be given as the input image. If given in 4D, batch conversion of images is possible.
A 4D tensor [batch, new_height, new_width, channels] or a 3D tensor [new_height, new_width, channels] is returned as the return value. This changes according to the input tensor.
Try reducing the image to 128x128 with the following settings.
tf.image.resize_images(image, 128, 128)
The result is as follows.
Lena has been able to shrink without problems, but the cat is distorted. In this way, ** If the original aspect ratio is not the same as new_width and new_height, the resized image will be distorted. ** To avoid this, use resize_image_with_crop_or_pad. I'll try this later.
The resize_images function can take four methods:
The results of trying one by one are as follows:
It can be seen that the degree of blurring is slightly different from the original image when enlarged. It seems that each has its own uses.
align_corners Below are the images when align_corners is False and True:
To be honest, I don't really understand the difference, but it seems that True scales the input to (new_height -1) / (height -1), and False scales it to new_height / height. By setting it to True, it seems that the positions of all four corners of the input and output are accurately aligned. I'm not sure what to do for it.
tf.image.resize_image_with_crop_or_pad(image, target_height, target_width) The resize_image_with_crop_or_pad function is a function that trims and / or pad the image to the specified size (target_height x target_width).
Resize the image to target_width and target_height by cropping the center of the image or padding the black image.
Crop the center of the image if the width or height is greater than the specified target_width or target_height, respectively. The following is a case where a 256x170 cat image is resized by specifying 128x128 for target_height and target_width. You can see that the center of the image is cropped.
Embeds a black image if the width or height is less than target_width or target_height. The following is a case where a 256x170 cat image is resized by specifying 196x196 for target_height and target_width. The center of the image is cropped while being padded up and down.
By combining resize_image_with_crop_or_pad and resize_images, you can reduce the image with only padding without cropping.
manner
Recommended Posts