51,411
社区成员
发帖
与我相关
我的任务
分享
def opencv_rotate(img, angle):
h, w = img.shape[:2]
center = (w / 2, h / 2)
scale = 1.0
# 2.1获取M矩阵
"""
M矩阵
[
cosA -sinA (1-cosA)*centerX+sinA*centerY
sinA cosA -sinA*centerX+(1-cosA)*centerY
]
"""
M = cv2.getRotationMatrix2D(center, angle, scale)
# 2.2 新的宽高,radians(angle) 把角度转为弧度 sin(弧度)
new_H = int(w * fabs(sin(radians(angle))) + h * fabs(cos(radians(angle))))
new_W = int(h * fabs(sin(radians(angle))) + w * fabs(cos(radians(angle))))
# 2.3 平移
M[0, 2] += (new_W - w) / 2
M[1, 2] += (new_H - h) / 2
rotate = cv2.warpAffine(img, M, (new_W, new_H), borderValue=(0, 0, 0))
return rotate
如上转成java代码
链接地址:图像旋转:getRotationMatrix2D详解--无损失旋转图片 - 云+社区 - 腾讯云 (tencent.com)