Affine transformation of image

In Python's OpenCV library, affine transformation is a method of geometrically transforming an image. It changes the shape, size and position of the image by applying linear and translational transformations. Affine transformation can use the cv2.getAffineTransform() function to calculate the affine transformation matrix, and then use the cv2.warpAffine() function to apply the transformation matrix to the image.

The following is the mathematical principle of the implementation process of affine transformation:
1. Select three points: Before performing affine transformation, we need to select three points in the original image and the corresponding three points in the target image. These three points can be used to define the affine transformation matrix.
2. Calculate the affine transformation matrix: Use the cv2.getAffineTransform() function to calculate the affine transformation matrix based on the selected points. The affine transformation matrix is ​​a 2x3 matrix that contains transformation parameters such as translation, scaling, and shearing.
3. Apply affine transformation: Use the cv2.warpAffine() function to apply the calculated affine transformation matrix to the original image. This will transform the image according to the transformation parameters in the matrix, producing the target image.

The specific implementation process is as follows:
1. Select three points: Select three points (x1, y1), (x2, y2), (x3, y3) in the original image, and determine their corresponding points in the target image ( x1', y1'), (x2', y2'), (x3', y3').
2. Calculate the affine transformation matrix: Use the cv2.getAffineTransform() function to calculate the affine transformation matrix. This function accepts three points in the original image and the target image as input, and then calculates the affine transformation matrix by solving a system of linear equations.
The calculation formula of the affine transformation matrix is ​​as follows:

[a1 a2 b1]
[a3 a4 b2]

3. Apply affine transformation: Use the cv2.warpAffine() function to apply the affine transformation matrix to the original image. This function accepts the original image, the calculated affine transformation matrix and the size of the target image as input, and then transforms the original image according to the transformation matrix to generate the target image.
The calculated formula for the transformed pixel position is as follows:

x' = a1*x + a2*y + b1
y' = a3*x + a4*y + b2

Among them, (x, y) is the pixel position in the original image, (x', y') is the pixel position in the target image.
Through this process, we can perform affine transformation operations such as translation, rotation, scaling, and shearing on the image to achieve geometric transformation of the image.

The following is a code example that uses classes to implement affine transformations:

import cv2
import numpy as np

class AffineTransformer:
    def __init__(self):
        self.source_points = []
        self.target_points = []
        self.transformation_matrix = None

    def set_points(self, source_points, target_points):
        self.source_points = np.float32(source_points)
        self.target_points = np.float32(target_points)

    def calculate_transformation_matrix(self):
        self.transformation_matrix = cv2.getAffineTransform(self.source_points, self.target_points)

    def apply_transform(self, image):
        if self.transformation_matrix is None:
            raise ValueError("Transformation matrix has not been calculated.")
        else:
            rows, cols = image.shape[:2]
            return cv2.warpAffine(image, self.transformation_matrix, (cols, rows))

# 示例用法
# 创建仿射变换器实例
transformer = AffineTransformer()

# 设置源点和目标点
source_points = [[50, 50], [200, 50], [50, 200]]  # 源图像中的三个点
target_points = [[70, 100], [220, 50], [150, 250]]  # 目标图像中对应的三个点
transformer.set_points(source_points, target_points)

# 计算仿射变换矩阵
transformer.calculate_transformation_matrix()

# 读取原始图像
image = cv2.imread('input_image.jpg')

# 应用仿射变换
transformed_image = transformer.apply_transform(image)

# 显示原始图像和变换后的图像
cv2.imshow('Original Image', image)
cv2.imshow('Transformed Image', transformed_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

In the above example, the AffineTransformer class encapsulates the functionality of affine transformation. First set three points in the source image and target image through the set_points method, and then calculate the affine transformation matrix through the calculate_transformation_matrix method. Finally, the apply_transform method is used to apply the transformation matrix to the original image to generate the target image. Please note that the point coordinates used in the example are in two dimensions, that is, in the form [x, y]. You can modify the coordinates of the source point and target point according to actual needs. Also, in order to run the example, input_image.jpg needs to be replaced with the actual input image path

Link: link

Guess you like

Origin blog.csdn.net/qq_50993557/article/details/131281846