Crop the central region of the image(s). 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. -------- | | |
(image, central_fraction)
| 619 | |
| 620 | @tf_export('image.central_crop') |
| 621 | def central_crop(image, central_fraction): |
| 622 | """Crop the central region of the image(s). |
| 623 | |
| 624 | Remove the outer parts of an image but retain the central region of the image |
| 625 | along each dimension. If we specify central_fraction = 0.5, this function |
| 626 | returns the region marked with "X" in the below diagram. |
| 627 | |
| 628 | -------- |
| 629 | | | |
| 630 | | XXXX | |
| 631 | | XXXX | |
| 632 | | | where "X" is the central 50% of the image. |
| 633 | -------- |
| 634 | |
| 635 | This function works on either a single image (`image` is a 3-D Tensor), or a |
| 636 | batch of images (`image` is a 4-D Tensor). |
| 637 | |
| 638 | Args: |
| 639 | image: Either a 3-D float Tensor of shape [height, width, depth], or a 4-D |
| 640 | Tensor of shape [batch_size, height, width, depth]. |
| 641 | central_fraction: float (0, 1], fraction of size to crop |
| 642 | Usage Example: ```python >> import tensorflow as tf >> x = |
| 643 | tf.random.normal(shape=(256, 256, 3)) >> tf.image.central_crop(x, 0.5) ``` |
| 644 | |
| 645 | Raises: |
| 646 | ValueError: if central_crop_fraction is not within (0, 1]. |
| 647 | |
| 648 | Returns: |
| 649 | 3-D / 4-D float Tensor, as per the input. |
| 650 | """ |
| 651 | with ops.name_scope(None, 'central_crop', [image]): |
| 652 | image = ops.convert_to_tensor(image, name='image') |
| 653 | if central_fraction <= 0.0 or central_fraction > 1.0: |
| 654 | raise ValueError('central_fraction must be within (0, 1]') |
| 655 | if central_fraction == 1.0: |
| 656 | return image |
| 657 | |
| 658 | _AssertAtLeast3DImage(image) |
| 659 | rank = image.get_shape().ndims |
| 660 | if rank != 3 and rank != 4: |
| 661 | raise ValueError('`image` should either be a Tensor with rank = 3 or ' |
| 662 | 'rank = 4. Had rank = {}.'.format(rank)) |
| 663 | |
| 664 | # Helper method to return the `idx`-th dimension of `tensor`, along with |
| 665 | # a boolean signifying if the dimension is dynamic. |
| 666 | def _get_dim(tensor, idx): |
| 667 | static_shape = tensor.get_shape().dims[idx].value |
| 668 | if static_shape is not None: |
| 669 | return static_shape, False |
| 670 | return array_ops.shape(tensor)[idx], True |
| 671 | |
| 672 | # Get the height, width, depth (and batch size, if the image is a 4-D |
| 673 | # tensor). |
| 674 | if rank == 3: |
| 675 | img_h, dynamic_h = _get_dim(image, 0) |
| 676 | img_w, dynamic_w = _get_dim(image, 1) |
| 677 | img_d = image.get_shape()[2] |
| 678 | else: |
nothing calls this directly
no test coverage detected