前言

官方Api介绍

_Crop a random portion of image and resize it to a given size.

If the image is torch Tensor, it is expected
to have […, H, W] shape, where … means an arbitrary number of leading dimensions

A crop of the original image is made: the crop has a random area (H * W)
and a random aspect ratio. This crop is finally resized to the given
size. This is popularly used to train the Inception networks.

Args:
size (int or sequence): expected output size of the crop, for each edge. If size is an
int instead of sequence like (h, w), a square output size (size, size) is
made. If provided a sequence of length 1, it will be interpreted as (size[0], size[0]).

  1. .. note::<br /> In torchscript mode size as single int is not supported, use a sequence of length 1: ``[size, ]``.<br /> scale (tuple of float): Specifies the lower and upper bounds for the random area of the crop,<br /> before resizing. The scale is defined with respect to the area of the original image.<br /> ratio (tuple of float): lower and upper bounds for the random aspect ratio of the crop, before<br /> resizing.<br /> interpolation (InterpolationMode): Desired interpolation enum defined by<br /> :class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.BILINEAR``.<br /> If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.BILINEAR`` and<br /> ``InterpolationMode.BICUBIC`` are supported.<br /> For backward compatibility integer values (e.g. ``PIL.Image.NEAREST``) are still acceptable._

需准备的东西

演示代码

  1. # 导入库
  2. import torchvision.transforms as transforms
  3. import torchvision as tv
  4. import matplotlib.pyplot as plt
  5. import matplotlib
  6. # 设置字体 这两行需要手动设置
  7. matplotlib.rcParams['font.sans-serif'] = ['SimHei']
  8. matplotlib.rcParams['axes.unicode_minus'] = False
  9. # 1. 中心裁剪
  10. transform = transforms.Compose([
  11. transforms.RandomResizedCrop(size=900, scale=(0.03, 1.8), ratio=(0.9999, 1.0), interpolation=2)
  12. ])
  13. # 读取图片
  14. picTensor = tv.io.read_image('testpic.png')
  15. # 转换图片
  16. picTransformed = transform(picTensor)
  17. # 显示
  18. print('图像处理之前的图片大小:', picTensor.shape)
  19. print('图像之后的图片大小:', picTransformed.shape)
  20. picNumpy = picTensor.permute(1, 2, 0).numpy()
  21. plt.imshow(picNumpy)
  22. plt.title('图像处理之前的图片')
  23. plt.show()
  24. picNumpy = picTransformed.permute(1, 2, 0).numpy()
  25. plt.imshow(picNumpy)
  26. plt.title('图像处理之后的图片')
  27. plt.show()