transforms中RandomResizedCrop、Resize、CenterCrop的理解

首先要记住,transforms只能对PIL读入的图片进行操作,而且PIL和opencv只能读取H * W * C形式的图片

transforms.RandomResizedCrop(size) : 将原图片随机裁剪出一块,再缩放成相应 (size*size) 的比例

import matplotlib.pyplot as plt
from PIL import Image
from torchvision import transforms

file_path = "./flower.jpg"
img = Image.open(file_path)
print("origin_img_size:", img.size)  # (280, 320)

trans = transforms.RandomResizedCrop(224)  # 随机裁剪,再缩放成 224×224
img1 = trans(img)
print("随机裁剪后的大小:", img1.size) # (224, 224)

plt.subplot(1, 2, 1), plt.imshow(img)
plt.subplot(1, 2, 2), plt.imshow(img1)
plt.show()

在这里插入图片描述

transforms.Resize(size):将图片的短边缩放成size的比例,然后长边也跟着缩放,使得缩放后的图片相对于原图的长宽比不变。

如果想要resize成自己想要的图片大小,可以直接使用transforms.Resize((size,size))

transforms.CenterCrop(size):从图片中心开始沿两边裁剪,裁剪后的图片大小为(size*size)

import matplotlib.pyplot as plt
from PIL import Image
from torchvision import transforms

file_path = "./flower.jpg"
img = Image.open(file_path)
print("origin_img_size:", img.size)

trans = transforms.Compose([transforms.Resize((256, 256), Image.BILINEAR),
                            transforms.CenterCrop(100)])
img1 = trans(img)
print("随机裁剪后的大小:", img1.size)
plt.subplot(1, 2, 1), plt.imshow(img)
plt.subplot(1, 2, 2), plt.imshow(img1)
plt.show()

在这里插入图片描述

  • 18
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值