Skip to content

About

Human motion detection and tracking using HSV skin colour segmentation — built from scratch with Python & OpenCV. CVIP 2022 SLIIT

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

👁️ Human Motion Detection and Tracking

A Computer Vision algorithm built entirely from scratch in Python that detects and tracks a moving human in outdoor video using skin colour segmentation in HSV colour space.

Module: Computer Vision and Image Processing (CVIP) Degree: BSc Engineering Honours — Electrical & Electronic Engineering Author: Sivakumaran Niroshan | ID: EN19361932 | SLIIT | 2022


🎯 Project Overview

In this project, a human motion detection algorithm was developed without using any machine learning or deep learning libraries. The algorithm works by extracting human skin colour features from each video frame using pixel-level manipulation in HSV colour space, then applying custom-built filters and contouring to draw a green bounding box around the detected human.

Final Result ✅

Final Result — Green Bounding Box


🔄 Algorithm Pipeline

Pipeline Diagram

The pipeline processes each video frame through 6 sequential stages, all implemented from scratch:

Step Function Description
1 scale_Frame() Resize to 50% — reduces computation by 75%
2 cv2.COLOR_BGR2HSV Convert to HSV colour space — better skin detection
3 human_Detection_Function() Pixel-by-pixel skin colour segmentation
4 cv2.COLOR_BGR2GRAY Convert to grayscale for filtering
5 blur_Funection() Mean filter (5×5 kernel) — noise reduction
5 gaussian_Funection() Gaussian filter (3×3 kernel) — further smoothing
6 threshold_Function() Binary threshold — separate human from background
7 contour_funection() Find bounding corners → draw green rectangle

📸 Step-by-Step Results

Step 1 — Original Frame (Resized to 50%)

Original Frame


Step 2 — HSV Colour Space Conversion

HSV Frame

Skin tones appear as warm orange/yellow. HSV separates colour (hue) from brightness, making skin detection robust to lighting changes.


Step 3 — Human Skin Segmentation

Skin Detection

Only pixels within the skin HSV range [0,10,60]–[20,150,255] are kept. All other pixels become black.


Step 4 — Grayscale Conversion

Grayscale

Reduces the 3-channel colour image to a single channel for faster convolution filtering.


Step 5 — Mean Filter (5×5 Kernel)

Mean Filter

Noise is reduced by replacing each pixel with the average of its 5×5 neighbourhood. Manual convolution — no cv2.blur() used.


Step 6 — Gaussian Filter (3×3 Kernel)

Gaussian Filter

Further smoothing using weighted convolution. Centre pixel gets more weight (4/16) than edges (1/16).


Step 7 — Final Output: Green Bounding Box ✅

Final Bounding Box

The contour function finds the top-left and bottom-right corners of all white pixels and draws a green rectangle around the human.


🧠 Key Technical Concepts

Why HSV instead of RGB?

HSV (Hue-Saturation-Value) isolates colour information in the Hue channel independently from brightness. This means the skin colour range stays consistent under different lighting conditions — bright sunlight, shade, etc. RGB mixes colour and brightness making consistent thresholding much harder.

# Skin colour range in HSV
lower_Skin_Colour_Limit = np.array([0, 10, 60],    dtype="uint8")
upper_Skin_Colour_Limit = np.array([20, 150, 255], dtype="uint8")

Mean Filter vs Gaussian Filter

Filter Kernel Behaviour
Mean (5×5) All weights = 1/25 Uniform averaging — stronger smoothing
Gaussian (3×3) Centre weighted (4/16) Edge-preserving smoothing

Bounding Box Algorithm

The contour function manually scans all white pixels to find:

  • Top-left corner: minimum row + minimum column
  • Bottom-right corner: maximum row + maximum column
cv2.rectangle(frame, (top_left_x, top_left_y),
              (bottom_right_x, bottom_right_y), (0, 255, 0), 3)

🗂️ Project Structure

SLIIT-CVIP/
├── EN19361932.py              # Main algorithm — fully commented
├── images/
│   ├── pipeline/              # Diagrams and code screenshots
│   │   ├── 01-project-overview-diagram.png
│   │   ├── 02-image-processing-applications.png
│   │   ├── 03-mean-filter-code.png
│   │   └── 04-contour-function-code.png
│   └── results/               # Output at each pipeline stage
│       ├── 01-original-frame.png
│       ├── 02-hsv-colour-space-frame.png
│       ├── 03-human-detection-skin-segmentation.png
│       ├── 04-grayscale-frame.png
│       ├── 05-mean-filter-output.png
│       ├── 06-gaussian-filter-output.png
│       └── 07-final-green-bounding-box.png
├── .gitignore
└── README.md

▶️ How to Run

Prerequisites

pip install opencv-python numpy vidgear

Run

python EN19361932.py

Place your input video as n22.mp4 in the same directory. Output video will be saved as Out_1.mp4.

Controls

  • Press Q to stop processing early
  • All pipeline stages are displayed in separate windows in real time

⚙️ Dependencies

Library Purpose
opencv-python (cv2) Video capture, colour conversion, display
numpy Array operations and kernel matrices
vidgear Compressed H.264 video output writing

📋 Limitations & Future Work

  • Algorithm is sensitive to objects with similar skin colour (e.g. wooden surfaces)
  • Per-pixel loops in Python are slow — could be vectorised with NumPy
  • Works best for single human tracking in outdoor environments
  • Future improvement: Replace skin detection with a CNN-based detector for better accuracy across multiple people and lighting conditions

👤 Author

Sivakumaran Niroshan | Student ID: EN19361932 Module: Computer Vision and Image Processing Degree: BSc Engineering Honours — Electrical & Electronic Engineering Institute: SLIIT — Sri Lanka Institute of Information Technology Year: 2022

About

Human motion detection and tracking using HSV skin colour segmentation — built from scratch with Python & OpenCV. CVIP 2022 SLIIT

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages