前言

随机裁剪函数,具体的解释可以读一读英文的官方api,
共有5个参数,
size
padding
pad_id_needed
fill
padding_mode

官方Api介绍

_Crop the given image at a random location.
If the image is torch Tensor, it is expected
to have […, H, W] shape, where … means an arbitrary number of leading dimensions,
but if non-constant padding is used, the input is expected to have at most 2 leading dimensions

Args:
size (sequence or int): Desired output size of the crop. If size is an
int instead of sequence like (h, w), a square crop (size, size) is
made. If provided a sequence of length 1, it will be interpreted as (size[0], size[0]).
padding (int or sequence, optional): Optional padding on each border
of the image. Default is None. If a single int is provided this
is used to pad all borders. If sequence of length 2 is provided this is the padding
on left/right and top/bottom respectively. If a sequence of length 4 is provided
this is the padding for the left, top, right and bottom borders respectively.

  1. .. note::<br /> In torchscript mode padding as single int is not supported, use a sequence of<br /> length 1: ``[padding, ]``.<br /> pad_if_needed (boolean): It will pad the image if smaller than the<br /> desired size to avoid raising an exception. Since cropping is done<br /> after padding, the padding seems to be done at a random offset.<br /> fill (number or str or tuple): Pixel fill value for constant fill. Default is 0. If a tuple of<br /> length 3, it is used to fill R, G, B channels respectively.<br /> This value is only used when the padding_mode is constant.<br /> Only number is supported for torch Tensor.<br /> Only int or str or tuple value is supported for PIL Image.<br /> padding_mode (str): Type of padding. Should be: constant, edge, reflect or symmetric.<br /> Default is constant.
  2. - constant: pads with a constant value, this value is specified with fill
  3. - edge: pads with the last value at the edge of the image.<br /> If input a 5D torch Tensor, the last 3 dimensions will be padded instead of the last 2
  4. - reflect: pads with reflection of image without repeating the last value on the edge.<br /> For example, padding [1, 2, 3, 4] with 2 elements on both sides in reflect mode<br /> will result in [3, 2, 1, 2, 3, 4, 3, 2]
  5. - symmetric: pads with reflection of image repeating the last value on the edge.<br /> For example, padding [1, 2, 3, 4] with 2 elements on both sides in symmetric mode<br /> will result in [2, 1, 1, 2, 3, 4, 4, 3]_

需准备的东西

演示代码

  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.RandomCrop(size=(2100, 3174), padding=700, pad_if_needed=True, fill=180, padding_mode='constant')
  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()

图像处理之前的图片大小: torch.Size([3, 2100, 3174]) 图像之后的图片大小: torch.Size([3, 2100, 3174]) image.png image.png