在图像识别任务中,图像旋转是一种比较常用的数据增强方法。结合cv2
的cv2.getRotationMatrix2D()
和cv2.warpAffine()
就可以实现图像的旋转。
对于像目标检测这类带标注框的图像识别任务,不仅要对图像旋转,还要知道标注框被旋转到哪里了,所以还要计算坐标点的变化。下面的函数实现了:
- 给定角度,旋转图像。
- 自动调整图像尺寸,保证原图像的内容不会被旋转到图像外。
- 计算原图中坐标点points在旋转后的图像中的位置坐标。
import numpy as np
import cv2
from math import fabs, sin, radians, cos
def rotate_with_points(image, degree, points, fill=0):
"""逆时针旋转图像image角度degree,并计算原图中坐标点points在旋转后的图像中的位置坐标.
Args:
image: 图像数组
degree: 旋转角度
points (np.array): ([x, ...], [y, ...]), shape=(2, m),原图上的坐标点
fill: 旋转图像后,空白处填充的颜色,默认填0(黑色)
Return:
new_img: 旋转后的图像
new_pts: 原图中坐标点points在旋转后的图像中的位置坐标
"""
h, w = image.shape
new_h = int(w * fabs(sin(radians(degree))) + h * fabs(cos(radians(degree))))
new_w = int(h * fabs(sin(radians(degree))) + w * fabs(cos(radians(degree))))
rtt_mat = cv2.getRotationMatrix2D((w / 2, h / 2), degree, 1)
rtt_mat[0, 2] += (new_w - w) / 2
rtt_mat[1, 2] += (new_h - h) / 2
new_img = cv2.warpAffine(image, rtt_mat, (new_w, new_h), borderValue=fill)
a = np.array([rtt_mat[0][: 2], rtt_mat[1][: 2]])
b = np.array([[rtt_mat[0][2]], [rtt_mat[1][2]]])
new_pts = np.round(np.dot(a, points.astype(np.float32)) + b).astype(np.int64)
return new_img, new_pts