size using the specified
tf.image.resize( images, size, method=ResizeMethod.BILINEAR, preserve_aspect_ratio=False, antialias=False, name=None )
Resized images will be distorted if their original aspect ratio is not the same as
size. To avoid distortions see
image = tf.constant([ [1,0,0,0,0], [0,1,0,0,0], [0,0,1,0,0], [0,0,0,1,0], [0,0,0,0,1], ]) # Add "batch" and "channels" dimensions image = image[tf.newaxis, ..., tf.newaxis] image.shape.as_list() # [batch, height, width, channels] [1, 5, 5, 1] tf.image.resize(image, [3,5])[0,...,0].numpy() array([[0.6666667, 0.3333333, 0. , 0. , 0. ], [0. , 0. , 1. , 0. , 0. ], [0. , 0. , 0. , 0.3333335, 0.6666665]], dtype=float32)
It works equally well with a single image instead of a batch of images:
tf.image.resize(image, [3,5]).shape.as_list() [3, 5, 1]
antialias is true, the sampling filter will anti-alias the input image as well as interpolate. When downsampling an image with anti-aliasing the sampling filter kernel is scaled in order to properly anti-alias the input image signal.
antialias has no effect when upsampling an image:
a = tf.image.resize(image, [5,10]) b = tf.image.resize(image, [5,10], antialias=True) tf.reduce_max(abs(a - b)).numpy() 0.0
method argument expects an item from the
image.ResizeMethod enum, or the string equivalent. The options are:
bilinear: Bilinear interpolation. If
antialiasis true, becomes a hat/tent filter function with radius 1 when downsampling.
lanczos3: Lanczos kernel with radius 3. High-quality practical filter but may have some ringing, especially on synthetic images.
lanczos5: Lanczos kernel with radius 5. Very-high-quality filter but may have stronger ringing.
bicubic: Cubic interpolant of Keys. Equivalent to Catmull-Rom kernel. Reasonably good quality and faster than Lanczos3Kernel, particularly when upsampling.
gaussian: Gaussian kernel with radius 3, sigma = 1.5 / 3.0.
nearest: Nearest neighbor interpolation.
antialiashas no effect when used with nearest neighbor interpolation.
area: Anti-aliased resampling with area interpolation.
antialiashas no effect when used with area interpolation; it always anti-aliases.
mitchellcubic: Mitchell-Netravali Cubic non-interpolating filter. For synthetic images (especially those lacking proper prefiltering), less ringing than Keys cubic kernel but less sharp.
Note: Near image edges the filtering kernel may be partially outside the image boundaries. For these pixels, only input pixels inside the image will be included in the filter sum, and the output value will be appropriately normalized.
The return value has type
float32, unless the
ResizeMethod.NEAREST_NEIGHBOR, then the return dtype is the dtype of
nn = tf.image.resize(image, [5,7], method='nearest') nn[0,...,0].numpy() array([[1, 0, 0, 0, 0, 0, 0], [0, 1, 1, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 1, 1, 0], [0, 0, 0, 0, 0, 0, 1]], dtype=int32)
preserve_aspect_ratio=True, the aspect ratio is preserved, so
size is the maximum for each dimension:
max_10_20 = tf.image.resize(image, [10,20], preserve_aspect_ratio=True) max_10_20.shape.as_list() [1, 10, 10, 1]
| || 4-D Tensor of shape |
| || A 1-D int32 Tensor of 2 elements: |
| || An |
| || Whether to preserve the aspect ratio. If this is set, then |
| ||Whether to use an anti-aliasing filter when downsampling an image.|
| ||A name for this operation (optional).|
| || if the shape of |
| || if |
| ||if an unsupported resize method is specified.|
| If |
© 2020 The TensorFlow Authors. All rights reserved.
Licensed under the Creative Commons Attribution License 3.0.
Code samples licensed under the Apache 2.0 License.