tf.image.crop_to_bounding_box
Stay organized with collections
Save and categorize content based on your preferences.
Crops an image
to a specified bounding box.
tf.image.crop_to_bounding_box(
image, offset_height, offset_width, target_height, target_width
)
This op cuts a rectangular bounding box out of image
. The top-left corner
of the bounding box is at offset_height, offset_width
in image
, and the
lower-right corner is at
offset_height + target_height, offset_width + target_width
.
Example Usage:
image = tf.constant(np.arange(1, 28, dtype=np.float32), shape=[3, 3, 3])
image[:,:,0] # print the first channel of the 3-D tensor
<tf.Tensor: shape=(3, 3), dtype=float32, numpy=
array([[ 1., 4., 7.],
[10., 13., 16.],
[19., 22., 25.]], dtype=float32)>
cropped_image = tf.image.crop_to_bounding_box(image, 0, 0, 2, 2)
cropped_image[:,:,0] # print the first channel of the cropped 3-D tensor
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[ 1., 4.],
[10., 13.]], dtype=float32)>
Args |
image
|
4-D Tensor of shape [batch, height, width, channels] or 3-D
Tensor of shape [height, width, channels] .
|
offset_height
|
Vertical coordinate of the top-left corner of the bounding
box in image .
|
offset_width
|
Horizontal coordinate of the top-left corner of the bounding
box in image .
|
target_height
|
Height of the bounding box.
|
target_width
|
Width of the bounding box.
|
Returns |
If image was 4-D, a 4-D Tensor of shape
[batch, target_height, target_width, channels] .
If image was 3-D, a 3-D Tensor of shape
[target_height, target_width, channels] .
It has the same dtype with image .
|
Raises |
ValueError
|
image is not a 3-D or 4-D Tensor .
|
ValueError
|
offset_width < 0 or offset_height < 0 .
|
ValueError
|
target_width <= 0 or target_height <= 0 .
|
ValueError
|
width < offset_width + target_width or
height < offset_height + target_height .
|
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 2023-10-06 UTC.
[null,null,["Last updated 2023-10-06 UTC."],[],[],null,["# tf.image.crop_to_bounding_box\n\n\u003cbr /\u003e\n\n|------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.12.1/tensorflow/python/ops/image_ops_impl.py#L1157-L1255) |\n\nCrops an `image` to a specified bounding box.\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.crop_to_bounding_box`](https://www.tensorflow.org/api_docs/python/tf/image/crop_to_bounding_box)\n\n\u003cbr /\u003e\n\n tf.image.crop_to_bounding_box(\n image, offset_height, offset_width, target_height, target_width\n )\n\nThis op cuts a rectangular bounding box out of `image`. The top-left corner\nof the bounding box is at `offset_height, offset_width` in `image`, and the\nlower-right corner is at\n`offset_height + target_height, offset_width + target_width`.\n\n#### Example Usage:\n\n image = tf.constant(np.arange(1, 28, dtype=np.float32), shape=[3, 3, 3])\n image[:,:,0] # print the first channel of the 3-D tensor\n \u003ctf.Tensor: shape=(3, 3), dtype=float32, numpy=\n array([[ 1., 4., 7.],\n [10., 13., 16.],\n [19., 22., 25.]], dtype=float32)\u003e\n cropped_image = tf.image.crop_to_bounding_box(image, 0, 0, 2, 2)\n cropped_image[:,:,0] # print the first channel of the cropped 3-D tensor\n \u003ctf.Tensor: shape=(2, 2), dtype=float32, numpy=\n array([[ 1., 4.],\n [10., 13.]], 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` | Vertical coordinate of the top-left corner of the bounding box in `image`. |\n| `offset_width` | Horizontal coordinate of the top-left corner of the bounding box in `image`. |\n| `target_height` | Height of the bounding box. |\n| `target_width` | Width of the bounding box. |\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 `Tensor` of shape `[batch, target_height, target_width, channels]`. If `image` was 3-D, a 3-D `Tensor` of shape `[target_height, target_width, channels]`. It has the same dtype with `image`. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|------------------------------------------------------------------------------------|\n| `ValueError` | `image` is not a 3-D or 4-D `Tensor`. |\n| `ValueError` | `offset_width \u003c 0` or `offset_height \u003c 0`. |\n| `ValueError` | `target_width \u003c= 0` or `target_height \u003c= 0`. |\n| `ValueError` | `width \u003c offset_width + target_width` or `height \u003c offset_height + target_height`. |\n\n\u003cbr /\u003e"]]