赞
踩
有时候直接进行resize会有形变,所以想到这样的方式,同比例缩放,然后补0。
参考:https://www.pythonf.cn/read/144858
- class ZeroPaddingResize(object):
- """
- resize image with same scale, fill with zero padding
- """
- def __init__(self, size=(224, 224), interpolation=Image.BILINEAR):
- self.size = size
- self.interpolation = interpolation
-
- def __call__(self, img):
- ih, iw = img.size
- h, w = self.size[0], self.size[1]
- scale = min(w / iw, h / ih)
- new_w = int(iw * scale+0.5)
- new_h = int(ih * scale+0.5)
-
- img = img.resize((new_w, new_h), self.interpolation)
- new_img = Image.new('RGB', self.size, (0, 0, 0))
- new_img.paste(img, ((w-new_w)//2, (h-new_h)//2))
-
- return new_img
- import torchvision
- import ZeroPaddingResize
-
- transform_train = torchvision.transforms.Compose([
- ZeroPaddingResize((224, 224)),
- torchvision.transforms.ToTensor(),
- ])
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。