HOG and SVM - iffatAGheyas/computer-vision-handbook GitHub Wiki

🧭 HOG + SVM for Object Detection

Histogram of Oriented Gradients (HOG) paired with a linear Support Vector Machine (SVM) forms a fast, shape-based detector—particularly effective for pedestrian detection in surveillance and robotics.


🧠 How It Works

  • HOG (Histogram of Oriented Gradients):
    Divides the image into small cells, computes a histogram of gradient directions for each cell, then concatenates these into a feature vector.

  • SVM (Support Vector Machine):
    A linear classifier that learns to separate feature vectors of target objects (e.g. people) from background.

Pipeline:

  1. HOG extracts gradient-based features
  2. SVM classifies each window → object detection

🚀 Benefits

Advantage Details
Shape-based Works well on rigid objects like humans
No deep learning required Fast, lightweight inference
Pretrained pedestrian detector Available out-of-the-box in OpenCV
Broad applicability Still used in surveillance, robotics, automotive

🐍 Step-by-Step Code: Human Detection in OpenCV

OpenCV includes a built-in HOG+SVM pedestrian detector:

import cv2
import matplotlib.pyplot as plt

# Load and resize the image
img = cv2.imread("people.jpg")
img = cv2.resize(img, (640, 480))

# Initialize HOG descriptor + default people detector
hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())

# Detect people
(rects, weights) = hog.detectMultiScale(
    img,
    winStride=(8, 8),
    padding=(8, 8),
    scale=1.05
)

# Draw bounding boxes
for (x, y, w, h) in rects:
    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)

# Display result
plt.figure(figsize=(10, 6))
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.title("HOG + SVM Pedestrian Detection")
plt.axis("off")
plt.show()

image

🔍 Key Steps Explained

Function Purpose
HOGDescriptor() Create HOG feature extractor
setSVMDetector() Load pretrained SVM classifier
detectMultiScale() Scan image at multiple scales for detections
winStride & padding Control sliding-window step and overlap
scale Pyramid scaling factor between passes

👁️ What HOG Sees

  1. Divide the image into overlapping cells
  2. Compute gradient-orientation histogram for each cell
  3. Combine histograms into a global feature vector
  4. Classify with SVM: region contains an object or background

Summary

Component Role
HOG Gradient-based feature extractor
SVM Linear classifier (decision boundary)
Use Case Pedestrian detection in surveillance