spyrit.core.torch.center_crop

spyrit.core.torch.center_crop(img: tensor, out_shape: tuple, in_shape: tuple = None) tensor[source]

Crops the center of an image to the specified shape.

This function uses the torchvision.transforms.CenterCrop class to crop the center of an image to the specified shape. This function can however crop images that are vectorized (flattened, 1D) by specifying the input shape.

Args:

img (torch.tensor): Image to crop. If the image is vectorized, the input shape must be specified.

out_shape (tuple): Shape of the output image after cropping. Must be a tuple of two integers (height, width).

in_shape (tuple, optional): Shape of the input image, must be specified if and only if the input image is vectorized. Must be a tuple of two integers (height, width). If None, the input is supposed to be a 2D image. Defaults to None.

Returns:

torch.tensor: Cropped image. It has the same number of dimensions as the input image.