【 Python高级编程】什么是图像仿射变换

图像仿射变换(Affine Transformation)是图像处理中的一种基本操作,它通过对图像的几何位置进行线性变换来实现图像的缩放、旋转、平移、剪切等操作。仿射变换保持了图像中的直线性和平行性,即变换后的图像中,原来平行的线依然是平行的,原来直线的部分依然是直线。

1.仿射变换的数学基础

仿射变换可以用一个矩阵乘法和一个向量加法来表示:

( x ′ y ′ ) = ( a b c d ) ( x y ) + ( e f ) \begin{pmatrix} x' \\ y' \end{pmatrix} = \begin{pmatrix} a & b \\ c & d \end{pmatrix} \begin{pmatrix} x \\ y \end{pmatrix} + \begin{pmatrix} e \\ f \end{pmatrix} (xy)=(acbd)(xy)+(ef)

  • 其中:
    ( x , y ) (x, y) (x,y) 是原图像中的点。
    ( x ′ , y ′ ) (x', y') (x,y)是变换后的点。
    ( a b c d ) \begin{pmatrix} a & b \\ c & d \end{pmatrix} (acbd)是线性变换矩阵,决定了旋转、缩放、剪切等变换。
    ( e f ) \begin{pmatrix} e \\ f \end{pmatrix} (ef) 是平移向量。

2. 常见的仿射变换

  • 平移(Translation): 平移操作示意图像在 x 和 y 方向上移动。
    ( x ′ y ′ ) = ( 1 0 0 1 ) ( x y ) + ( t x t y ) \begin{pmatrix} x' \\ y' \end{pmatrix} = \begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix} \begin{pmatrix} x \\ y \end{pmatrix} + \begin{pmatrix} tx \\ ty \end{pmatrix} (xy)=(1001)(xy)+(txty)

其中 ( t x , t y ) (tx, ty) (tx,ty) 是平移的距离。

  • 旋转(Rotation): 旋转操作示意图像绕某一点旋转。
    ( x ′ y ′ ) = ( cos ⁡ θ − sin ⁡ θ sin ⁡ θ cos ⁡ θ ) ( x y ) \begin{pmatrix} x' \\ y' \end{pmatrix} = \begin{pmatrix} \cos \theta & -\sin \theta \\ \sin \theta & \cos \theta \end{pmatrix} \begin{pmatrix} x \\ y \end{pmatrix} (xy)=(cosθsinθsinθcosθ)(xy)

其中 θ \theta θ 是旋转角度。

  • 缩放(Scaling): 缩放操作示意图像按比例放大或缩小。
    ( x ′ y ′ ) = ( s x 0 0 s y ) ( x y ) \begin{pmatrix} x' \\ y' \end{pmatrix} = \begin{pmatrix} sx & 0 \\ 0 & sy \end{pmatrix} \begin{pmatrix} x \\ y \end{pmatrix} (xy)=(sx00sy)(xy)

其中 s x sx sx s y sy sy 是 x 和 y 方向的缩放因子。

  • 剪切(Shearing): 剪切操作示意图像在某一方向上倾斜。
    ( x ′ y ′ ) = ( 1 s h x s h y 1 ) ( x y ) \begin{pmatrix} x' \\ y' \end{pmatrix} = \begin{pmatrix} 1 & sh_x \\ sh_y & 1 \end{pmatrix} \begin{pmatrix} x \\ y \end{pmatrix} (xy)=(1shyshx1)(xy)

其中 s h x sh_x shx s h y sh_y shy是剪切因子。

3.在 OpenCV 中的实现

在 OpenCV 中,可以使用 cv2.getAffineTransform()cv2.warpAffine() 函数来实现仿射变换。

1. 使用 cv2.getAffineTransform()

这是通过三个点来确定一个仿射变换矩阵。

import cv2
import numpy as np

# 读取图像
image = cv2.imread('path_to_your_image.jpg')

# 定义三个点及其变换后的位置
pts1 = np.float32([[50, 50], [200, 50], [50, 200]])
pts2 = np.float32([[10, 100], [200, 50], [100, 250]])

# 计算仿射变换矩阵
M = cv2.getAffineTransform(pts1, pts2)

# 应用仿射变换
transformed_image = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))

# 显示结果
cv2.imshow('Transformed Image', transformed_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
2. 使用 cv2.warpAffine()

直接应用预计算的仿射变换矩阵。

import cv2
import numpy as np

# 读取图像
image = cv2.imread('path_to_your_image.jpg')

# 定义仿射变换矩阵
M = np.float32([[1, 0, 100], [0, 1, 50]])  # 平移操作

# 应用仿射变换
transformed_image = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))

# 显示结果
cv2.imshow('Transformed Image', transformed_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

4,总结

仿射变换是图像处理中的一种基本变换,能够实现图像的平移、旋转、缩放和剪切等操作。通过理解仿射变换的数学基础和在 OpenCV 中的实现方法,可以灵活地对图像进行各种几何变换。

  • 31
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现通过按键选择图像仿变换,可以使用Python中的OpenCV库。下面是一个基本的代码示例: ```python import cv2 import numpy as np img = cv2.imread('image.jpg') # 定义图像的四个角点 pts1 = np.float32([[50, 50], [200, 50], [50, 200]]) pts2 = np.float32([[10, 100], [200, 50], [100, 250]]) # 根据选择的按键进行相应的仿变换 while True: cv2.imshow('Original Image', img) key = cv2.waitKey(0) & 0xFF if key == ord('q'): break elif key == ord('1'): M = cv2.getAffineTransform(pts1, pts2) dst = cv2.warpAffine(img, M, (img.shape[1], img.shape[0])) cv2.imshow('Affine Transformation', dst) elif key == ord('2'): M = cv2.getAffineTransform(pts2, pts1) dst = cv2.warpAffine(img, M, (img.shape[1], img.shape[0])) cv2.imshow('Affine Transformation', dst) cv2.destroyAllWindows() ``` 在这个例子中,我们首先读取了一张图片,然后定义了原始图像的四个角点和目标图像的四个角点。根据用户选择的不同按键,我们使用OpenCV的`getAffineTransform()`函数计算仿变换矩阵,并使用`warpAffine()`函数将变换应用到原始图像上。 当用户按下'1'键时,我们使用`getAffineTransform()`函数计算从原始图像到目标图像仿变换矩阵,并将其应用到原始图像上。当用户按下'2'键时,我们反过来计算从目标图像到原始图像仿变换矩阵,并将其应用到原始图像上。 最后,我们使用OpenCV的`imshow()`函数显示原始图像变换后的结果,并通过`waitKey()`函数等待用户的按键输入。用户可以按'q'键退出程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值