0
0
Computer Visionml~5 mins

Homography and image alignment in Computer Vision

Choose your learning style9 modes available
Introduction
Homography helps us match one picture to another by finding how one image can be shifted, rotated, or stretched to fit the other. This is useful to line up images taken from different angles.
When you want to stitch photos together to make a panorama.
When you want to overlay graphics on a video or image correctly.
When you want to correct the perspective of a photo taken at an angle.
When you want to track objects moving between camera views.
When you want to align images for comparison or analysis.
Syntax
Computer Vision
import cv2
import numpy as np

# Find homography matrix
homography_matrix, status = cv2.findHomography(src_points, dst_points, method=cv2.RANSAC)

# Warp source image to align with destination image
aligned_image = cv2.warpPerspective(source_image, homography_matrix, (width, height))
src_points and dst_points are arrays of matching points between two images.
cv2.findHomography uses RANSAC to ignore bad matches and find the best transformation.
Examples
Basic example with four matching points to find homography.
Computer Vision
src_points = np.array([[10, 20], [50, 80], [90, 40], [120, 100]])
dst_points = np.array([[15, 25], [55, 85], [95, 45], [125, 105]])
homography_matrix, status = cv2.findHomography(src_points, dst_points, cv2.RANSAC)
Edge case: empty points will cause failure; need at least 4 points.
Computer Vision
src_points = np.array([])
dst_points = np.array([])
homography_matrix, status = cv2.findHomography(src_points, dst_points, cv2.RANSAC)
Edge case: only one point is not enough to compute homography.
Computer Vision
src_points = np.array([[10, 20]])
dst_points = np.array([[15, 25]])
homography_matrix, status = cv2.findHomography(src_points, dst_points, cv2.RANSAC)
Sample Model
This program creates a simple square image, rotates it to make a destination image, finds the homography, aligns the source to destination, and checks how close they are.
Computer Vision
import cv2
import numpy as np

# Create two simple images with a square shifted
source_image = np.zeros((200, 200), dtype=np.uint8)
cv2.rectangle(source_image, (50, 50), (150, 150), 255, -1)  # white square

# Destination image has the square shifted and rotated
angle = 10  # degrees
M = cv2.getRotationMatrix2D((100, 100), angle, 1)
destination_image = cv2.warpAffine(source_image, M, (200, 200))

# Define matching points manually (corners of the square)
src_points = np.array([[50, 50], [150, 50], [150, 150], [50, 150]], dtype=np.float32)
dst_points = cv2.transform(np.array([src_points]), M)[0]

# Find homography matrix
homography_matrix, status = cv2.findHomography(src_points, dst_points, cv2.RANSAC)

# Warp source image to align with destination
aligned_image = cv2.warpPerspective(source_image, homography_matrix, (200, 200))

# Check difference between aligned image and destination
difference = cv2.absdiff(aligned_image, destination_image)

# Print homography matrix and sum of difference pixels
print("Homography matrix:")
print(homography_matrix)
print(f"Sum of absolute difference pixels: {np.sum(difference)}")
OutputSuccess
Important Notes
Finding homography requires at least 4 pairs of matching points.
The time complexity depends on the number of points and RANSAC iterations, usually fast for small sets.
Common mistake: Using points that do not correspond well causes wrong homography.
Use homography when images are related by perspective transform; use simpler transforms if only translation or rotation.
Summary
Homography finds how to map one image to another using matching points.
It helps align images taken from different views or angles.
Use cv2.findHomography and cv2.warpPerspective to compute and apply homography.