Feature Extraction
Module: Computer Vision | Difficulty: Intermediate
Classical Feature Detectors
Harris Corner Detector
The corner response function:
where
SIFT Features
Scale-Invariant Feature Transform detects keypoints at multiple scales:
- Build scale-space:
- Detect extrema in Difference of Gaussians
- Assign orientation based on gradient histogram
- Compute 128-dimensional descriptor
ORB (Oriented FAST and Rotated BRIEF)
Efficient alternative combining FAST keypoint detector with BRIEF descriptor, adding rotation invariance.
Feature Matching with BFMatcher
import cv2
import numpy as np
def match_features(img1, img2, method='orb'):
if method == 'orb':
detector = cv2.ORB_create(nfeatures=1000)
elif method == 'sift':
detector = cv2.SIFT_create()
kp1, des1 = detector.detectAndCompute(img1, None)
kp2, des2 = detector.detectAndCompute(img2, None)
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
matches = bf.match(des1, des2)
matches = sorted(matches, key=lambda x: x.distance)
return kp1, kp2, matches
def compute_homography(kp1, kp2, matches):
pts1 = np.float32([kp1[m.queryIdx].pt for m in matches])
pts2 = np.float32([kp2[m.trainIdx].pt for m in matches])
H, mask = cv2.findHomography(pts1, pts2, cv2.RANSAC, 5.0)
return H, mask
Key Takeaways
- SIFT provides scale and rotation invariance
- ORB offers real-time performance with competitive accuracy
- Deep features (from CNNs) now outperform hand-crafted descriptors