Feature Extraction

Feature Extraction Overview

Feature extraction: Converting raw image into meaningful measurements

Types:

  • Low-level features: Edges, corners, colors, textures
  • Mid-level features: SIFT, SURF, ORB descriptors
  • High-level features: Object parts, semantic concepts (CNN)

Purpose: Reduce dimensionality while preserving important information

Histogram of Oriented Gradients (HOG)

HOG: Describes local object appearance using gradient orientations

Algorithm:

  1. Compute gradients (magnitude + direction)
  2. Divide image into cells (e.g., 8×8 pixels)
  3. Compute histogram of gradients per cell (9 bins)
  4. Group cells into blocks (e.g., 2×2 cells)
  5. Normalize histograms within blocks
  6. Concatenate all histograms → feature vector

Use case: Pedestrian detection, object recognition

HOG Parameters

Key parameters:

  • Cell size: 8×8 or 16×16 pixels
  • Block size: 2×2 or 3×3 cells
  • Orientations: 9 bins (0-180° for unsigned gradient)
  • Block stride: Overlap between blocks

Feature vector size:

  • Example: 64×128 image, 8×8 cells, 2×2 blocks, 9 bins
  • = 7 blocks × 15 blocks × 4 cells × 9 bins = 3780 dimensions

HOG in Python

  1. from skimage.feature import hog
  2. from skimage import exposure
  3. import cv2
  4. # load image
  5. img = cv2.imread('person.jpg')
  6. gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  7. # compute HOG features
  8. #ans: features, hog_image = hog(gray, orientations=9, pixels_per_cell=(8, 8), cells_per_block=(2, 2), visualize=True)
  9. # features: HOG descriptor vector
  10. #ans: print(features.shape) # (N,) 1D array
  11. # hog_image: visualization of gradients
  12. #ans: hog_image = exposure.rescale_intensity(hog_image, out_range=(0, 255))
  13. #ans: hog_image = np.uint8(hog_image)

Local Binary Patterns (LBP)

LBP: Texture descriptor

Algorithm:

  1. For each pixel, compare with 8 neighbors
  2. If neighbor ≥ center: 1, else: 0
  3. Read clockwise → 8-bit binary number
  4. Convert to decimal (0-255)

Properties:

  • Rotation invariant (optional)
  • Grayscale invariant
  • Fast to compute

Use case: Texture classification, face recognition

LBP Implementation

  1. from skimage.feature import local_binary_pattern
  2. # compute LBP
  3. radius = 3
  4. n_points = 8 * radius
  5. #ans: lbp = local_binary_pattern(gray, n_points, radius, method='uniform')
  6. # method options:
  7. #ans: 'default': original LBP
  8. #ans: 'uniform': rotation invariant (most common)
  9. #ans: 'var': variance-based
  10. # create histogram
  11. #ans: hist, _ = np.histogram(lbp.ravel(), bins=np.arange(0, n_points + 3), range=(0, n_points + 2))
  12. #ans: hist = hist.astype('float')
  13. #ans: hist /= (hist.sum() + 1e-7) # normalize

Color Histograms

Color histogram: Distribution of colors in image

Process:

  1. Choose color space (RGB, HSV, LAB)
  2. Divide each channel into bins
  3. Count pixels in each bin
  4. Normalize (make sum = 1)

Use case: Image retrieval, color-based matching

Color Histogram in OpenCV

  1. # compute histogram for each channel
  2. #ans: hist_b = cv2.calcHist([img], [0], None, [256], [0, 256])
  3. #ans: hist_g = cv2.calcHist([img], [1], None, [256], [0, 256])
  4. #ans: hist_r = cv2.calcHist([img], [2], None, [256], [0, 256])
  5. # parameters:
  6. #ans: [img]: source image
  7. #ans: [0]: channel index (0=B, 1=G, 2=R)
  8. #ans: None: no mask
  9. #ans: [256]: number of bins
  10. #ans: [0, 256]: range
  11. # normalize
  12. #ans: hist_b = cv2.normalize(hist_b, hist_b).flatten()

HSV Histogram

  1. # convert to HSV
  2. hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
  3. # compute 2D histogram (Hue vs Saturation)
  4. #ans: hist = cv2.calcHist([hsv], [0, 1], None, [180, 256], [0, 180, 0, 256])
  5. #ans: channels [0,1] = Hue and Saturation
  6. #ans: bins [180, 256] = 180 for H, 256 for S
  7. # normalize
  8. #ans: cv2.normalize(hist, hist, 0, 255, cv2.NORM_MINMAX)

Haar Cascades

Haar cascades: Pre-trained classifiers for object detection

Based on:

  • Haar-like features (rectangular patterns)
  • Cascade of weak classifiers
  • AdaBoost training

Pre-trained models: Face, eyes, smile, full body, etc.

Advantage: Fast, real-time detection

Haar Cascade in OpenCV

  1. # load pre-trained face detector
  2. #ans: face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
  3. # detect faces
  4. gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  5. #ans: faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
  6. # faces: array of [x, y, w, h]
  7. #ans: for (x, y, w, h) in faces:
  8. #ans: cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)

Template Matching

Template matching: Find template image in larger image

Methods:

  • TM_CCOEFF_NORMED: Correlation coefficient (best)
  • TM_SQDIFF_NORMED: Squared difference
  • TM_CCORR_NORMED: Cross-correlation

Limitation: Not scale or rotation invariant

Template Matching in OpenCV

  1. # load template
  2. template = cv2.imread('template.jpg', 0)
  3. w, h = template.shape[::-1]
  4. # template matching
  5. #ans: result = cv2.matchTemplate(gray, template, cv2.TM_CCOEFF_NORMED)
  6. # find location of best match
  7. #ans: min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
  8. # for CCOEFF_NORMED, higher is better
  9. #ans: top_left = max_loc
  10. #ans: bottom_right = (top_left[0] + w, top_left[1] + h)
  11. #ans: cv2.rectangle(img, top_left, bottom_right, (0, 255, 0), 2)

Exercises - Part 1 (Concepts)

  1. # what is HOG?
  2. #ans: Histogram of Oriented Gradients, describes object appearance
  3. # what does LBP encode?
  4. #ans: texture pattern by comparing pixel with neighbors
  5. # what is color histogram?
  6. #ans: distribution of colors in image
  7. # what are Haar cascades?
  8. #ans: pre-trained classifiers using Haar-like features
  9. # template matching limitation?
  10. #ans: not scale or rotation invariant

Exercises - Part 2 (Concepts)

  1. # HOG typical cell size?
  2. #ans: 8×8 or 16×16 pixels
  3. # LBP output range?
  4. #ans: 0-255 (8-bit patterns from 8 neighbors)
  5. # why normalize histograms?
  6. #ans: make scale-invariant, sum to 1
  7. # what is cascade in Haar cascades?
  8. #ans: series of weak classifiers, fast rejection
  9. # best template matching method?
  10. #ans: TM_CCOEFF_NORMED (normalized correlation coefficient)

Exercises - Part 3 (Coding)

  1. # compute HOG features
  2. from skimage.feature import hog
  3. #ans: features, hog_img = hog(gray, orientations=9, pixels_per_cell=(8, 8), cells_per_block=(2, 2), visualize=True)
  4. # compute LBP
  5. from skimage.feature import local_binary_pattern
  6. #ans: lbp = local_binary_pattern(gray, 24, 3, method='uniform')
  7. # color histogram
  8. #ans: hist_b = cv2.calcHist([img], [0], None, [256], [0, 256])
  9. #ans: hist_b = cv2.normalize(hist_b, hist_b).flatten()

Exercises - Part 4 (Coding)

  1. # face detection with Haar cascade
  2. #ans: face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
  3. gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  4. #ans: faces = face_cascade.detectMultiScale(gray, 1.1, 5, minSize=(30, 30))
  5. #ans: for (x, y, w, h) in faces:
  6. #ans: cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)

Exercises - Part 5 (Mixed)

  1. # template matching
  2. template = cv2.imread('template.jpg', 0)
  3. w, h = template.shape[::-1]
  4. #ans: result = cv2.matchTemplate(gray, template, cv2.TM_CCOEFF_NORMED)
  5. #ans: min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
  6. #ans: top_left = max_loc
  7. #ans: cv2.rectangle(img, top_left, (top_left[0]+w, top_left[1]+h), (0, 255, 0), 2)
  8. # HSV histogram
  9. hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
  10. #ans: hist = cv2.calcHist([hsv], [0, 1], None, [180, 256], [0, 180, 0, 256])

Google tag (gtag.js)