数据增强之仿射变换(旋转图像)

图片仿射变换需要用到一些线性代数的知识,以下为参考链接。
最基本的变换
几何变换
矩阵分析:空间变换与基变换
矩阵分析:从特征值到奇异值

图片发生仿射变换,对应的坐标点标注也要进行响应的变换。核心代码如下

#图片进行变换
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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值