仿射变换实现letterbox形式的resize

为啥会用仿射变换实现,因为高级…

1. yolov5中letterbox实现

保持比例缩放,防止变形。长边缩放
短边填充

import cv2 as cv
import numpy as np
from typing import Optional, Tuple
import matplotlib.pyplot as plt


def letterbox(img, new_shape=(640, 640), color=(114, 114, 114), auto=False, scaleFill=False, scaleup=True, stride=32):
    # Resize and pad image while meeting stride-multiple constraints
    shape = img.shape[:2]  # current shape [height, width]
    if isinstance(new_shape, int):
        new_shape = (new_shape, new_shape)

    # Scale ratio (new / old)
    r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])
    if not scaleup:  # only scale down, do not scale up (for better test mAP)
        r = min(r, 1.0)

    # Compute padding
    ratio = r, r  # width, height ratios
    new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))
    dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1]  # wh padding
    if auto:  # minimum rectangle
        dw, dh = np.mod(dw, stride), np.mod(dh, stride)  # wh padding
    elif scaleFill:  # stretch
        dw, dh = 0.0, 0.0
        new_unpad = (new_shape[1], new_shape[0])
        ratio = new_shape[1] / shape[1], new_shape[0] / shape[0]  # width, height ratios

    dw /= 2  # divide padding into 2 sides
    dh /= 2
    dw = int(dw)
    dh = int(dh)
    if shape[::-1] != new_unpad:  # resize
        img = cv.resize(img, new_unpad, interpolation=cv.INTER_LINEAR)
    top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))
    left, right = int(round(dw - 0.1)), int(round(dw + 0.1))
    img = cv.copyMakeBorder(img, top, bottom, left, right, cv.BORDER_CONSTANT, value=color)  # add border
    return img, ratio[0], (dw, dh)  # letterbox默认等比缩放,取一个值就可
img = cv.imread('./test_img_xml/1.jpg')
dst = letterbox(img)[0]

plt.figure(1)
plt.imshow(img[:, :, ::-1], cmap='gray')
plt.figure(2)
plt.imshow(dst[:, :, ::-1], cmap='gray')
plt.show()

在这里插入图片描述

2. 仿射变换

  1. 先将图片平移到左上角原点,得到Center矩阵
  2. 再将图片resize到目标尺寸,得到Resize矩阵
  3. 再将图片中心由左上角原点平移到缩放后图像的中心点,得到Translate矩阵
    最后的仿射变换矩阵 M = T @ R @ C
  4. 有需要的话,可以再填充边缘
    在这里插入图片描述
img = cv.imread('./test_img_xml/1.jpg')
h0, w0 = img.shape[:2]
print(w0, h0)

C = np.eye(3)
C[0, 2] = -w0/2
C[1, 2] = -h0/2
dst_shape = get_minimum_dst_shape((w0, h0), (640, 640))
w1, h1 = dst_shape
print(dst_shape)
R = get_resize_matrix((w0, h0), (w1, h1))
print(R)
T = np.eye(3)
T[0, 2] = w1/2
T[1, 2] = h1/2

M = T @ R @ C
dst = cv.warpAffine(img, M[:2], (w1, h1))


padh = int((640-h1)/2)
padw = int((640 - w1)/2)
dst2 = cv.copyMakeBorder(dst, padh, padh, padw, padw, cv.BORDER_CONSTANT, value=(114, 114, 114))
plt.figure(1)
plt.imshow(img[:, :, ::-1], cmap='gray')
plt.figure(2)
plt.imshow(dst[:, :, ::-1], cmap='gray')
plt.figure(3)
plt.imshow(dst2[:, :, ::-1], cmap='gray')
plt.show()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值