图片仿射变换矩阵
创始人
2024-06-01 12:09:56
0

这里用齐次坐标系

平移

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​​=​1000​0100​0010​txtytz1​​xyz1​
容易验证,反着平移,也就是求逆
(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=​1000​0100​0010​−tx−ty−tz1​​=T(−tx,−ty,−tz)

旋转

2d

绕原点逆时针旋转

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​​=(G0T​01​)​xy1​​=​cosθsinθ0​−sinθcosθ0​001​​xy1​

绕任意点逆时针旋转

先平移到原点,旋转,再平移回去
假设绕(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​)(G0T​01​)T(−cx​,−cy​)​xy1​​=​cosθsinθ0​−sinθcosθ0​(1−cosθ)cx​+cy​sinθ(1−cosθ)cy​−cx​sinθ1​​xy1​​​
顺时针旋转,也就是求逆(相当于逆时针旋转−θ-\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​)(G0T​01​)T(−cx​,−cy​))−1​=T(cx​,cy​)(GT0T​01​)T(−cx​,−cy​)=​cosθ−sinθ0​sinθcosθ0​(1−cosθ)cx​−cy​sinθ(1−cosθ)cy​+cx​sinθ1​​​

缩放

x′=Sxxy′=Syyz′=Szz\begin{aligned} x^{\prime} &= S_xx\\ y^{\prime} &= S_yy\\ z^{\prime} &= S_zz\\ \end{aligned} x′y′z′​=Sx​x=Sy​y=Sz​z​
于是可以写出
(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​​=​Sx000​0Sy00​00Sz0​0001​​xyz1​
求逆
(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=​Sx1​000​0Sy1​00​00Sz1​0​0001​​=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()

相关内容

热门资讯

东方航空×泡泡玛特:DIMOO... 文旅观察近日,中国东方航空与泡泡玛特携手推出的DIMOO中泰建交50周年限定主题彩绘专机正式首航,执...
多个“第一”!重大突破! 今天(7月9日)上午国新办举行首场“高质量完成‘十四五’规划”系列主题新闻发布会一起看 ↓国家发展改...
中广核新能源:6月发电量140... 格隆汇7月9日|中广核新能源(1811.HK)公告,6月本集团按合并报表口径完成发电量1,403.5...
1.52亿千瓦!江苏电网最高用... 转自:上观新闻7月7日,江苏电网最高用电负荷,年内第三次刷新历史新高,达1.52亿千瓦。截至目前,南...
ST华铭因信披违规被罚,收购聚... 近日,ST华铭(维权)(300462)发布公告,近日收到中国证券监督管理委员会上海监管局下发的《行政...
山西泽辰医药乌帕替尼缓释片启动... 药物临床试验登记与信息公示平台数据显示,山西泽辰医药科技有限公司的乌帕替尼缓释片在健康受试者中随机、...
西海都市报公布地址、订阅及零售... 本报地址:西宁市长江路5号 广告经营许可证: 广告部电话:6125601 排版:西海都市报社编辑部 ...
台风丹娜丝“绘就”陆家嘴诗意画... 转自:上观新闻连日来,受台风“丹娜丝”外围环流持续影响,上海晴雨相间,同时带来降温、大风,云雾光影间...
国家发改委:中国做的这件事,全... 转自:北京日报客户端在国新办今天(9日)举行的首场“高质量完成‘十四五’规划”系列主题新闻发布会上,...
或受欧盟法规影响,苹果将在欧盟...   炒股就看金麒麟分析师研报,权威,专业,及时,全面,助您挖掘潜力主题机会! IT之家 7 月 9...