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 Perspective Transformations on an image using OpenCV Python?
In Perspective Transformation, straight lines remain straight even after the transformation. To apply a perspective transformation, we need a 3×3 perspective transformation matrix and four points on both the input and output images.
Key Functions
We use cv2.getPerspectiveTransform() to find the transformation matrix ?
M = cv2.getPerspectiveTransform(pts1, pts2)
Where:
pts1 ? An array of four points on the input image
pts2 ? An array of corresponding four points on the output image
To apply the transformation, we use cv2.warpPerspective() ?
dst = cv2.warpPerspective(img, M, (width, height))
Where:
img ? The image to be transformed
M ? Perspective transformation matrix
(width, height) ? Dimensions of the output image
Basic Example
Let's apply perspective transformation to correct a skewed rectangular object ?
import cv2
import numpy as np
# Read the input image
img = cv2.imread('/python/opencv/images/warning_wall.jpg')
# Get image dimensions
rows, cols, ch = img.shape
# Define four points on the skewed rectangle in input image
pts1 = np.float32([[56,65], [368,52], [28,387], [389,390]])
# Define corresponding points for a perfect rectangle in output
pts2 = np.float32([[0,0], [300,0], [0,300], [300,300]])
# Calculate perspective transform matrix
M = cv2.getPerspectiveTransform(pts1, pts2)
# Apply perspective transformation
dst = cv2.warpPerspective(img, M, (300, 300))
# Display results
cv2.imshow('Original Image', img)
cv2.imshow('Transformed Image', dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
Advanced Example with Matplotlib
This example shows side-by-side comparison using matplotlib ?
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Read image in grayscale
img = cv2.imread('/python/opencv/images/warning_wall.jpg', 0)
# Define source and destination points
pts1 = np.float32([[56,65], [368,52], [28,387], [389,390]])
pts2 = np.float32([[0,0], [400,0], [0,400], [400,400]])
# Get transformation matrix
M = cv2.getPerspectiveTransform(pts1, pts2)
# Apply transformation with custom output size
dst = cv2.warpPerspective(img, M, (400, 400))
# Display using matplotlib
plt.figure(figsize=(12, 5))
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('Perspective Corrected')
plt.axis('off')
plt.tight_layout()
plt.show()
Common Use Cases
| Application | Purpose | Example |
|---|---|---|
| Document Scanning | Correct skewed documents | Straighten photographed papers |
| Computer Vision | Object recognition | Normalize object orientation |
| Image Rectification | Geometric correction | Fix camera distortion |
Key Points
Always define points in the same order (e.g., top-left, top-right, bottom-left, bottom-right)
Use
np.float32()for point arrays to ensure proper data typeOutput dimensions can be different from input dimensions
The transformation preserves straight lines but changes angles and distances
Conclusion
Perspective transformation is essential for correcting geometric distortions in images. Use cv2.getPerspectiveTransform() to calculate the matrix and cv2.warpPerspective() to apply the transformation with your desired output dimensions.
