Feature Detection

What are Features?

Feature: Distinctive pattern in an image that can be reliably detected

Good features:

  • Corners: Intersection of edges (high gradient in multiple directions)
  • Blobs: Regions different from surroundings
  • Edges: Boundaries between regions

Why detect features?

  • Object recognition
  • Image matching
  • Tracking
  • 3D reconstruction
  • Image stitching

Corner Detection

Corner: Point where two edges meet (intensity change in multiple directions)

Properties:

  • High gradient in multiple directions
  • Locally unique
  • Repeatable (detected consistently)
  • Invariant to rotation

Use case: Tracking, matching, calibration

Harris Corner Detector

Harris algorithm: Measures corner-ness using gradient matrix

Formula: R = det(M) - k × trace(M)²

Where M is gradient matrix:

R > threshold: Corner detected

Harris Corner in OpenCV

  1. import cv2
  2. import numpy as np
  3. gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  4. gray = np.float32(gray)
  5. # detect harris corners
  6. #ans: corners = cv2.cornerHarris(gray, blockSize=2, ksize=3, k=0.04)
  7. # blockSize: neighborhood size
  8. #ans: size of window to compute gradient
  9. # ksize: Sobel kernel size
  10. #ans: for computing gradients (3, 5, 7)
  11. # k: Harris parameter
  12. #ans: typically 0.04-0.06, sensitivity parameter
  13. # threshold to mark corners
  14. corners = cv2.dilate(corners, None)
  15. img[corners > 0.01 * corners.max()] = [0, 0, 255]
  16. #ans: marks corners in red where response > threshold

Shi-Tomasi Corner Detector

Shi-Tomasi: Improved Harris, better for tracking

Formula: R = min(λ1, λ2) where λ are eigenvalues

Advantage: More reliable corner selection

Use case: Optical flow, tracking

Shi-Tomasi in OpenCV

  1. # detect good features to track
  2. #ans: corners = cv2.goodFeaturesToTrack(gray, maxCorners=100, qualityLevel=0.01, minDistance=10)
  3. # maxCorners: maximum number to return
  4. #ans: 100 strongest corners
  5. # qualityLevel: minimum quality (0-1)
  6. #ans: 0.01 means keep corners > 1% of best
  7. # minDistance: minimum pixel distance between corners
  8. #ans: 10 pixels apart
  9. # draw corners
  10. #ans: for corner in corners:
  11. #ans: x, y = corner.ravel()
  12. #ans: cv2.circle(img, (int(x), int(y)), 3, (0, 255, 0), -1)

FAST Corner Detector

FAST: Features from Accelerated Segment Test

Algorithm:

  1. Pick pixel p, intensity Ip
  2. Consider circle of 16 pixels around p
  3. If N contiguous pixels all brighter/darker than Ip ± threshold → corner
  4. N typically 12 (FAST-12)

Advantage: Very fast, real-time capable

FAST in OpenCV

  1. # create FAST detector
  2. #ans: fast = cv2.FastFeatureDetector_create()
  3. # detect keypoints
  4. #ans: keypoints = fast.detect(gray, None)
  5. # draw keypoints
  6. #ans: img_kp = cv2.drawKeypoints(img, keypoints, None, color=(0, 255, 0))
  7. # adjust threshold
  8. fast.setThreshold(30)
  9. #ans: higher threshold = fewer keypoints, stronger corners
  10. # control non-max suppression
  11. fast.setNonmaxSuppression(True)
  12. #ans: removes adjacent weak corners

Blob Detection

Blob: Region significantly different from surroundings

SimpleBlobDetector: Detects bright/dark circular regions

Parameters:

  • Color (bright/dark blobs)
  • Size (min/max radius)
  • Circularity
  • Convexity
  • Inertia

Blob Detection in OpenCV

  1. # create blob detector with default params
  2. #ans: detector = cv2.SimpleBlobDetector_create()
  3. # detect blobs
  4. #ans: keypoints = detector.detect(gray)
  5. # draw blobs
  6. #ans: img_blobs = cv2.drawKeypoints(img, keypoints, None, (0, 0, 255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
  7. # custom parameters
  8. params = cv2.SimpleBlobDetector_Params()
  9. params.filterByArea = True
  10. params.minArea = 100
  11. params.maxArea = 5000
  12. params.filterByCircularity = True
  13. params.minCircularity = 0.8
  14. #ans: detector = cv2.SimpleBlobDetector_create(params)
  15. #ans: detects circular blobs 100-5000 pixels, circularity > 0.8

Exercises - Part 1 (Concepts)

  1. # what is a corner?
  2. #ans: point with high gradient in multiple directions
  3. # why detect features?
  4. #ans: object recognition, matching, tracking, stitching
  5. # what does Harris detect?
  6. #ans: corners using gradient matrix
  7. # difference between Harris and Shi-Tomasi?
  8. #ans: Shi-Tomasi uses min(λ1,λ2), better for tracking
  9. # what is FAST?
  10. #ans: fast corner detector using circle of pixels

Exercises - Part 2 (Concepts)

  1. # what is a blob?
  2. #ans: region significantly different from surroundings
  3. # what is k parameter in Harris?
  4. #ans: sensitivity, typically 0.04-0.06
  5. # what is qualityLevel in goodFeaturesToTrack?
  6. #ans: minimum quality as fraction of best corner (0-1)
  7. # why FAST is fast?
  8. #ans: simple comparison test, no gradient computation
  9. # what does minDistance parameter do?
  10. #ans: ensures corners are at least N pixels apart

Exercises - Part 3 (Coding)

  1. # detect Harris corners
  2. gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  3. gray = np.float32(gray)
  4. #ans: corners = cv2.cornerHarris(gray, 2, 3, 0.04)
  5. # detect Shi-Tomasi corners
  6. #ans: corners = cv2.goodFeaturesToTrack(gray, 100, 0.01, 10)
  7. # create FAST detector
  8. #ans: fast = cv2.FastFeatureDetector_create()
  9. #ans: keypoints = fast.detect(gray, None)

Exercises - Part 4 (Coding)

  1. # detect blobs
  2. #ans: detector = cv2.SimpleBlobDetector_create()
  3. #ans: keypoints = detector.detect(gray)
  4. # draw Harris corners
  5. corners = cv2.cornerHarris(gray, 2, 3, 0.04)
  6. corners = cv2.dilate(corners, None)
  7. #ans: img[corners > 0.01 * corners.max()] = [0, 0, 255]
  8. # draw Shi-Tomasi corners
  9. corners = cv2.goodFeaturesToTrack(gray, 100, 0.01, 10)
  10. #ans: for corner in corners:
  11. #ans: x, y = corner.ravel()
  12. #ans: cv2.circle(img, (int(x), int(y)), 3, (255, 0, 0), -1)

Exercises - Part 5 (Mixed)

  1. # adjust FAST threshold
  2. fast = cv2.FastFeatureDetector_create()
  3. #ans: fast.setThreshold(50)
  4. #ans: keypoints = fast.detect(gray, None)
  5. # detect top 50 Shi-Tomasi corners
  6. #ans: corners = cv2.goodFeaturesToTrack(gray, 50, 0.01, 10)
  7. # custom blob parameters
  8. params = cv2.SimpleBlobDetector_Params()
  9. #ans: params.filterByArea = True
  10. #ans: params.minArea = 50
  11. #ans: params.maxArea = 1000
  12. #ans: detector = cv2.SimpleBlobDetector_create(params)

Google tag (gtag.js)