- 可以理解一下常规的翻转和平移。“线性变换”+“平移”
- 空间变换中的仿射变换对应着五种变换,平移,缩放,旋转,翻转,错切。而这五种变化由原图像转变到变换图像的过程,可以用仿射变换矩阵进行描述。而这个变换过程可以用一个3*3的矩阵与原图进行相乘得到。
- 仿射变换(Affine Transformation)其实是另外两种简单变换的叠加:一个是线性变换,一个是平移变换
- 共线性:若几个点变换前在一条线上,则仿射变换后仍然在一条线上
- 共线比例不变性:变换前一条线上两条线段的比例,在变换后比例不变
- 平行性:若两条线变换前平行,则变换后仍然平行。
矩阵显示
具体公式(可以看成线性变换R和平移变换T的叠加):
[ cos ( θ ) − sin ( θ ) t x sin ( θ ) cos ( θ ) t y 0 0 1 ] [ x y 1 ] = [ x ′ y ′ 1 ] \left[ \begin{array}{ccc}{\cos (\theta)} & {-\sin (\theta)} & {t_{x}} \\ {\sin (\theta)} & {\cos (\theta)} & {t_{y}} \\ {0} & {0} & {1} \end{array}\right] \left[ \begin{array}{l}{x} \\ {y} \\ {1}\end{array}\right]=\left[ \begin{array}{c}{x^{\prime}} \\ {y^{\prime}} \\ {1}\end{array}\right] cos(θ)sin(θ)0−sin(θ)cos(θ)0txty1 xy1 = x′y′1
其中 s , θ , t x , t y s, \theta, t_x, t_y s,θ,tx,ty变换量,角度经过变换后具体的公式为:
[ s cos ( θ ) − s sin ( θ ) t x s sin ( θ ) s cos ( θ ) t y 0 0 1 ] [ x y 1 ] = [ x ′ y ′ 1 ] \left[ \begin{array}{ccc}{s\cos (\theta)} & {-s\sin (\theta)} & {t_{x}} \\ {s\sin (\theta)} & {s\cos (\theta)} & {t_{y}} \\ {0} & {0} & {1} \end{array}\right] \left[ \begin{array}{l}{x} \\ {y} \\ {1}\end{array}\right]=\left[ \begin{array}{c}{x^{\prime}} \\ {y^{\prime}} \\ {1}\end{array}\right] scos(θ)ssin(θ)0−ssin(θ)scos(θ)0txty1 xy1 = x′y′1
仿射变换基本公式:
f ( x ) = A x + b , x ∈ X f(x)=Ax+b,x∈X f(x)=Ax+b,x∈X
平移变换矩阵为:
[ 1 0 t x 0 1 t y 0 0 1 ] \left[ \begin{array}{ccc}{1} & {0} & {t_{x}} \\ {0} & {1} & {t_{y}} \\ {0} & {0} & {1} \end{array}\right] 100010txty1
其中 t x , t y {t_{x}} ,{t_{y}} tx,ty控制平移的参数,向上向下平移参数
反射变换
[ − 1 0 0 0 1 0 0 0 1 ] \left[ \begin{array}{ccc}{-1} & {0} & {0} \\ {0} & {1} & {0} \\ {0} & {0} & {1} \end{array}\right] −100010001
控制矩阵的正负可以按照对应的坐标轴进行变换。具体如上图。
仿射变换,变化
import cv2
import numpy as np
src = cv2.imread("./images/1.jpg")
rows,cols,channel = src.shape
M = np.float32([[1,0,50],[0,1,50]])
# M = cv2.getRotationMatrix2D((cols/2,rows/2),angle=45,scale=0.7)
dst = cv2.warpAffine(src,M=M,dsize=(cols,rows))
cv2.imshow("src",src)
cv2.imshow("dst",dst)
cv2.waitKey(0)
- 示例2:
import cv2
import numpy as np
src = cv2.imread("./images/1.jpg")
rows,cols,channel = src.shape
# M = np.float32([[0.5,0,0],[0,0.5,0]])
# M = np.float32([[0.5,0,0],[0,0.5,0]])
M = np.float32([[-0.5,0,cols//2],[0,0.5,0]])
# M = cv2.getRotationMatrix2D((cols/2,rows/2),angle=45,scale=0.7)
dst = cv2.warpAffine(src,M=M,dsize=(cols,rows))
cv2.imshow("src",src)
cv2.imshow("dst",dst)
cv2.waitKey(0)
import cv2
import numpy as np
src = cv2.imread("./images/1.jpg")
rows,cols,channel = src.shape
# M = np.float32([[0.5,0,0],[0,0.5,0]])
# M = np.float32([[0.5,0,0],[0,0.5,0]])
# M = np.float32([[-0.5,0,cols//2],[0,0.5,0]])
M = np.float32([[1,0.5,0],[0,1,0]])
# M = cv2.getRotationMatrix2D((cols/2,rows/2),angle=45,scale=0.7)
dst = cv2.warpAffine(src,M=M,dsize=(cols,rows))
cv2.imshow("src",src)
cv2.imshow("dst",dst)
cv2.waitKey(0)
import cv2
import numpy as np
src = cv2.imread("./images/1.jpg")
rows,cols,channel = src.shape
# M = np.float32([[0.5,0,0],[0,0.5,0]])
# M = np.float32([[0.5,0,0],[0,0.5,0]])
# M = np.float32([[-0.5,0,cols//2],[0,0.5,0]])
# M = np.float32([[1,0.5,0],[0.5,1,0]])
M = cv2.getRotationMatrix2D((cols/2,rows/2),angle=45,scale=0.7)
dst = cv2.warpAffine(src,M=M,dsize=(cols,rows))
cv2.imshow("src",src)
cv2.imshow("dst",dst)
cv2.waitKey(0)