mindspore.dataset.vision.CenterCrop

View Source On Gitee
class mindspore.dataset.vision.CenterCrop(size)[source]

Crop the input image at the center to the given size. If input image size is smaller than output size, input image will be padded with 0 before cropping.

Parameters

size (Union[int, sequence]) – The output size of the cropped image. If size is an integer, a square crop of size (size, size) is returned. If size is a sequence of length 2, it should be (height, width). The size value(s) must be larger than 0.

Raises
  • TypeError – If size is not of type integer or sequence.

  • ValueError – If size is less than or equal to 0.

  • RuntimeError – If given tensor shape is not <H, W> or <…, H, W, C>.

Supported Platforms:

CPU

Examples

>>> import numpy as np
>>> import mindspore.dataset as ds
>>> import mindspore.dataset.vision as vision
>>>
>>> # Use the transform in dataset pipeline mode
>>> data = np.random.randint(0, 255, size=(1, 100, 100, 3)).astype(np.uint8)
>>> numpy_slices_dataset = ds.NumpySlicesDataset(data, ["image"])
>>>
>>> # crop image to a square
>>> transforms_list1 = [vision.CenterCrop(50)]
>>> numpy_slices_dataset = numpy_slices_dataset.map(operations=transforms_list1, input_columns=["image"])
>>> for item in numpy_slices_dataset.create_dict_iterator(num_epochs=1, output_numpy=True):
...     print(item["image"].shape, item["image"].dtype)
...     break
(50, 50, 3) uint8
>>>
>>> # crop image to portrait style
>>> numpy_slices_dataset = ds.NumpySlicesDataset(data, ["image"])
>>> transforms_list2 = [vision.CenterCrop((60, 40))]
>>> numpy_slices_dataset = numpy_slices_dataset.map(operations=transforms_list2, input_columns=["image"])
>>> for item in numpy_slices_dataset.create_dict_iterator(num_epochs=1, output_numpy=True):
...     print(item["image"].shape, item["image"].dtype)
...     break
(60, 40, 3) uint8
>>>
>>> # Use the transform in eager mode
>>> data = np.array([[0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5]], dtype=np.uint8).reshape((2, 2, 3))
>>> output = vision.CenterCrop(1)(data)
>>> print(output.shape, output.dtype)
(1, 1, 3) uint8
Tutorial Examples: