Bird
Raised Fist0
Computer Visionml~20 mins

Homography and image alignment in Computer Vision - ML Experiment: Train & Evaluate

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Experiment - Homography and image alignment
Problem:Align two images of the same scene taken from different viewpoints using homography transformation.
Current Metrics:Alignment error (mean pixel distance) on validation set: 15.2 pixels
Issue:The current homography estimation produces visible misalignment, especially near image edges, indicating inaccurate transformation.
Your Task
Improve the homography estimation to reduce the alignment error to below 8 pixels on the validation set.
Use only feature matching and homography estimation techniques.
Do not use deep learning models or external pretrained networks.
Keep the input images and feature detector type the same.
Hint 1
Hint 2
Hint 3
Solution
Computer Vision
import cv2
import numpy as np

def align_images(img1, img2):
    # Detect ORB features and compute descriptors.
    orb = cv2.ORB_create(nfeatures=5000)
    kp1, des1 = orb.detectAndCompute(img1, None)
    kp2, des2 = orb.detectAndCompute(img2, None)

    # Match features using BFMatcher with Hamming distance.
    bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=False)
    matches = bf.knnMatch(des1, des2, k=2)

    # Apply Lowe's ratio test to filter good matches.
    good_matches = []
    for m, n in matches:
        if m.distance < 0.75 * n.distance:
            good_matches.append(m)

    if len(good_matches) < 4:
        raise ValueError('Not enough good matches to compute homography.')

    # Extract location of good matches.
    src_pts = np.float32([kp1[m.queryIdx].pt for m in good_matches]).reshape(-1, 1, 2)
    dst_pts = np.float32([kp2[m.trainIdx].pt for m in good_matches]).reshape(-1, 1, 2)

    # Compute homography using RANSAC to remove outliers.
    H, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)

    # Warp img1 to align with img2.
    height, width = img2.shape[:2]
    aligned_img = cv2.warpPerspective(img1, H, (width, height))

    return aligned_img, H, mask

# Example usage:
# img1 = cv2.imread('image1.jpg')
# img2 = cv2.imread('image2.jpg')
# aligned_img, H, mask = align_images(img1, img2)
# cv2.imwrite('aligned.jpg', aligned_img)
Increased ORB feature detector to 5000 features for more keypoints.
Used Lowe's ratio test (0.75) to filter out weak matches.
Applied RANSAC in findHomography to robustly estimate homography and remove outliers.
Results Interpretation

Before: Alignment error = 15.2 pixels (visible misalignment)

After: Alignment error = 6.7 pixels (much better alignment)

Using robust matching techniques like Lowe's ratio test and RANSAC for homography estimation greatly improves image alignment by removing bad matches and outliers.
Bonus Experiment
Try using a different feature detector like SIFT or AKAZE and compare the alignment error.
💡 Hint
SIFT and AKAZE may find more distinctive keypoints, which can improve matching quality and homography estimation.

Practice

(1/5)
1. What is the main purpose of computing a homography matrix in image alignment?
easy
A. To increase the brightness of an image
B. To detect edges in an image
C. To segment objects in an image
D. To find a transformation that maps points from one image to another

Solution

  1. Step 1: Understand homography concept

    Homography is a matrix that relates points between two images taken from different views.
  2. Step 2: Identify its use in image alignment

    It helps to map points from one image to corresponding points in another to align them.
  3. Final Answer:

    To find a transformation that maps points from one image to another -> Option D
  4. Quick Check:

    Homography = Point mapping [OK]
Hint: Homography maps points between images [OK]
Common Mistakes:
  • Confusing homography with edge detection
  • Thinking homography changes image brightness
  • Mixing homography with image segmentation
2. Which OpenCV function is used to compute the homography matrix from matched points?
easy
A. cv2.warpPerspective()
B. cv2.findHomography()
C. cv2.matchTemplate()
D. cv2.resize()

Solution

  1. Step 1: Identify function for homography calculation

    cv2.findHomography() computes the homography matrix from matched point sets.
  2. Step 2: Differentiate from other functions

    cv2.warpPerspective applies the homography, matchTemplate finds template matches, resize changes image size.
  3. Final Answer:

    cv2.findHomography() -> Option B
  4. Quick Check:

    Compute homography = findHomography [OK]
Hint: Find homography matrix with cv2.findHomography() [OK]
Common Mistakes:
  • Using warpPerspective to compute homography
  • Confusing template matching with homography calculation
  • Trying to resize image to get homography
3. Given the following code snippet, what will be the shape of aligned_img after applying homography?
import cv2
import numpy as np
img1 = cv2.imread('img1.jpg')
img2 = cv2.imread('img2.jpg')
pts1 = np.array([[10,10],[100,10],[100,100],[10,100]])
pts2 = np.array([[12,14],[102,12],[98,110],[14,108]])
H, _ = cv2.findHomography(pts1, pts2)
aligned_img = cv2.warpPerspective(img1, H, (img2.shape[1], img2.shape[0]))
medium
A. Same height and width as img1
B. Shape is (4, 2) because of points
C. Same height and width as img2
D. Shape depends on H matrix size

Solution

  1. Step 1: Understand warpPerspective parameters

    The third parameter in warpPerspective sets output image size as (width, height).
  2. Step 2: Check given size argument

    It uses (img2.shape[1], img2.shape[0]) which is width and height of img2.
  3. Final Answer:

    Same height and width as img2 -> Option C
  4. Quick Check:

    Output size = img2.shape [OK]
Hint: warpPerspective size param sets output image shape [OK]
Common Mistakes:
  • Assuming output shape matches img1
  • Thinking homography matrix size affects output shape
  • Confusing point arrays with image shape
4. You wrote this code to align two images but get a distorted output. What is the likely error?
H, status = cv2.findHomography(pts1, pts2)
aligned = cv2.warpPerspective(img1, pts1, (img2.shape[1], img2.shape[0]))
medium
A. Using pts1 instead of H in warpPerspective
B. Swapping pts1 and pts2 in findHomography
C. Not converting points to float32
D. Missing cv2.imshow to display image

Solution

  1. Step 1: Check warpPerspective arguments

    warpPerspective expects the homography matrix as the second argument, not point arrays.
  2. Step 2: Identify incorrect argument usage

    Code passes pts1 (points) instead of H (homography matrix), causing distortion.
  3. Final Answer:

    Using pts1 instead of H in warpPerspective -> Option A
  4. Quick Check:

    warpPerspective needs homography matrix [OK]
Hint: Pass homography matrix, not points, to warpPerspective [OK]
Common Mistakes:
  • Passing points instead of homography matrix
  • Swapping source and destination points in findHomography
  • Ignoring data type requirements for points
5. You want to stitch two images taken from different angles into a panorama. Which sequence of steps correctly uses homography for alignment?
hard
A. Detect keypoints -> Match points -> Compute homography -> Warp one image -> Blend images
B. Resize images -> Compute homography -> Detect edges -> Warp images -> Blend images
C. Match points -> Resize images -> Compute homography -> Warp images -> Detect keypoints
D. Warp images -> Detect keypoints -> Compute homography -> Match points -> Blend images

Solution

  1. Step 1: Detect and match keypoints

    First, find keypoints in both images and match them to get corresponding points.
  2. Step 2: Compute homography and warp image

    Use matched points to compute homography, then warp one image to align with the other.
  3. Step 3: Blend images to create panorama

    Finally, blend the aligned images smoothly to form a panorama.
  4. Final Answer:

    Detect keypoints -> Match points -> Compute homography -> Warp one image -> Blend images -> Option A
  5. Quick Check:

    Correct panorama steps = Detect keypoints -> Match points -> Compute homography -> Warp one image -> Blend images [OK]
Hint: Detect -> Match -> Compute -> Warp -> Blend for panorama [OK]
Common Mistakes:
  • Resizing images before matching points
  • Warping images before computing homography
  • Detecting keypoints after warping images