前言
官方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]).
.. 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._
需准备的东西
演示代码
# 导入库
import torchvision.transforms as transforms
import torchvision as tv
import matplotlib.pyplot as plt
import matplotlib
# 设置字体 这两行需要手动设置
matplotlib.rcParams['font.sans-serif'] = ['SimHei']
matplotlib.rcParams['axes.unicode_minus'] = False
# 1. 中心裁剪
transform = transforms.Compose([
transforms.RandomResizedCrop(size=900, scale=(0.03, 1.8), ratio=(0.9999, 1.0), interpolation=2)
])
# 读取图片
picTensor = tv.io.read_image('testpic.png')
# 转换图片
picTransformed = transform(picTensor)
# 显示
print('图像处理之前的图片大小:', picTensor.shape)
print('图像之后的图片大小:', picTransformed.shape)
picNumpy = picTensor.permute(1, 2, 0).numpy()
plt.imshow(picNumpy)
plt.title('图像处理之前的图片')
plt.show()
picNumpy = picTransformed.permute(1, 2, 0).numpy()
plt.imshow(picNumpy)
plt.title('图像处理之后的图片')
plt.show()