这里用齐次坐标系
x′=x+txy′=y+tyz′=z+tz\begin{aligned} x^{\prime} &= x + tx\\ y^{\prime} &= y + ty\\ z^{\prime} &= z + tz\\ \end{aligned} x′y′z′=x+tx=y+ty=z+tz
于是可以写出
(x′y′z′1)=T(tx,ty,tz)(xyz1)=(100tx010ty001tz0001)(xyz1)\begin{pmatrix} x^{\prime}\\ y^{\prime}\\ z^{\prime}\\ 1\\ \end{pmatrix} = \mathbf{T}\left(tx,ty,tz\right) \begin{pmatrix} x\\ y\\ z\\ 1\\ \end{pmatrix}=\begin{pmatrix} 1&0&0&tx\\ 0&1&0&ty\\ 0&0&1&tz\\ 0&0&0&1 \end{pmatrix}\begin{pmatrix} x\\ y\\ z\\ 1\\ \end{pmatrix} x′y′z′1=T(tx,ty,tz)xyz1=100001000010txtytz1xyz1
容易验证,反着平移,也就是求逆
(T(tx,ty,tz))−1=(100−tx010−ty001−tz0001)=T(−tx,−ty,−tz)\left(\mathbf{T}\left(tx,ty,tz\right)\right)^{-1} = \begin{pmatrix} 1&0&0&-tx\\ 0&1&0&-ty\\ 0&0&1&-tz\\ 0&0&0&1 \end{pmatrix} = \mathbf{T}\left(-tx,-ty,-tz\right) (T(tx,ty,tz))−1=100001000010−tx−ty−tz1=T(−tx,−ty,−tz)
x=rcosαy=rsinα\begin{aligned} x &= r\cos \alpha\\ y & = r\sin \alpha\\ \end{aligned} xy=rcosα=rsinα
x′=rcosβ=rcos(α+θ)y′=rsinβ=rsin(α+θ)\begin{aligned} x^{\prime}&= r\cos \beta= r\cos \left(\alpha + \theta\right)\\ y^{\prime}&= r\sin \beta= r\sin\left(\alpha + \theta\right)\\ \end{aligned} x′y′=rcosβ=rcos(α+θ)=rsinβ=rsin(α+θ)
展开后,容易写出
(x′y′)=G(xy)=(cosθ−sinθsinθcosθ)(xy)\begin{pmatrix} x^{\prime}\\ y^{\prime}\\ \end{pmatrix}=\mathbf{G}\begin{pmatrix} x\\ y\\ \end{pmatrix} = \begin{pmatrix} \cos \theta & -\sin\theta\\ \sin\theta & \cos\theta \end{pmatrix}\begin{pmatrix} x\\ y\\ \end{pmatrix} (x′y′)=G(xy)=(cosθsinθ−sinθcosθ)(xy)
也就是Givens旋转变换
顺时针旋转,也就是求逆(相当于逆时针旋转−θ-\theta−θ)
G−1=GT=(cosθsinθ−sinθcosθ)\mathbf{G}^{-1} = \mathbf{G}^T = \begin{pmatrix} \cos \theta & \sin\theta\\ -\sin\theta & \cos\theta \end{pmatrix} G−1=GT=(cosθ−sinθsinθcosθ)
齐次坐标里
(x′y′1)=(G00T1)(xy1)=(cosθ−sinθ0sinθcosθ0001)(xy1)\begin{pmatrix} x^{\prime}\\ y^{\prime}\\ 1\\ \end{pmatrix}=\begin{pmatrix} \mathbf{G} &\mathbf{0}\\ \mathbf{0}^T&1\\ \end{pmatrix}\begin{pmatrix} x\\ y\\ 1\\ \end{pmatrix} = \begin{pmatrix} \cos \theta & -\sin\theta & 0\\ \sin\theta & \cos\theta & 0\\ 0 & 0 & 1\\ \end{pmatrix}\begin{pmatrix} x\\ y\\ 1\\ \end{pmatrix} x′y′1=(G0T01)xy1=cosθsinθ0−sinθcosθ0001xy1
先平移到原点,旋转,再平移回去
假设绕(cx,cy)\left(c_x,c_y\right)(cx,cy)
(x′y′1)=T(cx,cy)(G00T1)T(−cx,−cy)(xy1)=(cosθ−sinθ(1−cosθ)cx+cysinθsinθcosθ(1−cosθ)cy−cxsinθ001)(xy1)\begin{aligned} \begin{pmatrix} x^{\prime}\\ y^{\prime}\\ 1\\ \end{pmatrix} &= \mathbf{T}\left(c_x,c_y\right)\begin{pmatrix} \mathbf{G} &\mathbf{0}\\ \mathbf{0}^T&1\\ \end{pmatrix}\mathbf{T}\left(-c_x,-c_y\right)\begin{pmatrix} x\\ y\\ 1\\ \end{pmatrix}\\ &= \begin{pmatrix} \cos \theta & -\sin\theta & \left(1-\cos\theta\right)c_x + c_y \sin \theta\\ \sin\theta & \cos\theta & \left(1-\cos\theta\right)c_y - c_x\sin\theta\\ 0 & 0 & 1\\ \end{pmatrix}\begin{pmatrix} x\\ y\\ 1\\ \end{pmatrix} \end{aligned} x′y′1=T(cx,cy)(G0T01)T(−cx,−cy)xy1=cosθsinθ0−sinθcosθ0(1−cosθ)cx+cysinθ(1−cosθ)cy−cxsinθ1xy1
顺时针旋转,也就是求逆(相当于逆时针旋转−θ-\theta−θ)
(T(cx,cy)(G00T1)T(−cx,−cy))−1=T(cx,cy)(GT00T1)T(−cx,−cy)=(cosθsinθ(1−cosθ)cx−cysinθ−sinθcosθ(1−cosθ)cy+cxsinθ001)\begin{aligned} \left(\mathbf{T}\left(c_x,c_y\right)\begin{pmatrix} \mathbf{G} &\mathbf{0}\\ \mathbf{0}^T&1\\ \end{pmatrix}\mathbf{T}\left(-c_x,-c_y\right)\right)^{-1} &= \mathbf{T}\left(c_x,c_y\right)\begin{pmatrix} \mathbf{G}^T &\mathbf{0}\\ \mathbf{0}^T&1\\ \end{pmatrix}\mathbf{T}\left(-c_x,-c_y\right)\\ &=\begin{pmatrix} \cos \theta & \sin\theta & \left(1-\cos\theta\right)c_x - c_y \sin \theta\\ -\sin\theta & \cos\theta & \left(1-\cos\theta\right)c_y + c_x\sin\theta\\ 0 & 0 & 1\\ \end{pmatrix} \end{aligned} (T(cx,cy)(G0T01)T(−cx,−cy))−1=T(cx,cy)(GT0T01)T(−cx,−cy)=cosθ−sinθ0sinθcosθ0(1−cosθ)cx−cysinθ(1−cosθ)cy+cxsinθ1
x′=Sxxy′=Syyz′=Szz\begin{aligned} x^{\prime} &= S_xx\\ y^{\prime} &= S_yy\\ z^{\prime} &= S_zz\\ \end{aligned} x′y′z′=Sxx=Syy=Szz
于是可以写出
(x′y′z′1)=S(Sx,Sy,Sz)(xyz1)=(Sx0000Sy0000Sz00001)(xyz1)\begin{pmatrix} x^{\prime}\\ y^{\prime}\\ z^{\prime}\\ 1\\ \end{pmatrix} = \mathbf{S}\left(Sx,Sy,Sz\right) \begin{pmatrix} x\\ y\\ z\\ 1\\ \end{pmatrix}=\begin{pmatrix} Sx&0&0&0\\ 0&Sy&0&0\\ 0&0&Sz&0\\ 0&0&0&1 \end{pmatrix}\begin{pmatrix} x\\ y\\ z\\ 1\\ \end{pmatrix} x′y′z′1=S(Sx,Sy,Sz)xyz1=Sx0000Sy0000Sz00001xyz1
求逆
(S(Sx,Sy,Sz))−1=(1Sx00001Sy00001Sz00001)=S(1Sx,1Sy,1Sz)\left(\mathbf{S}\left(Sx,Sy,Sz\right)\right)^{-1} =\begin{pmatrix} \frac{1}{Sx}&0&0&0\\ 0&\frac{1}{Sy}&0&0\\ 0&0&\frac{1}{Sz}&0\\ 0&0&0&1 \end{pmatrix} =\mathbf{S}\left(\frac{1}{Sx},\frac{1}{Sy},\frac{1}{Sz}\right) (S(Sx,Sy,Sz))−1=Sx10000Sy10000Sz100001=S(Sx1,Sy1,Sz1)
#!/usr/bin/env python
# _*_ coding:utf-8 _*_
import numpy as np
from cv2 import cv2path = '000001163.jpg'if __name__ == '__main__':img = cv2.imread(path)h, w = img.shape[:2]print(img.shape)angle = 30matrix = cv2.getRotationMatrix2D((w // 2, h // 2), angle, 1)rad = np.deg2rad(angle)cosine = np.cos(rad)sine = np.sin(rad)new_h = int(np.round(np.abs(h * cosine) + np.abs(w * sine)))new_w = int(np.round(np.abs(w * cosine) + np.abs(h * sine)))print(matrix)dst = cv2.warpAffine(img, matrix, (new_w, new_h))print(dst.shape)cv2.imshow("src", img)cv2.imshow("dst", dst)cv2.waitKey()cv2.destroyAllWindows()