tf.image.central_crop
Stay organized with collections
Save and categorize content based on your preferences.
Crop the central region of the image(s).
tf.image.central_crop(
image, central_fraction
)
Used in the notebooks
Remove the outer parts of an image but retain the central region of the image
along each dimension. If we specify central_fraction = 0.5
, this function
returns the region marked with "X" in the below diagram. The larger the value
of central_fraction
, the larger the dimension of the region to be cropped
and retained.
--------
| |
| XXXX |
| XXXX |
| | where "X" is the central 50% of the image.
--------
This function works on either a single image (image
is a 3-D Tensor), or a
batch of images (image
is a 4-D Tensor).
Usage Example:
x = [[[1.0, 2.0, 3.0],
[4.0, 5.0, 6.0],
[7.0, 8.0, 9.0],
[10.0, 11.0, 12.0]],
[[13.0, 14.0, 15.0],
[16.0, 17.0, 18.0],
[19.0, 20.0, 21.0],
[22.0, 23.0, 24.0]],
[[25.0, 26.0, 27.0],
[28.0, 29.0, 30.0],
[31.0, 32.0, 33.0],
[34.0, 35.0, 36.0]],
[[37.0, 38.0, 39.0],
[40.0, 41.0, 42.0],
[43.0, 44.0, 45.0],
[46.0, 47.0, 48.0]]]
tf.image.central_crop(x, 0.5)
<tf.Tensor: shape=(2, 2, 3), dtype=float32, numpy=
array([[[16., 17., 18.],
[19., 20., 21.]],
[[28., 29., 30.],
[31., 32., 33.]]], dtype=float32)>
Args |
image
|
Either a 3-D float Tensor of shape [height, width, depth], or a 4-D
Tensor of shape [batch_size, height, width, depth].
|
central_fraction
|
float (0, 1], fraction of size to crop
|
Raises |
ValueError
|
if central_crop_fraction is not within (0, 1].
|
Returns |
3-D / 4-D float Tensor, as per the input.
|
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.central_crop\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#L858-L1002) |\n\nCrop the central region of the image(s).\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.central_crop`](https://www.tensorflow.org/api_docs/python/tf/image/central_crop)\n\n\u003cbr /\u003e\n\n tf.image.central_crop(\n image, central_fraction\n )\n\n### Used in the notebooks\n\n| Used in the tutorials |\n|--------------------------------------------------------------------------------------|\n| - [Data augmentation](https://www.tensorflow.org/tutorials/images/data_augmentation) |\n\nRemove the outer parts of an image but retain the central region of the image\nalong each dimension. If we specify `central_fraction = 0.5`, this function\nreturns the region marked with \"X\" in the below diagram. The larger the value\nof `central_fraction`, the larger the dimension of the region to be cropped\nand retained. \n\n --------\n | |\n | XXXX |\n | XXXX |\n | | where \"X\" is the central 50% of the image.\n --------\n\nThis function works on either a single image (`image` is a 3-D Tensor), or a\nbatch of images (`image` is a 4-D Tensor).\n\n#### Usage Example:\n\n x = [[[1.0, 2.0, 3.0],\n [4.0, 5.0, 6.0],\n [7.0, 8.0, 9.0],\n [10.0, 11.0, 12.0]],\n [[13.0, 14.0, 15.0],\n [16.0, 17.0, 18.0],\n [19.0, 20.0, 21.0],\n [22.0, 23.0, 24.0]],\n [[25.0, 26.0, 27.0],\n [28.0, 29.0, 30.0],\n [31.0, 32.0, 33.0],\n [34.0, 35.0, 36.0]],\n [[37.0, 38.0, 39.0],\n [40.0, 41.0, 42.0],\n [43.0, 44.0, 45.0],\n [46.0, 47.0, 48.0]]]\n tf.image.central_crop(x, 0.5)\n \u003ctf.Tensor: shape=(2, 2, 3), dtype=float32, numpy=\n array([[[16., 17., 18.],\n [19., 20., 21.]],\n [[28., 29., 30.],\n [31., 32., 33.]]], dtype=float32)\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|--------------------|-----------------------------------------------------------------------------------------------------------------------------|\n| `image` | Either a 3-D float Tensor of shape \\[height, width, depth\\], or a 4-D Tensor of shape \\[batch_size, height, width, depth\\]. |\n| `central_fraction` | float (0, 1\\], fraction of size to crop |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|-------------------------------------------------|\n| `ValueError` | if central_crop_fraction is not within (0, 1\\]. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| 3-D / 4-D float Tensor, as per the input. ||\n\n\u003cbr /\u003e"]]