Module 4: Feature Extraction and Matching
1. Learning Objectives
- Understand how to detect and describe local image features
- Compare feature detectors like Harris, FAST, SIFT, SURF, and ORB
- Apply feature matching techniques using OpenCV
- Use feature matches for image alignment and recognition
2. Key Concepts
- Keypoints: Interest points in the image that are repeatable and distinctive (e.g., corners, blobs)
- Descriptors: Vectors that describe the region around a keypoint
- Feature Matching: Comparing descriptors across images to find correspondences
3. Keypoint Detectors
- Harris Corner Detector: Detects corners using gradient-based scoring
- FAST (Features from Accelerated Segment Test): Efficient for real-time keypoint detection
4. Descriptors
- SIFT: Scale-Invariant Feature Transform (good for scale/rotation changes)
- SURF: Speeded-Up Robust Features (patented)
- ORB: Oriented FAST and Rotated BRIEF (efficient and open-source)
5. Feature Matching Techniques
- Brute Force Matcher: Compares descriptors one-by-one
- FLANN (Fast Library for Approximate Nearest Neighbors): Faster for large datasets
6. Hands-On Lab
Use OpenCV to detect keypoints and match features between two uploaded images.
import cv2
import numpy as np
from google.colab.patches import cv2_imshow
from google.colab import files
# Upload two images
uploaded = files.upload()
img1 = cv2.imread(list(uploaded.keys())[0], 0) # Grayscale
uploaded = files.upload()
img2 = cv2.imread(list(uploaded.keys())[0], 0)
# Detect ORB keypoints and descriptors
orb = cv2.ORB_create()
kp1, des1 = orb.detectAndCompute(img1, None)
kp2, des2 = orb.detectAndCompute(img2, None)
# Match descriptors using BFMatcher
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
matches = bf.match(des1, des2)
matches = sorted(matches, key=lambda x: x.distance)
# Draw top 20 matches
matched_img = cv2.drawMatches(img1, kp1, img2, kp2, matches[:20], None, flags=2)
cv2_imshow(matched_img)
7. Real-World Applications
- Panorama stitching
- Object recognition and tracking
- Augmented reality anchor detection
8. Assignment
Objective: Practice keypoint detection and feature matching with real-world images.
- 1. Upload two similar but not identical images (e.g. same object from different angles)
- 2. Use ORB or SIFT to detect and match features
- 3. Visualize and save the top 20 matches
- 4. Optional: Try FLANN for faster matching
- 5. Write a reflection: Which method worked best and why?
Due: End of Week 4