How to crop an image at random location in PyTorch
In this article, we will discuss how to pad an image on all sides in PyTorch.
Cropping is a technique of removal of unwanted outer areas from an image to achieve this we use a method in python that is torchvision.transforms.RandomCrop(). It is used to crop an image at a random location in PyTorch. This method accepts images like PIL Image and Tensor Image. The tensor image is a PyTorch tensor with [C, H, W] shape, where C represents a number of channels and H, W represents height and width respectively.
- size: Desired crop size of the image.
Return: it returns the cropped image of given input size.
Image used for demonstration:
In this example, we are transforming the image with a height of 200 and a width of 400.
In this example, we are transforming the image at the center. In this, we will get a square image as output.