Understanding Affine Transformation
Affine transformation is a geometric transformation that preserves points, straight lines, and planes. It includes translation, rotation, scaling, and shearing while maintaining parallelism in lines. OpenCV provides the cv2.getAfflineTransform() function to perform this transformation using three points before and after transformation.
Applying Affine Transformation in OpenCV
import cv2 import numpy as np
- Import the OpenCV (cv2) and NumPy (np) libraries. OpenCV is used for image processing, while NumPy helps in handling matrix operations.
image = cv2.imread('input.jpg')
- Load the input image using cv2.imread(). Replace ‘input.jpg’ with the actual image path.
rows, cols, ch = image.shape
- Get the shape (dimensions) of the image:
- rows: Height of the image.
- cols: Width of the image.
- ch: Number of color channels (3 for RGB images).
pts1 = np.float32([[50, 50], [200, 50], [50, 200]])
- Define three points in the original image that will be used to determine the transformation.
- These points represent a triangular region in the image.
pts2 = np.float32([[10, 100], [200, 50], [100, 250]])
- Define the corresponding three points in the output image where the original points will be mapped.
- This determines how the transformation affects the image.
matrix = cv2.getAffineTransform(pts1, pts2)
Compute the affine transformation matrix using cv2.getAffineTransform() , which takes two sets of three points.
transformed_image = cv2.warpAffine(image, matrix, (cols, rows))
- Apply the affine transformation to the image using cv2.warpAffine().
- The function takes:
- The original image.
- The transformation matrix.
- The size of the output image (cols,rows).
cv2.imshow('Affine Transformed Image', transformed_image) cv2.waitKey(0) cv2.destroyAllWindows()
- Display the transformed image using cv2.imshow().
- Wait for a key press using cv2.waitKey(0), then close the window using cv2.destroyAllWindows().
Complete Code:
import cv2 import numpy as np image = cv2.imread('input.jpg') rows, cols, ch = image.shape # Define three points in the original and transformed image pts1 = np.float32([[50, 50], [200, 50], [50, 200]]) pts2 = np.float32([[10, 100], [200, 50], [100, 250]]) # Compute the affine transformation matrix matrix = cv2.getAffineTransform(pts1, pts2) # Apply the affine transformation transformed_image = cv2.warpAffine(image, matrix, (cols, rows)) # Display the transformed image cv2.imshow('Affine Transformed Image', transformed_image) cv2.waitKey(0) cv2.destroyAllWindows()
The output will be an image with a transformed triangular region. The selected points in the original image are mapped to new positions, resulting in a distortion effect. If input.jpg is an image of a square, the transformed image will show a shifted and skewed version of the square.
Before Transformation (Original Image)
- A normal image before applying affine transformation.
After Transformation (Output Image)
- A skewed image with the transformation applied.