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

该博客介绍了图像处理中的仿射变换,包括平移、缩放、旋转和翻转等基本操作。通过3x3变换矩阵来描述这些变换,并展示了如何使用OpenCV库在Python中实现这些变换。示例代码演示了如何应用变换矩阵对图像进行实际操作。
摘要由CSDN通过智能技术生成
  • 可以理解一下常规的翻转和平移。“线性变换”+“平移”
  • 空间变换中的仿射变换对应着五种变换,平移,缩放,旋转,翻转,错切。而这五种变化由原图像转变到变换图像的过程,可以用仿射变换矩阵进行描述。而这个变换过程可以用一个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 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)

在这里插入图片描述

  • 16
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小陈phd

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

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

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

打赏作者

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

抵扣说明:

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

余额充值