Skip to content

VidhanGupta-01/Color-Based-Object-Tracking

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

Color-Based Object Detection & Tracking

A real-time color-based object detection and tracking system built with Python and OpenCV. Supports 5 switchable detection modes, multi-object tracking, motion trail visualization, and a live HSV tuning interface.


Features

  • 5 Detection Modes — Green, Red, Blue, Multi (all 3 simultaneously), and Custom HSV slider mode
  • Dual-range red masking — handles red's hue wrapping at 0°/180° in HSV color space
  • 30-point motion trail — visualizes object movement history per color channel
  • 3-step mask cleaning pipeline — median blur → morphological open → morphological close for noise-robust detection
  • Live HSV tuner — interactive trackbar controls to dial in custom color ranges in real time
  • Contour-based tracking — bounding boxes and center-point labels rendered on the largest detected contour per color

Demo

Mode Description
S Custom HSV slider mode — tune your own color range live
G Green object tracking
R Red object tracking (dual-range HSV)
B Blue object tracking
M Multi-color mode — tracks Green, Red, and Blue simultaneously

How It Works

1. Color Space Conversion

Each frame is converted from BGR to HSV, which separates color (hue) from lighting (value) — making detection far more robust under changing light conditions than RGB.

2. Masking

  • Green and Blue use a single HSV range via cv2.inRange()
  • Red uses two ranges combined with bitwise OR, since red straddles the 0°/180° boundary in the HSV hue wheel

3. Mask Cleaning

Each mask goes through a 3-step pipeline:

Median Blur (5x5)        → removes salt-and-pepper noise
Morphological Open       → eliminates small false-positive blobs
Morphological Close      → fills small holes inside detected regions

4. Tracking

  • Contours are extracted with cv2.findContours()
  • The largest contour is selected as the tracked object
  • A bounding box and color label are drawn around it
  • The center point is appended to a rolling 30-point trail, drawn as connected line segments

Project Structure

Color-Based-Object-Tracking/
├── main.py               # Main application loop

Installation

git clone https://github.com/VidhanGupta-01/Color-Based-Object-Tracking.git
cd Color-Based-Object-Tracking
pip install opencv-python numpy

Usage

python main.py

Once running, use keyboard shortcuts to switch modes:

Key Action
S Switch to HSV slider (custom) mode
G Switch to green tracking mode
R Switch to red tracking mode
B Switch to blue tracking mode
M Switch to multi-color tracking mode
Q Quit

Three windows will open:

  • Original — live feed with tracking overlay
  • HSV — raw HSV frame
  • Mask — current active detection mask

Requirements

opencv-python
numpy

A working webcam is required.


Tech Stack

  • Python 3
  • OpenCV — camera capture, HSV conversion, contour detection, drawing
  • NumPy — array operations for HSV range definitions

Author

Developed by Vidhan Gupta
GitHub · LinkedIn

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages