图片仿射变换矩阵
创始人
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()

相关内容

热门资讯

今天,宜爬“山”! 转自:珠海发布大家都说,父爱如山,母爱又何尝不是!它以山的姿态站在我们的人生坐标里,它用山的沉默垒出...
汽车车门把手将迎来国家标准,释... 转自:新华网  新华社北京5月10日电 题:汽车车门把手将迎来国家标准,释放什么信号?  新华社记者...
10日付费短剧热度大盘5650... 来源:新腕儿今天发布的是昨天(5月10日)短剧付费大盘日榜,整体大盘热度值为5650万。特别声明:以...
漂泊 53 年,苏联金星探测器... IT之家 5 月 11 日消息,俄罗斯国家航天集团宣布,近地空间危险情况自动预警系统监测显示,宇宙-...
加州州长再就关税批评特朗普政府 转自:海客新闻 【#加州州长再就关税批评特朗普政府#】#...
都有数|玩儿着就能挣钱?国家认... 转自:北京日报客户端人社部日前公示拟新增无人机群飞行规划员、装修管家等17个新职业,以及黄金鉴定估价...
我爱你,不止今天! 咿呀学语时当我第一次模糊地喊出“妈妈”你尚且青涩的眼眸中亮起柔软的光长大离家后一声“妈妈”成了电话里...
焦点访谈 | 突围拓展多元市场... 来源:央视网 央视网消息(焦点访谈):当前我国的外贸行业正面临前所未有的挑战。美国肆意挥舞关税大棒,...
雨水按下暂停键 阳光重返天气舞...     昨日19时许,南宁城区迎来降雨,体感稍凉。    本报记者宋延康 摄  本报讯(记者赵金玲)...
科普大篷车进乡村 科技体验零距...     科技辅导员为学生讲授科技知识。    本报通讯员肖重虎 摄  本报讯(记者凌剑伊 通讯员蒙蓓...
镇江人才吸引力升至全国37位 金山网讯 5月9日,智联招聘旗下专业研究机构智联研究院联合“泽平宏观”正式发布《中国城市人才吸引力排...
【加快落实“五大任务” 推动高... 来源:内蒙古新闻网-《内蒙古日报  5月7日,在位于乌兰察布市四子王旗忽鸡图乡小东营村灌木林退化修复...
时政微纪录丨习主席的俄罗斯时间 来源:央视新闻客户端 应俄罗斯联邦总统普京邀请,国家主席...
常见故障100%识别 我国货运... 记者今天(11日)从国家能源集团获悉,我国货运铁路首套智能巡检机器人在河北沧州投用,这对促进我国铁路...
中共太原市委组织部公示 现对市委研究拟任职干部予以公示。郭强,男,汉族,1981年10月生,在职大学,中共党员,现任太原物产...
苏丹一州首府遭无人机袭击,20... 苏丹政府当地时间10日说,苏丹北科尔多凡州首府欧拜伊德市当天遭无人机袭击,造成20名囚犯死亡。苏丹文...
陈钧书画金石题跋艺术作品展在铜...   本报讯 (记者 吴洋凯)5月7日,由铜川书画院主办,铜川书画公园文化艺术有限公司承办的“砚耕四十...
花季少女被限制高消费,原因竟然... 转自:上观新闻2024年的夏天,大一学生小赵攥着身份证站在高铁站售票厅,屏幕上“无法购买高铁票”的提...
新职业新工种,开启无限可能! 转自:新华网 早上叫醒你的不是闹钟,可能是睡眠健康管理师...
政策一经出台,便在金融市场激起... 2025 年 5 月 7 日,中国人民银行今日向市场释放重磅政策组合拳。潘功胜行长在国务院新闻办发布...