当前位置: 首页> 科技> 数码 > 传统CV算法——仿射变换原理及应用

传统CV算法——仿射变换原理及应用

时间:2025/9/17 7:35:28来源:https://blog.csdn.net/weixin_42917352/article/details/118730410 浏览次数:0次
  • 可以理解一下常规的翻转和平移。“线性变换”+“平移”
  • 空间变换中的仿射变换对应着五种变换,平移,缩放,旋转,翻转,错切。而这五种变化由原图像转变到变换图像的过程,可以用仿射变换矩阵进行描述。而这个变换过程可以用一个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(θ)0sin(θ)cos(θ)0txty1 xy1 = xy1

其中 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(θ)0ssin(θ)scos(θ)0txty1 xy1 = xy1

仿射变换基本公式:

f ( x ) = A x + b , x ∈ X f(x)=Ax+b,x∈X f(x)=Ax+b,xX

平移变换矩阵为:

[ 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 npsrc = cv2.imread("./images/1.jpg")rows,cols,channel = src.shapeM = 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 npsrc = 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 npsrc = 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)

在这里插入图片描述

关键字:传统CV算法——仿射变换原理及应用

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: