图片仿射变换矩阵

这里用齐次坐标系

平移

x ′ = x + t x y ′ = y + t y z ′ = z + t z \begin{aligned} x^{\prime} &= x + tx\\ y^{\prime} &= y + ty\\ z^{\prime} &= z + tz\\ \end{aligned} xyz=x+tx=y+ty=z+tz
于是可以写出
( x ′ y ′ z ′ 1 ) = T ( t x , t y , t z ) ( x y z 1 ) = ( 1 0 0 t x 0 1 0 t y 0 0 1 t z 0 0 0 1 ) ( x y z 1 ) \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} xyz1 =T(tx,ty,tz) xyz1 = 100001000010txtytz1 xyz1
容易验证,反着平移,也就是求逆
( T ( t x , t y , t z ) ) − 1 = ( 1 0 0 − t x 0 1 0 − t y 0 0 1 − t z 0 0 0 1 ) = T ( − t x , − t y , − t z ) \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= 100001000010txtytz1 =T(tx,ty,tz)

旋转

2d

绕原点逆时针旋转

x = r cos ⁡ α y = r sin ⁡ α \begin{aligned} x &= r\cos \alpha\\ y & = r\sin \alpha\\ \end{aligned} xy=rcosα=rsinα
x ′ = r cos ⁡ β = r cos ⁡ ( α + θ ) y ′ = r sin ⁡ β = r sin ⁡ ( α + θ ) \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} xy=rcosβ=rcos(α+θ)=rsinβ=rsin(α+θ)
在这里插入图片描述
展开后,容易写出
( x ′ y ′ ) = G ( x y ) = ( cos ⁡ θ − sin ⁡ θ sin ⁡ θ cos ⁡ θ ) ( x y ) \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} (xy)=G(xy)=(cosθsinθsinθcosθ)(xy)
也就是Givens旋转变换
顺时针旋转,也就是求逆(相当于逆时针旋转 − θ -\theta θ)
G − 1 = G T = ( cos ⁡ θ sin ⁡ θ − sin ⁡ θ cos ⁡ θ ) \mathbf{G}^{-1} = \mathbf{G}^T = \begin{pmatrix} \cos \theta & \sin\theta\\ -\sin\theta & \cos\theta \end{pmatrix} G1=GT=(cosθsinθsinθcosθ)
齐次坐标里
( x ′ y ′ 1 ) = ( G 0 0 T 1 ) ( x y 1 ) = ( cos ⁡ θ − sin ⁡ θ 0 sin ⁡ θ cos ⁡ θ 0 0 0 1 ) ( x y 1 ) \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} xy1 =(G0T01) xy1 = cosθsinθ0sinθcosθ0001 xy1

绕任意点逆时针旋转

先平移到原点,旋转,再平移回去
假设绕 ( c x , c y ) \left(c_x,c_y\right) (cx,cy)
( x ′ y ′ 1 ) = T ( c x , c y ) ( G 0 0 T 1 ) T ( − c x , − c y ) ( x y 1 ) = ( cos ⁡ θ − sin ⁡ θ ( 1 − cos ⁡ θ ) c x + c y sin ⁡ θ sin ⁡ θ cos ⁡ θ ( 1 − cos ⁡ θ ) c y − c x sin ⁡ θ 0 0 1 ) ( x y 1 ) \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} xy1 =T(cx,cy)(G0T01)T(cx,cy) xy1 = cosθsinθ0sinθcosθ0(1cosθ)cx+cysinθ(1cosθ)cycxsinθ1 xy1
顺时针旋转,也就是求逆(相当于逆时针旋转 − θ -\theta θ)
( T ( c x , c y ) ( G 0 0 T 1 ) T ( − c x , − c y ) ) − 1 = T ( c x , c y ) ( G T 0 0 T 1 ) T ( − c x , − c y ) = ( cos ⁡ θ sin ⁡ θ ( 1 − cos ⁡ θ ) c x − c y sin ⁡ θ − sin ⁡ θ cos ⁡ θ ( 1 − cos ⁡ θ ) c y + c x sin ⁡ θ 0 0 1 ) \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(1cosθ)cxcysinθ(1cosθ)cy+cxsinθ1

缩放

x ′ = S x x y ′ = S y y z ′ = S z z \begin{aligned} x^{\prime} &= S_xx\\ y^{\prime} &= S_yy\\ z^{\prime} &= S_zz\\ \end{aligned} xyz=Sxx=Syy=Szz
于是可以写出
( x ′ y ′ z ′ 1 ) = S ( S x , S y , S z ) ( x y z 1 ) = ( S x 0 0 0 0 S y 0 0 0 0 S z 0 0 0 0 1 ) ( x y z 1 ) \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} xyz1 =S(Sx,Sy,Sz) xyz1 = Sx0000Sy0000Sz00001 xyz1
求逆
( S ( S x , S y , S z ) ) − 1 = ( 1 S x 0 0 0 0 1 S y 0 0 0 0 1 S z 0 0 0 0 1 ) = S ( 1 S x , 1 S y , 1 S z ) \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 cv2

path = '000001163.jpg'

if __name__ == '__main__':
    img = cv2.imread(path)
    h, w = img.shape[:2]
    print(img.shape)
    angle = 30
    matrix = 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()


参考:
https://gautamnagrawal.medium.com/rotating-image-by-any-angle-shear-transformation-using-only-numpy-d28d16eb5076

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Nightmare004

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值