首页 > 其他分享 >2.Transformation线性变换

2.Transformation线性变换

时间:2023-07-01 13:11:40浏览次数:34  
标签:begin end 矩阵 pmatrix bmatrix alpha 线性变换 Transformation

WHY

  • 我们通过摄像机对拍摄的画面进行缩放、旋转、偏移,来将三维模型映射到二维的屏幕画面上

二维线性变换

\[x^,=a~x+b~y\\y^,=c~x+d~y\\\begin{bmatrix}x^,\\y^, \\ \end{bmatrix} =\begin{bmatrix} a&b \\ c&d \\ \end{bmatrix}\cdot\begin{bmatrix}x\\y \\ \end{bmatrix}\\x^,=M~x \]

  • 二维线性变换通用形式

1.缩放矩阵

\[\begin{bmatrix}x^,\\y^, \\ \end{bmatrix}=\begin{bmatrix}S&0 \\ 0&S \\ \end{bmatrix}\cdot\begin{bmatrix}x\\y \\ \end{bmatrix} \]

  • 对于矩阵上任意点\(\begin{bmatrix}x\\y \\ \end{bmatrix}\)经过缩放矩阵后,结果为\(\begin{bmatrix}Sx\\Sy \\ \end{bmatrix}\),比较好理解
  • 当然,x轴和y轴方向的缩放不一定相同

2.反射矩阵

\[\begin{bmatrix}x^,\\y^, \\ \end{bmatrix}=\begin{bmatrix}-1&0\\0&1 \\ \end{bmatrix}\cdot\begin{bmatrix}x\\y \\ \end{bmatrix} \]

  • 对于矩阵上任意点\(\begin{bmatrix}x\\y \\ \end{bmatrix}\)经过反射后,结果为\(\begin{bmatrix}-x\\y \\ \end{bmatrix}\),相当于图像对于y轴进行了反转

3.切变矩阵

\[\begin{bmatrix}x^,\\y^, \\ \end{bmatrix}=\begin{bmatrix}1&a\\0&1 \\ \end{bmatrix}\cdot\begin{bmatrix}x\\y \\ \end{bmatrix} \]

  • 切变,具体表现为像拉着图像的一角向一个方向倾斜
  • 观察可得当y=0时,图像在水平方向移动为0,当y=1时,图像在水平方向移动为a,且图像的y轴一直没有变化
  • 得出结论,对于矩阵上任意点\(\begin{bmatrix}x\\y \\ \end{bmatrix}\)经过切变后,结果为\(\begin{bmatrix}x+ay\\y \\ \end{bmatrix}\)

4.旋转矩阵

  • 前提:绕着原点(0,0)旋转,且默认逆时针旋转

\[R_\theta=\begin{bmatrix}cos\theta&-sin\theta\\sin\theta&cos\theta \\ \end{bmatrix} \]

推导流程

  • 通过特殊点\(A(0,1)\)经过旋转\(\theta\)角度,现在的坐标为\((cos\theta,sin\theta)\),可得\(\begin{bmatrix}cos\theta\\sin\theta \\ \end{bmatrix}=\begin{bmatrix}A~B\\C~D \\ \end{bmatrix}\begin{bmatrix}1\\0 \\ \end{bmatrix}\),得到两个式子\(cos\theta=A*1+B*0=A,sin\theta=C*1+D*0=C\)
  • 通过特殊点\(B(0,1)\)经过旋转\(\theta\)角度,现在的坐标为\(B(-sin\theta,cos\theta)\),可得\(\begin{bmatrix}-sin\theta\\cos\theta \\ \end{bmatrix}=\begin{bmatrix}A~B\\C~D \\ \end{bmatrix}\begin{bmatrix}0\\1 \\ \end{bmatrix}\),得到两个式子\(-sin\theta=A*0+B*1=B,cos\theta=C*0+D*1=D\)
  • 得出\(A=cos\theta,B=-sin\theta,C=sin\theta,D=cos\theta\)

齐次座标

WHY

为什么要学习齐次座标,因为平移变换不能直接写成矩阵形式

\[\begin{bmatrix}x^,\\y^, \\ \end{bmatrix} =\begin{bmatrix} a&b \\ c&d \\ \end{bmatrix}\cdot\begin{bmatrix}x\\y \\ \end{bmatrix}+\begin{bmatrix}t_x\\t_y\end{bmatrix} \]

  • 带入上面的通用式子后发现不能直接套用
  • 为了统一标准表现矩阵的变换,大佬们引入了齐次座标
  • 二维点=\((x,y,1)^T\)
  • 二维向量=\((x,y,0)^T\)
  • 二维点的平移矩阵如下

\[\begin{bmatrix}x^,\\y^,\\w^, \\ \end{bmatrix} =\begin{bmatrix} 1&0&t_x\\ 0&1&t_y\\0&0&1 \\ \end{bmatrix}\cdot\begin{bmatrix}x\\y \\1\\ \end{bmatrix}=\begin{bmatrix}x+t_x\\y+t_y\\1 \\ \end{bmatrix} \]

仿射变换

  • 统一二维线性变换和平移变换,引入了仿射变换
  • 仿射变换=线性变换+平移,类似结构如下

\[\begin{pmatrix}x^,\\y^, \\ \end{pmatrix} =\begin{pmatrix} a&b \\ c&d \\ \end{pmatrix}\cdot\begin{pmatrix}x\\y \\ \end{pmatrix}+\begin{pmatrix}t_x\\t_y\end{pmatrix} \]

  • 所有仿射变换可以写成齐次坐标形式

\[\begin{pmatrix}x^,\\y^,\\1 \\ \end{pmatrix} =\begin{pmatrix} a&b&t_x\\ c&d&t_y\\0&0&1 \\ \end{pmatrix}\cdot\begin{pmatrix}x\\y \\1\\ \end{pmatrix} \]

缩放矩阵(齐次坐标)

\[S(s_x,s_y)=\begin{pmatrix} s_x&0&0 \\ 0&s_y&0 \\0&0&1 \\ \end{pmatrix} \]

旋转矩阵(齐次坐标)

\[R(\alpha)=\begin{pmatrix} cos\alpha&-sin\alpha&0\\sin\alpha&cos\alpha&0\\0&0&1 \\ \end{pmatrix} \]

平移矩阵(齐次坐标)

\[T(t_x,t_y)=\begin{pmatrix}1&0&t_x\\0&1&t_y\\0&0&1 \\ \end{pmatrix} \]

多种变换组合

要点

  • 组合变换的顺序不能调换
  • 运算的顺序为从右往左

例子:

图像先旋转45,再x轴平移1。

例题:这个矩阵如何变化到最终结果

  • 先平移到原点(0,0)
  • 旋转到相应角度
  • 平移相同距离返回到移动前的位置

三维线性变换

  • 3D point=\((x,y,z,1)^T\)
  • 3D vector=\((x,y,z,0)^T\)

三维空间仿射变换

\[\begin{pmatrix}x^,\\y^,\\z^,\\1 \\ \end{pmatrix} =\begin{pmatrix}a&b&c&t_x\\d&e&f&t_y\\g&h&i&t^z\\0&0&0&1 \\ \end{pmatrix}\cdot\begin{pmatrix}x\\y \\z\\1\\ \end{pmatrix} \]

缩放

\[S(s_x,s_y,s_z)=\begin{pmatrix}1&0&0&0\\0&1&0&0\\0&0&1&0\\0&0&0&1 \\ \end{pmatrix} \]

平移

\[T(t_x,t_y,t_z)=\begin{pmatrix}1&0&0&t_x\\0&1&0&t_y\\0&0&1&t_z\\0&0&0&1 \\ \end{pmatrix} \]

旋转

-绕x轴旋转

\[R_x(\alpha)=\begin{pmatrix}1&0&0&0\\0&cos\alpha&-sin\alpha&0\\0&sin\alpha&cos\alpha&0\\0&0&0&1 \\ \end{pmatrix} \]

-绕y轴旋转

\[R_y(\alpha)=\begin{pmatrix}cos\alpha&0&sin\alpha&0\\0&1&0&0\\-sin\alpha&0&cos\alpha&0\\0&0&0&1 \\ \end{pmatrix} \]

-绕z轴旋转

\[R_z(\alpha)=\begin{pmatrix}cos\alpha&-sin\alpha&0&0\\sin\alpha&cos\alpha&0&0\\0&0&1&0\\0&0&0&1 \\ \end{pmatrix} \]

  • 绕x轴旋转,绕z轴旋转和绕y轴旋转矩阵为什么不一致
  • 因为y轴是由z轴叉乘x轴获得的(根据右手螺旋定则,手指由z轴绕向x轴,大拇指方向为y轴方向)
  • x轴是由yXz得到,z轴是由xXy得到,y是相反的
罗德里格斯旋转公式:把任意旋转转化成矩阵形式

\[R(n,\alpha)=cos(\alpha)I+(1+cos(\alpha))nn^T+sin(\alpha)\underbrace{\begin{pmatrix}0&-n_z&n_y\\n_z&0&-n_x\\-n_y&n_x&0 \\ \end{pmatrix}}_{N} \]

推导流程

观测变换(Viewing Transformation)

  • 视图、摄像机变换
  • 投影变换
    • 正交变换
    • 透视变换

1.视图变换(View/Camera Transformation)

问题:怎么拍一张照片

  1. 选一个好地方,人摆好位置(model transformation)
  2. 选一个好的角度放置摄像机,包括摄像机的位置高度,朝向等(view transformation)
  3. 拍照,把三维空间投影到二维空间(projection transformation)

简称MVP变换

  • 通过三个变量来确定摄像机的唯一属性
  • Position \(\color{red}\vec{e}\),摄像机在三维空间里的位置座标
  • Look-at/gaze direction \(\color{red}\hat{g}\),表示摄像机的朝向
  • up direction \(\color{red}\hat{t}\),摄像机自身向上的方向

  • 约定摄像机摆放在原点(0,0,0),自身朝向以y轴为向上,以-z轴方向为观测方向

问题:怎么让一个摄像机回到原点并恢复默认观察状态

  1. 移动摄像机回到原点(0,0,0)
  2. 旋转\(\hat{g}\),使其朝向-z轴
  3. 旋转\(\hat{t}\),使其朝向y轴

简单的描述为先平移后旋转

NEXT:怎么用矩阵来表示?
  • \(M_{view}=R_{view}T_{view}\)
  • 平移的矩阵好写的,但是旋转\(\hat{g}\)和旋转\(\hat{t}\)的矩阵比较复杂
  • 但是从-Z、Y、X轴旋转到\(\hat{g},\hat{t}\)向量的逆矩阵很好写

  • 得到逆矩阵
  • 由于旋转矩阵是正交矩阵,矩阵的转置=逆矩阵,转置后获得了旋转矩阵

总结

  • 拍摄的物体要和摄像机一起运动到相对位置上
  • 摄像机自身朝向为y轴,拍摄方向为-Z轴

2.投影变换(Projection transformation)

把三维空间投影到二维空间

a)正交投影(Orthographic projection)

  • 不管摄像机距离远近,一样的大小
  • 常用于UI界面
常规方式理解正交投影操作

  • 正交投影的操作类似于把一个已知长宽高的立方体,不同比例缩放到\((-1,1)^3\)的标准1体积立方体中来
  • 首先,把立方体放置到原点(0,0,0)
  • 然后把长宽高缩放到(-1,1)

矩阵怎么表示?

\[M_{ortho}=\begin{bmatrix} \frac{2}{r-l}&0&0&0\\ 0&\frac{2}{t-b}&0&0\\0&0&\frac{2}{n-f}&0 \\0&0&0&1 \end{bmatrix}\cdot\begin{bmatrix} 1&0&0&-\frac{l+r}{2}\\ 0&1&0&-\frac{t+b}{2}\\0&0&1&-\frac{n+f}{2} \\0&0&0&1 \end{bmatrix} \]

b)透视投影(Perspective projection)

特点:

  • 近大远小
  • 齐次坐标中表示3维中的一个点(x,y,z,1),乘以k(k!=0),(kx,ky,kz,k),这个点还是原来的点(3维中)。
透视投影的转换关系表示

  • 关键思想:找到初始点(x,y,z)与转化后的点(x',y',z')的关系
  • 根据相似三角形\(y^,=\frac{n}{z}y\),同理\(x^,=\frac{n}{z}x\),z轴方向未知
  • 我们可以反推出变换矩阵的第一第二行和最后一行

接着来求第三行

  • 根据近平面投影切面的点P’(x',y',z')的z'始终为z',z’对于投影后的P’已经没有意义了,这个信息点已经没用了
  • 但对于3D图形管线来说,为了便于进行后面的片元操作,例如z缓冲消隐算法,有必要把投影之前的z保存下来,方便后面使用。
  • 因此,我们利用这个没用的信息点存储为n,设第3个数为n
  • x,y,1同乘以n,得到新的式子,已知矩阵的第三行与x,y无关,所以前两个位置为0,后两个位置设为A,B ,得到An+B1=n^2

矩阵表示:

  • 再根据远平面切面的中心点经过变换仍是中心点,得到

  • 联立后可得

  • 最终推导出透视投影的变换矩阵

\[M_{persp->ortho}=\begin{pmatrix}n&0&0&0\\0&n&0&0\\0&0&n+f&-nf\\0&0&1&0 \\ \end{pmatrix} \]

标签:begin,end,矩阵,pmatrix,bmatrix,alpha,线性变换,Transformation
From: https://www.cnblogs.com/oOLzYOo/p/17519149.html

相关文章

  • 直接线性变换法DLT(张正友标定法)
    相机2D坐标到3D坐标的变换是一个SLAM、三维重建、相机标定的经典问题,这个过程必定要求解相机的内参矩阵、外参矩阵。虽然这是一个slam最基础的入门问题,似乎只要调一个函数就能完成,但实际上,该问题是一个涉及最优化、矩阵论、代数等多个领域的较复杂数学问题。参考资料:PnP算法详......
  • CF280E - Sequence Transformation
    给定一个不降整数序列\(1\lex_1\lex_2\le\cdots\lex_n\leq\),请构造一个实数序列\(y\)满足\(y_i\in[1,q]\),\(y_i-y_{i-1}\in[a,b]\),且最小化\(\sum(y_i-x_i)^2\),保证有解。利用凸函数性质维护导数我们设\(dp_i(u)\)表示对于所有的合法的\(u\),\(y_i=u\)时\(\sum_......
  • Customising claims transformation in ASP.NET Core Identity
    I’vebeentestingoutthenewversionofASP.NETIdentityandhadtheneedtoincludeadditionalclaimsinthe ClaimIdentity generatedwhenauserisauthenticated.TransformingClaimsIdentityASP.NETCoresupportsClaimsTransformationoutofthebox.J......
  • 现代计算机图形学——P4. Transformation 3D
    P4.TransformationCont.(上节课补充)————————————————————————————————————————————————————————————————今天的内容:———————————————————————————————————......
  • kettle从入门到精通 第十七课 kettle Transformation executor
    Transformationexecutor步骤是一个流程控件,和映射控件类似却又不一样。1、子转换需要配合使用从结果获取记录和复制记录到结果两个步骤,而子映射需要配合映射输入规范和映射输出规范使用。如下子转换实现的功能是计算x+y的值 2、从结果获取记录1)步骤名称:自定义2)字段:设置从......
  • 论文阅读笔记《Grounded Action Transformation for Robot Learning in Simulation》
    GroundedActionTransformationforRobotLearninginSimulation发表于AAAI2017仿真机器人学习中的接地动作变换HannaJ,StoneP.Groundedactiontransformationforrobotlearninginsimulation[C]//ProceedingsoftheAAAIConferenceonArtificialIntelligence......
  • 论文阅读笔记《Stochastic Grounded Action Transformation for Robot Learning in Si
    StochasticGroundedActionTransformationforRobotLearninginSimulation发表于IROS2020(CCFC)模拟中机器人学习的随机接地动作转换DesaiS,KarnanH,HannaJP,etal.Stochasticgroundedactiontransformationforrobotlearninginsimulation[C]//2020IEEE......
  • 04 Viewing Transformation
    关键点ModelViewTransformationMatrix(1-3)OrthographicProjectionMatrix(4)PerspectiveProjectionMatrix(5-6)1.View/Camera/ModelViewTransformationMVP(modeltransformation->viewtransformation->projectiontransformation)Cameradefine......
  • 03 Transformation
    关键点AffineTransformation(LinearTransformation,Transmation)HomogeneouscoordinatesCompositeTransform2Dand3D1.LinearTransformation(2Dforinstacnce)1.1ScaleReflection1.2Shear1.3Rotate默认以原点为中心逆时针。1.4总结:LinearTransfo......
  • 现代计算机图形学——P3. Transformation-2D
    P3.TransformationP3.Transformation 矩阵和变换联系起来 ReflectionMatrix(反射矩阵(名字不重要)):切变:旋转:推导:旋转矩阵中的B和D可以用(0,1)这个点来推算线性变换:(先不管这个M)齐次坐标为什么要用齐次坐标:......