cv::Mat cv::getRotationMatrix2D( Point2f center, double angle, double scale )
{ angle *= CV_PI/180; double alpha = cos(angle)*scale; double beta =
sin(angle)*scale; Mat M(2, 3, CV_64F); double* m = M.ptr<double>(); m[0] =
alpha; m[1] = beta; m[2] = (1-alpha)*center.x - beta*center.y; m[3] = -beta;
m[4] = alpha; m[5] = beta*center.x + (1-alpha)*center.y; return M; }

1、有图有真相

2、任务描述

3、开始计算

AC由AB旋转得到，故其长度相等，令AC=AB=L

x'=cx+L*cos(α-θ)=cx+L*(cos(α)*cos(θ)+sin(α)*sin(θ))     1式

y'=cy+L*sin(α-θ)=cy+L*(sin(a)*cos(θ)-sin(θ)*cos(a))       2式

cos(α)=(x-cx)/L
3式

sin(a)=(y-cy)/L
4式

x'=cx+(x-cx)*cos(θ)+(y-cy)*sin(θ)=x*cos(θ)+y*sin(θ)+cx*(1-cos(θ))-cy*sin(θ)

y'=cy+(y-cy)*cos(θ)-(x-cx)*sin(θ)=x*(-sin(θ))+y*cos(θ)+cy*(1-cos(θ))+cx*sin(θ)

[

[cos(θ),sin(θ),cx*(1-cos(θ))-cy*sin(θ)],

[-sin(θ),cos(θ),cy*(1-cos(θ))+cx*sin(θ)]

]

import numpy as np import cv2 as cv import math cx = 250 cy = 250 angle = 45
scale = 1.0 opencv_M = cv.getRotationMatrix2D((cx, cy), angle, scale)
print(f"opencv_M=\n{opencv_M}") print("=================================")
cos_angle = math.cos(angle / 180.0 * math.pi) sin_angle = math.sin(angle /
180.0 * math.pi) self_M = np.array([ [cos_angle, sin_angle, cx * (1 -
cos_angle) - cy * sin_angle], [-sin_angle, cos_angle, cy * (1 - cos_angle) + cx
* sin_angle] ]) print(f"self_M=\n{self_M}")

python中math函数库的三角函数输入参数，是转换成多少pi，而不是直接输入一个角度，（准确地说，它接受的是弧度）

GitHub

Gitee