为啥会用仿射变换实现,因为高级…
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. 仿射变换
- 先将图片平移到左上角原点,得到
Center
矩阵 - 再将图片resize到目标尺寸,得到
Resize
矩阵 - 再将图片中心由左上角原点平移到缩放后图像的中心点,得到
Translate
矩阵
最后的仿射变换矩阵M = T @ R @ C
- 有需要的话,可以再填充边缘
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()