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
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.
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 |
Skin tones appear as warm orange/yellow. HSV separates colour (hue) from brightness, making skin detection robust to lighting changes.
Only pixels within the skin HSV range
[0,10,60]–[20,150,255]are kept. All other pixels become black.
Reduces the 3-channel colour image to a single channel for faster convolution filtering.
Noise is reduced by replacing each pixel with the average of its 5×5 neighbourhood. Manual convolution — no cv2.blur() used.
Further smoothing using weighted convolution. Centre pixel gets more weight (4/16) than edges (1/16).
The contour function finds the top-left and bottom-right corners of all white pixels and draws a green rectangle around the human.
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")| Filter | Kernel | Behaviour |
|---|---|---|
| Mean (5×5) | All weights = 1/25 | Uniform averaging — stronger smoothing |
| Gaussian (3×3) | Centre weighted (4/16) | Edge-preserving smoothing |
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)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
pip install opencv-python numpy vidgearpython EN19361932.pyPlace your input video as
n22.mp4in the same directory. Output video will be saved asOut_1.mp4.
- Press Q to stop processing early
- All pipeline stages are displayed in separate windows in real time
| Library | Purpose |
|---|---|
opencv-python (cv2) |
Video capture, colour conversion, display |
numpy |
Array operations and kernel matrices |
vidgear |
Compressed H.264 video output writing |
- 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
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







