Skip to content

Latest commit

 

History

5 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Moving Object Detection

A real-time motion detection system built with OpenCV, tracking iterative improvements from a simple baseline to adaptive background modeling.

Overview

This project detects moving objects in a live webcam feed by comparing frames and flagging regions with significant pixel-level change. It's built as a learning project to demonstrate progression from a naive technique to a more robust, production-style approach.

Version History

v1 — Static-Frame Differencing (v1-baseline)

The initial approach: captures a single reference frame at startup and compares every subsequent frame against it using absolute differencing and a fixed global threshold.

Limitations identified:

  • Reference frame never updates — fragile to lighting changes or scene drift
  • Fixed global threshold with no adaptive modeling
  • No shadow handling

File: src/detector_v1.py

v2 — Adaptive Background Subtraction with MOG2 (v2-mog2-upgrade)

Replaces the static reference frame with cv2.createBackgroundSubtractorMOG2, a Gaussian Mixture Model that continuously updates its background estimate per pixel.

Improvements:

  • Background model adapts to gradual lighting/scene changes
  • Adaptive per-pixel thresholding instead of one fixed global value
  • Built-in shadow detection and filtering
  • Corrected area-filter logic (flags large contours, not small noise)

File: src/detector_v2.py

Tech Stack

  • Python 3
  • OpenCV (cv2)
  • imutils

Setup

git clone https://github.com/<your-username>/moving-object-detection.git
cd moving-object-detection
pip install -r requirements.txt

Usage

Run the latest version:

python src/detector_v2.py

Or run the original baseline for comparison:

python src/detector_v1.py

Press q to quit the camera feed window.

How It Works (v2)

  1. Capture frame from webcam, resize and convert to grayscale
  2. Apply MOG2 background subtractor to get a foreground mask
  3. Threshold out shadow pixels, dilate to merge nearby regions
  4. Find contours in the mask; filter by area to ignore small noise
  5. Draw bounding boxes around detected moving objects

Roadmap

  • Multi-object tracking with unique IDs (e.g. centroid tracking or cv2.Tracker APIs)
  • Save/export detection events with timestamps
  • Optional deep learning-based detection (e.g. YOLO) as a v3 comparison

License

MIT — see LICENSE

About

Real-time motion detection using OpenCV — from naive frame differencing to adaptive MOG2 background subtraction, with version-tagged iterative improvements.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages