在Python opencv中图像旋转矩阵可以用来旋转源图片到目标图片、旋转源坐标点到目标坐标点。根据图像旋转矩阵还可以逆向根据目标坐标点得到原始点的位置。
将常用的旋转函数汇总一下
1. 根据中心坐标点旋转图片
# 输入为三通道图片以及需要顺时针旋转的角度
def rotate_image(image, angle):
(h, w) = image.shape[:2]
(cX, cY) = (w // 2, h // 2)
M = cv2.getRotationMatrix2D((cX, cY), angle, 1.0)
cos = np.abs(M[0, 0])
sin = np.abs(M[0, 1])
nW = int((h * sin) + (w * cos))
nH = int((h * cos) + (w * sin))
M[0, 2] += (nW / 2) - cX
M[1, 2] += (nH / 2) - cY
# perform the actual rotation and return the image
return cv2.warpAffine(image, M, (nW, nH)),M
下面是另一个类似的
def func1(image,degree):
height, width = image.shape[:2]
heightNew = int(width * fabs(sin(radians(degree))) + height * fabs(cos(radians(degree))))
widthNew = int(height * fabs(sin(radians(d