tf.image.resize_with_crop_or_pad
Обрезает и/или дополняет изображение до заданной ширины и высоты.
tf.image.resize_with_crop_or_pad(
image, target_height, target_width
)
Используется в ноутбуках
| Используется в учебниках |
|---|
Изменяет размер изображения до целевой ширины и высоты, либо центрально обрезая изображение, либо дополняя его нулями.
Если width или height больше заданного target_width или target_height соответственно, этот оператор центрально обрезает по этому измерению.
Например:
image = np.arange(75).reshape(5, 5, 3) # create 3-D image input
image[:,:,0] # print first channel just for demo purposes
array([[ 0, 3, 6, 9, 12],
[15, 18, 21, 24, 27],
[30, 33, 36, 39, 42],
[45, 48, 51, 54, 57],
[60, 63, 66, 69, 72]])
image = tf.image.resize_with_crop_or_pad(image, 3, 3) # crop
# print first channel for demo purposes; centrally cropped output
image[:,:,0]
<tf.Tensor: shape=(3, 3), dtype=int64, numpy=
array([[18, 21, 24],
[33, 36, 39],
[48, 51, 54]])>Если width или height меньше указанного target_width или target_height соответственно, этот оператор центрально дополняет нулями по этому измерению.
Например:
image = np.arange(1, 28).reshape(3, 3, 3) # create 3-D image input
image[:,:,0] # print first channel just for demo purposes
array([[ 1, 4, 7],
[10, 13, 16],
[19, 22, 25]])
image = tf.image.resize_with_crop_or_pad(image, 5, 5) # pad
# print first channel for demo purposes; we should see 0 paddings
image[:,:,0]
<tf.Tensor: shape=(5, 5), dtype=int64, numpy=
array([[ 0, 0, 0, 0, 0],
[ 0, 1, 4, 7, 0],
[ 0, 10, 13, 16, 0],
[ 0, 19, 22, 25, 0],
[ 0, 0, 0, 0, 0]])>| Аргументы | |
|---|---|
image | 4-мерный тензор формы [batch, height, width, channels] или 3-мерный тензор формы [height, width, channels]. |
target_height | Целевая высота. |
target_width | Целевая ширина. |
| Исключения | |
|---|---|
ValueError | если target_height или target_width равны нулю или отрицательны. |
| Возвращаемое значение | |
|---|---|
Обрезанное и/или дополненное изображение. Если images был 4-мерным, 4-мерный float тензор формы [batch, new_height, new_width, channels]. Если images был 3-мерным, 3-мерный float тензор формы [new_height, new_width, channels]. |
© 2022 The TensorFlow Authors. All rights reserved.
Licensed under the Creative Commons Attribution License 4.0.
Code samples licensed under the Apache 2.0 License.
https://www.tensorflow.org/api_docs/python/tf/image/resize_with_crop_or_pad