图形变换核心原理(平移、缩放、旋转,拉伸)

以Android为例,在使用Matrix进行绘图时有如下变换方法

// 缩放
public boolean preScale(float sx, float sy, float px, float py)
// 平移
public boolean preTranslate(float dx, float dy)
// 旋转    
public boolean preRotate(float degrees, float px, float py)
// 倾斜    
public boolean preSkew(float kx, float ky, float px, float py)

​ 在我们进行图片变换的时候,需要做各种各样的操作,其中的原理需要我们细细体会一下,今天来总结下图形变换的核心原理

​ 首先我们计(w,v)为源图像的像素点位置,(x,y)为目标像素点的位置。我们当前有一个变化因子记为T,这三者之前存在着这样的映射关系:

( x , y , 1 ) = ( w , v , 1 ) T ​ (x,y,1)=(w,v,1)T​ (x,y,1)=(w,v,1)T

T是一个3×33×3的矩阵

T = [ 1 0 0 0 1 0 0 0 1 ] T =\begin{bmatrix} 1&0&0\\ 0&1&0\\ 0&0&1\\ \end{bmatrix} T=100010001

具体变换方式如下

变换名称仿射矩阵坐标公式
恒等变换 [ 1 0 0 0 1 0 0 0 1 ] \begin {bmatrix}1&0&0\\0&1&0\\0&0&1\\\end{bmatrix} 100010001x=w y=v
尺度变换 [ C x 0 0 0 C y 0 0 0 1 ] \begin {bmatrix}C_x&0&0\\0&C_y&0\\0&0&1\\\end{bmatrix} Cx000Cy0001 x = C x w , y = C y v x=C_x w ,y = C_yv x=Cxw,y=Cyv
旋转变换 [ c o s θ s i n θ 0 − s i n θ c o s θ 0 0 0 1 ] \begin {bmatrix}cos \theta&sin\theta&0\\-sin\theta &cos\theta &0\\0&0&1\\\end{bmatrix} cosθsinθ0sinθcosθ0001 x = w c o s θ − v s i n θ x= wcos\theta -vsin\theta x=wcosθvsinθ y = v s i n θ + w c o s θ y = vsin\theta +w cos\theta y=vsinθ+wcosθ
平移变换 [ 1 0 t x 0 1 t y 0 0 1 ] \begin {bmatrix}1&0&t_x\\0&1&t_y\\0&0&1\\\end{bmatrix} 100010txty1 x = w + t x , y = v + t y x = w + t_x , y = v + t_y x=w+tx,y=v+ty
偏移变换 [ 1 C 1 0 C 2 1 0 0 0 1 ] \begin {bmatrix}1&C_1&0\\C_2&1&0\\0&0&1\\\end{bmatrix} 1C20C110001 x = w C 1 + v x = w C_1 + v x=wC1+v y = w C 2 + v y = wC_2 + v y=wC2+v

以缩放为例,下图为缩放图像

在这里插入图片描述

缩放公式如下

[ 2 0 0 0 2 0 0 0 1 ] ∗ [ w v 1 ] = [ 2 w 2 v 1 ] \begin {bmatrix}2&0&0\\0&2&0\\0&0&1\\\end{bmatrix}*\begin {bmatrix} w\\v\\1\end{bmatrix}= \begin {bmatrix} 2w\\2v\\1\end{bmatrix} 200020001wv1=2w2v1

(0,0)坐标变成(0,0)

(0,1)坐标变成(0,2)

(1,1)坐标变成(2,2)

(1,0)坐标变成(2,0)

同理,平移公式如下

[ 1 0 x 0 1 y 0 0 1 ] ∗ [ w v 1 ] = [ w + x v + y 1 ] \begin {bmatrix}1&0&x\\0&1&y\\0&0&1\\\end{bmatrix}*\begin {bmatrix} w\\v\\1\end{bmatrix} =\begin {bmatrix} w+x\\v+y\\1\end{bmatrix} 100010xy1wv1=w+xv+y1

平移坐标(w,v)就变成了(w+x,x+y)

图像的旋转公式如下

[ c o s θ s i n θ 0 − s i n θ c o s θ 0 0 0 1 ] ∗ [ w v 1 ] = [ w c o s θ + v s i n θ v c o s θ − v s i n θ 1 ] \begin {bmatrix}cos \theta&sin\theta&0\\-sin\theta &cos\theta &0\\0&0&1\\\end{bmatrix}*\begin {bmatrix} w\\v\\1\end{bmatrix}=\begin {bmatrix} wcos\theta+vsin\theta&\\vcos\theta-vsin\theta\\1\end{bmatrix} cosθsinθ0sinθcosθ0001wv1=wcosθ+vsinθvcosθvsinθ1










  1. 矩阵乘法的概念:https://baike.baidu.com/item/矩阵乘法/5446029?fr=aladdin
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

ddssingsong

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

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

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

打赏作者

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

抵扣说明:

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

余额充值