Skip to main content

Object Detection

Learn how to detect objects in images and video using classical computer vision techniques including Haar cascades and Histogram of Oriented Gradients (HOG) detectors.

Haar Cascade Classifiers

Haar cascades are machine learning-based classifiers trained to detect specific objects. OpenCV comes with pre-trained models for faces, eyes, pedestrians, and more.

Loading Cascade Classifiers

Basic Object Detection

Nested Detection (Faces and Eyes)

Based on OpenCV’s facedetect.py sample:
Key parameters for detectMultiScale():
  • scaleFactor: How much the image size is reduced at each scale (1.1 = 10% reduction). Smaller values are more thorough but slower.
  • minNeighbors: How many neighbors each candidate rectangle should retain. Higher values result in fewer but more accurate detections.
  • minSize: Minimum object size. Objects smaller than this are ignored.

HOG (Histogram of Oriented Gradients) Detector

HOG descriptors are excellent for pedestrian detection.

People Detection with HOG

Based on OpenCV’s peopledetect.py sample:

Real-time Detection on Video

Available Pre-trained Cascades

OpenCV includes many pre-trained cascade classifiers:
  • haarcascade_frontalface_default.xml - General frontal face detection
  • haarcascade_frontalface_alt.xml - Alternative frontal face
  • haarcascade_frontalface_alt2.xml - Another alternative
  • haarcascade_profileface.xml - Profile (side) faces
  • lbpcascade_frontalface.xml - LBP-based face detection (faster)
  • haarcascade_eye.xml - General eye detection
  • haarcascade_eye_tree_eyeglasses.xml - Eyes with glasses
  • haarcascade_lefteye_2splits.xml - Left eye
  • haarcascade_righteye_2splits.xml - Right eye
  • haarcascade_fullbody.xml - Full body detection
  • haarcascade_upperbody.xml - Upper body
  • haarcascade_lowerbody.xml - Lower body
  • haarcascade_smile.xml - Smile detection
  • haarcascade_frontalcatface.xml - Cat face detection
  • haarcascade_frontalcatface_extended.xml - Extended cat face
  • haarcascade_licence_plate_rus_16stages.xml - Russian license plates

Custom Cascade Training

You can train custom cascade classifiers for specific objects:
1

Collect Training Data

Gather positive samples (images containing the object) and negative samples (images without the object).
2

Create Sample Description

Create text files listing the locations of positive samples and paths to negative samples.
3

Generate Samples

Use opencv_createsamples to generate training samples from your positive images.
4

Train Cascade

Use opencv_traincascade to train the classifier. This can take hours or days depending on data size.
5

Test and Refine

Test the classifier and collect more samples if needed to improve accuracy.
Training custom cascades requires:
  • Hundreds to thousands of positive samples
  • Even more negative samples
  • Significant computation time (can take days)
  • Careful parameter tuning
For most modern applications, consider using deep learning-based detection instead.

Performance Optimization

Performance tips:
  • Process at lower resolution (0.5x or 0.25x scale)
  • Use histogram equalization on grayscale images
  • Adjust scaleFactor (larger = faster but less accurate)
  • Increase minNeighbors to reduce false positives
  • Set appropriate minSize to skip small detections

Next Steps