tf.image.pad_to_bounding_box
Stay organized with collections
Save and categorize content based on your preferences.
Pad image
with zeros to the specified height
and width
.
tf.image.pad_to_bounding_box(
image, offset_height, offset_width, target_height, target_width
)
Used in the notebooks
Adds offset_height
rows of zeros on top, offset_width
columns of
zeros on the left, and then pads the image on the bottom and right
with zeros until it has dimensions target_height
, target_width
.
This op does nothing if offset_*
is zero and the image already has size
target_height
by target_width
.
Usage Example:
x = [[[1., 2., 3.],
[4., 5., 6.]],
[[7., 8., 9.],
[10., 11., 12.]]]
padded_image = tf.image.pad_to_bounding_box(x, 1, 1, 4, 4)
padded_image
<tf.Tensor: shape=(4, 4, 3), dtype=float32, numpy=
array([[[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.]],
[[ 0., 0., 0.],
[ 1., 2., 3.],
[ 4., 5., 6.],
[ 0., 0., 0.]],
[[ 0., 0., 0.],
[ 7., 8., 9.],
[10., 11., 12.],
[ 0., 0., 0.]],
[[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.]]], dtype=float32)>
Args |
image
|
4-D Tensor of shape [batch, height, width, channels] or 3-D Tensor
of shape [height, width, channels] .
|
offset_height
|
Number of rows of zeros to add on top.
|
offset_width
|
Number of columns of zeros to add on the left.
|
target_height
|
Height of output image.
|
target_width
|
Width of output image.
|
Returns |
If image was 4-D, a 4-D float Tensor of shape
[batch, target_height, target_width, channels]
If image was 3-D, a 3-D float Tensor of shape
[target_height, target_width, channels]
|
Raises |
ValueError
|
If the shape of image is incompatible with the offset_* or
target_* arguments, or either offset_height or offset_width is
negative.
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2024-04-26 UTC.
[null,null,["Last updated 2024-04-26 UTC."],[],[],null,["# tf.image.pad_to_bounding_box\n\n\u003cbr /\u003e\n\n|------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/image_ops_impl.py#L1005-L1069) |\n\nPad `image` with zeros to the specified `height` and `width`.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.image.pad_to_bounding_box`](https://www.tensorflow.org/api_docs/python/tf/image/pad_to_bounding_box)\n\n\u003cbr /\u003e\n\n tf.image.pad_to_bounding_box(\n image, offset_height, offset_width, target_height, target_width\n )\n\n### Used in the notebooks\n\n| Used in the tutorials |\n|------------------------------------------------------------------------------------------------------------|\n| - [Frame interpolation using the FILM model](https://www.tensorflow.org/hub/tutorials/tf_hub_film_example) |\n\nAdds `offset_height` rows of zeros on top, `offset_width` columns of\nzeros on the left, and then pads the image on the bottom and right\nwith zeros until it has dimensions `target_height`, `target_width`.\n\nThis op does nothing if `offset_*` is zero and the image already has size\n`target_height` by `target_width`.\n\n#### Usage Example:\n\n x = [[[1., 2., 3.],\n [4., 5., 6.]],\n [[7., 8., 9.],\n [10., 11., 12.]]]\n padded_image = tf.image.pad_to_bounding_box(x, 1, 1, 4, 4)\n padded_image\n \u003ctf.Tensor: shape=(4, 4, 3), dtype=float32, numpy=\n array([[[ 0., 0., 0.],\n [ 0., 0., 0.],\n [ 0., 0., 0.],\n [ 0., 0., 0.]],\n [[ 0., 0., 0.],\n [ 1., 2., 3.],\n [ 4., 5., 6.],\n [ 0., 0., 0.]],\n [[ 0., 0., 0.],\n [ 7., 8., 9.],\n [10., 11., 12.],\n [ 0., 0., 0.]],\n [[ 0., 0., 0.],\n [ 0., 0., 0.],\n [ 0., 0., 0.],\n [ 0., 0., 0.]]], dtype=float32)\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|-----------------|------------------------------------------------------------------------------------------------------------|\n| `image` | 4-D Tensor of shape `[batch, height, width, channels]` or 3-D Tensor of shape `[height, width, channels]`. |\n| `offset_height` | Number of rows of zeros to add on top. |\n| `offset_width` | Number of columns of zeros to add on the left. |\n| `target_height` | Height of output image. |\n| `target_width` | Width of output image. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| If `image` was 4-D, a 4-D float Tensor of shape `[batch, target_height, target_width, channels]` If `image` was 3-D, a 3-D float Tensor of shape `[target_height, target_width, channels]` ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|-----------------------------------------------------------------------------------------------------------------------------------------------|\n| `ValueError` | If the shape of `image` is incompatible with the `offset_*` or `target_*` arguments, or either `offset_height` or `offset_width` is negative. |\n\n\u003cbr /\u003e"]]