基于PIL的Image库的yolo版本的图像resize策略(包含bbox的变换)

import torch
import random
from PIL import Image

def resize(img, boxes, size, random_interpolation=False):
    w, h = img.size
    target_w, target_h = size
    scale = min(float(target_w)/float(w), float(target_h)/float(h))

    nw = int(w*scale)
    nh = int(h*scale)

    method = random.choice([
        Image.BOX,
        Image.NEAREST,
        Image.HAMMING,
        Image.BICUBIC,
        Image.LANCZOS,
        Image.BILINEAR]) if random_interpolation else Image.BILINEAR

    img = img.resize((nw, nh), method)
    new_img = Image.new('RGB', size, (128,128,128))             # 创建一副灰色画布
    new_img.paste(img, ((target_w-nw)//2, (target_h-nh)//2))    # 将缩放后的图片粘到画布的中央
   
    sw = float(nw)/float(w)
    sh = float(nh)/float(h)
    if boxes is not None:
        boxes = boxes * torch.Tensor([sw,sh,sw,sh])
        boxes[:, 1] = boxes[:, 1] + (target_h-nh)//2
        boxes[:, 3] = boxes[:, 3] + (target_h-nh)//2

    return img, boxes

 

你可能感兴趣的:(基于PIL的Image库的yolo版本的图像resize策略(包含bbox的变换))