MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / central_crop

Function central_crop

tensorflow/python/ops/image_ops_impl.py:621–726  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

619
620@tf_export('image.central_crop')
621def 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:

Callers

nothing calls this directly

Calls 9

_AssertAtLeast3DImageFunction · 0.85
sliceMethod · 0.80
_get_dimFunction · 0.70
name_scopeMethod · 0.45
get_shapeMethod · 0.45
formatMethod · 0.45
castMethod · 0.45
stackMethod · 0.45
set_shapeMethod · 0.45

Tested by

no test coverage detected