Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to apply Affine Transformation on an image in OpenCV Python?
Affine Transformation is a geometric transformation that preserves parallel lines in an image. This transformation requires three corresponding points between the input and output images to create a transformation matrix.
Syntax
To get the transformation matrix ?
M = cv2.getAffineTransform(pts1, pts2)
To apply the transformation ?
cv2.warpAffine(img, M, (cols, rows))
Parameters
pts1 Array of three points on the input image
pts2 Array of corresponding three points on the output image
img Input image to be transformed
M 2×3 transformation matrix of type
np.float64(cols, rows) Output image dimensions
Steps for Affine Transformation
Follow these steps to perform affine transformation ?
- Import required libraries (OpenCV, NumPy)
- Read the input image
- Define three corresponding points (pts1 and pts2)
- Compute transformation matrix using
cv2.getAffineTransform() - Apply transformation using
cv2.warpAffine() - Display the transformed image
Example 1: Basic Affine Transformation
This example demonstrates basic affine transformation with OpenCV display ?
import cv2
import numpy as np
# Read the input image
img = cv2.imread('/opencv/lines.jpg')
# Get image dimensions
rows, cols, _ = img.shape
# Define three points on input image
pts1 = np.float32([[50,50], [200,50], [50,200]])
# Define corresponding points on output image
pts2 = np.float32([[10,100], [200,50], [100,250]])
# Get the affine transformation matrix
M = cv2.getAffineTransform(pts1, pts2)
# Apply affine transformation
dst = cv2.warpAffine(img, M, (cols, rows))
# Display result
cv2.imshow("Original", img)
cv2.imshow("Affine Transform", dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
Example 2: Affine Transformation with Matplotlib
This example shows transformation using matplotlib for side-by-side comparison ?
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Create a simple test image
img = np.zeros((300, 300), dtype=np.uint8)
cv2.rectangle(img, (50, 50), (200, 200), 255, -1)
cv2.rectangle(img, (100, 100), (150, 150), 0, -1)
rows, cols = img.shape
# Define transformation points
pts1 = np.float32([[50,50], [200,50], [50,200]])
pts2 = np.float32([[10,100], [200,50], [100,250]])
# Get transformation matrix and apply
M = cv2.getAffineTransform(pts1, pts2)
dst = cv2.warpAffine(img, M, (cols, rows))
# Display using matplotlib
plt.figure(figsize=(10, 4))
plt.subplot(121)
plt.imshow(img, cmap='gray')
plt.title('Original Image')
plt.axis('off')
plt.subplot(122)
plt.imshow(dst, cmap='gray')
plt.title('Affine Transformed')
plt.axis('off')
plt.tight_layout()
plt.show()
Key Points
Affine transformation preserves parallel lines but not angles or lengths
Requires exactly three corresponding points to define the transformation
The transformation matrix M is 2×3 in size
Common transformations include rotation, scaling, shearing, and translation
Conclusion
Affine transformation in OpenCV uses three corresponding points to create a transformation matrix with cv2.getAffineTransform(), then applies it using cv2.warpAffine(). This technique is essential for geometric image corrections and computer vision applications.
