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
- Python
- C++
Basic Object Detection
- Python
- C++
Nested Detection (Faces and Eyes)
Based on OpenCV’s facedetect.py sample:- Python
- C++
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:- Python
- C++
Real-time Detection on Video
- Python
- C++
Available Pre-trained Cascades
OpenCV includes many pre-trained cascade classifiers:Face Detection Cascades
Face Detection Cascades
haarcascade_frontalface_default.xml- General frontal face detectionhaarcascade_frontalface_alt.xml- Alternative frontal facehaarcascade_frontalface_alt2.xml- Another alternativehaarcascade_profileface.xml- Profile (side) faceslbpcascade_frontalface.xml- LBP-based face detection (faster)
Eye Detection Cascades
Eye Detection Cascades
haarcascade_eye.xml- General eye detectionhaarcascade_eye_tree_eyeglasses.xml- Eyes with glasseshaarcascade_lefteye_2splits.xml- Left eyehaarcascade_righteye_2splits.xml- Right eye
Body and Gesture Cascades
Body and Gesture Cascades
haarcascade_fullbody.xml- Full body detectionhaarcascade_upperbody.xml- Upper bodyhaarcascade_lowerbody.xml- Lower bodyhaarcascade_smile.xml- Smile detection
Other Object Cascades
Other Object Cascades
haarcascade_frontalcatface.xml- Cat face detectionhaarcascade_frontalcatface_extended.xml- Extended cat facehaarcascade_licence_plate_rus_16stages.xml- Russian license plates
Custom Cascade Training
You can train custom cascade classifiers for specific objects:Collect Training Data
Gather positive samples (images containing the object) and negative samples (images without the object).
Create Sample Description
Create text files listing the locations of positive samples and paths to negative samples.
Train Cascade
Use
opencv_traincascade to train the classifier. This can take hours or days depending on data size.Performance Optimization
- Python
- C++
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
minNeighborsto reduce false positives - Set appropriate
minSizeto skip small detections
Next Steps
- Learn Face Detection for specialized face detection techniques
- Explore Deep Learning for more accurate modern detection methods
- Try Video Processing to apply detection to video streams
