图片仿射变换需要用到一些线性代数的知识,以下为参考链接。
最基本的变换
几何变换
矩阵分析:空间变换与基变换
矩阵分析:从特征值到奇异值
图片发生仿射变换,对应的坐标点标注也要进行响应的变换。核心代码如下
#图片进行变换
rotMat = cv2.getRotationMatrix2D(center, angle, 1.0)
img = cv2.warpAffine(img, rotMat, (width, height))
#坐标点标注进行变换
R = rotMat[:, : 2]
W = np.array([rotMat[0][2], rotMat[1][2]])
coor = np.dot(R, coor) + W
完整的代码如下
import random
import cv2
import numpy as np
import matplotlib.pyplot as plt
num_class = 17
a = {'unit': {'num_keypoints': 14, 'keypoints': [368, 61, 1, 369, 52, 2, 0, 0, 0, 382, 48, 2, 0, 0, 0, 368, 84, 2, 435, 81, 2, 362, 125, 2, 446, 125, 2, 360, 153, 2, 0, 0, 0, 397, 167, 1, 439, 166, 1, 369, 193, 2, 461, 234, 2, 361, 246, 2, 474, 287, 2], 'GT_bbox': [339, 22, 493, 322]}, 'imgInfo': {'imgID': 391895, 'img_paths': '000000391895.jpg'}, 'operation': 0}
image_name = a['imgInfo']['img_paths']
points = np.array(a['unit']['keypoints']).reshape(num_class, 3).astype(np.float32)
gt_bbox = a['unit']['GT_bbox']
symmetry = [(1, 2), (3, 4), (5, 6), (7, 8), (9, 10), (11, 12), (13, 14), (15, 16)]
img = cv2.imread(image_name)
height, width = img.shape[0], img.shape[1]
center = (width / 2., height / 2.)
rot_factor = 45
angle = random.uniform(0, rot_factor)
print(angle)
if random.randint(0, 1):
angle *= -1
rotMat = cv2.getRotationMatrix2D(center, angle, 1.0)
img = cv2.warpAffine(img, rotMat, (width, height))
label = points
n=17
allc = []
for i in range(n):
x, y = label[i][0], label[i][1]
v = label[i][2]
coor = np.array([x, y])
if x >= 0 and y >= 0:
R = rotMat[:, : 2]
W = np.array([rotMat[0][2], rotMat[1][2]])
coor = np.dot(R, coor) + W
allc.append(int(coor[0]))
allc.append(int(coor[1]))
v *= ((coor[0] >= 0) & (coor[0] < width) & (coor[1] >= 0) & (coor[1] < height))
allc.append(int(v))
label = np.array(allc).reshape(n, 3).astype(np.int)
plt.imshow(img)
plt.show()