前面我们提到了图像的缩放变换,可以用矩阵乘法的形式来表达变换后的像素位置映射关系。
那么,对于旋转变换呢?我们可以同样将其想象成二维平面上矢量的旋转。如下图所示,矢量 [ x 1 , y 1 ] [x_1,y_1] [x1,y1]逆时针旋转 θ θ θ度到了 [ x 2 , y 2 ] [x_2,y_2] [x2,y2]。
设定矢量的长度为s,根据坐标系定义,我们可以得到:
x
2
=
s
•
c
o
s
β
y
2
=
s
•
s
i
n
β
\begin{aligned} x2=s• cosβ\\ y2=s• sinβ \end{aligned}
x2=s•cosβy2=s•sinβ
根据上面的图形,有:
β
=
α
+
θ
β=α+θ
β=α+θ
因此:
x
2
=
s
•
c
o
s
(
α
+
θ
)
y
2
=
s
•
s
i
n
(
α
+
θ
)
x2=s• cos(α+θ) \\ y2=s• sin(α+θ)
x2=s•cos(α+θ)y2=s•sin(α+θ)
根据初中所学的三角函数公式:
s
i
n
(
α
+
θ
)
=
s
i
n
α
•
c
o
s
θ
+
c
o
s
α
•
s
i
n
θ
c
o
s
(
α
+
θ
)
=
c
o
s
α
•
c
o
s
θ
−
s
i
n
α
•
s
i
n
θ
sin(α+θ)=sinα•cosθ+cosα•sinθ \\ cos(α+θ)=cosα•cosθ-sinα•sinθ
sin(α+θ)=sinα•cosθ+cosα•sinθcos(α+θ)=cosα•cosθ−sinα•sinθ
于是:
x
2
=
s
•
c
o
s
α
•
c
o
s
θ
−
s
•
s
i
n
α
•
s
i
n
θ
y
2
=
s
•
s
i
n
α
•
c
o
s
θ
+
s
•
c
o
s
α
•
s
i
n
θ
x2=s•cosα•cosθ-s•sinα•sinθ\\ y2=s•sinα•cosθ+s•cosα•sinθ
x2=s•cosα•cosθ−s•sinα•sinθy2=s•sinα•cosθ+s•cosα•sinθ
由于:
x
1
=
s
•
c
o
s
α
y
1
=
s
•
s
i
n
α
x1=s• cosα\\ y1=s• sinα
x1=s•cosαy1=s•sinα
因此:
x
2
=
x
1
•
c
o
s
θ
−
y
1
•
s
i
n
θ
y
2
=
x
1
•
s
i
n
θ
+
y
1
•
c
o
s
θ
x2=x1•cosθ-y1•sinθ\\ y2=x1•sinθ+y1•cosθ
x2=x1•cosθ−y1•sinθy2=x1•sinθ+y1•cosθ
于是,上式写成矩阵乘法的形式如下:
[
x
2
y
2
]
=
[
c
o
s
θ
−
s
i
n
θ
s
i
n
θ
c
o
s
θ
]
[
x
1
y
1
]
\begin{aligned} { \left[ \begin{array}{ccc} x2\\ y2\\ \end{array} \right ]}={ \left[ \begin{array}{ccc} cosθ& -sinθ\\ sinθ & cosθ\\ \end{array} \right ]}{ \left[ \begin{array}{ccc} x1\\ y1\\ \end{array} \right ]} \end{aligned}
[x2y2]=[cosθsinθ−sinθcosθ][x1y1]
我们来看看一个图像逆时针旋转180度的情况。
import cv2
import numpy as np
import math
lenna = cv2.imread("lenna256.png", 0)
row, col = lenna.shape
lenna_rotation = np.zeros_like(lenna)
A = np.mat([[math.cos(math.pi), -math.sin(math.pi)], [math.sin(math.pi), math.cos(math.pi)]])
for r in range(row):
for l in range(col):
v = np.dot(A.I, np.array([r, l]).T)
lenna_rotation[r, l] = lenna[int(v[0, 0]), int(v[0, 1])]
cv2.imshow("lenna", lenna)
cv2.imshow("rotation", lenna_rotation)
cv2.waitKey()
上面的图像宽度和高度是一样的,而且旋转角度是180度,比较特殊。在一般情况下,我们需要注意的是2点:一是旋转图像一般要将旋转中心设置在图像的中心点位置;二是图像旋转后,可能越过了原来的图像边界范围。这些都比较麻烦,好在opencv已经做好了这一切。
lenna = cv2.imread("lenna256.png", 0)
row, col = lenna.shape
M = cv2.getRotationMatrix2D((col // 2, row // 2), 70, 0.5)
dst = cv2.warpAffine(lenna, M, (col, row))
cv2.imshow("rotation", dst)
cv2.waitKey()
转载自:https://blog.csdn.net/saltriver/article/details/79680189