Two computer vision pipelines built to detect, track, and analyze real-world objects using raw image processing techniques.
Explore the Documentation »
Report Bug
·
Request Feature
Table of Contents
TVCOutputFinal.mp4
Final output of the Traffic Flow system: Vehicles are tracked continuously within a specific region of interest, and their real-time speeds (km/h) are calculated across three highway lanes.
LCoutputstagesprocess.mp4
Video demonstration of the piece extraction and geometric classification stages.
This repository contains Traffic Flow & Lego Sorting Vision Systems, an academic project developed from scratch in collaboration with Martim Ramos for the Image Processing and Vision course at ISEL (Instituto Superior de Engenharia de Lisboa) in 2025.
The primary goal of this project is to apply core computer vision algorithms without relying on deep learning models. It solves two distinct problems: counting and tracking vehicles on a highway, and classifying Lego pieces based on their geometric properties.
Note: The detailed source documentation and step-by-step logic are written in Portuguese inside the Jupyter Notebooks, which are also exported as PDF files.
- Background Subtraction: Dynamic background estimation using temporal median filtering to isolate moving vehicles.
- Smart Tracking (TTL): Advanced object tracking utilizing Euclidean distance matching and "Time to Live" memory for bounding boxes that flicker near borders.
- Perspective Calibration: Homography matrix transformation converting standard 2D camera views into precise top-down layouts to measure real-world speeds.
- Color Pre-processing Masking: Resolving contrast issues via selective color replacement (masking green legos to red) before attempting Otsu binarization.
- Advanced Contour Geometry: Creating custom classification logic using polygon approximation, bounding box constraints, and centroid moments to accurately type Lego bricks.
TVCOutputStages.mp4
A full run-through of the pipeline, displaying each internal stage of processing side-by-side.
The Processing Pipeline This system processes a live highway video feed to accurately monitor traffic. The pipeline acts sequentially as follows:
-
Background Subtraction: A static version of the road is estimated by analyzing the median values of pixels over time. We then calculate the absolute difference between each frame and this static background to detect active movement.
-
Morphological Noise Cleaning: The raw active pixels contain significant noise (from shadows, shaking trees). We apply morphological Open to remove this small noise, followed by a Close operator to fill in the gaps inside the vehicle bodies, generating solid blocks of white pixels for the cars.
-
Speed & Perspective Warping: To calculate real-world speed (km/h), tracking pixel distances directly is inaccurate due to perspective distortion (cars far away look smaller and move fewer pixels). A Homography matrix converts the Region of Interest (ROI) into a top-down view. Using real road measurements (e.g., lane widths of 3.75m), we can translate pixel velocity into metric speed.
Final output of the Lego Sorting system: Scattered Lego bricks are successfully extracted from the background, their shapes classified by dimensions, and colored contours are drawn based on piece type.
The Processing Pipeline This script processes images of scattered Lego bricks on a blue table, identifies them, and draws accurate boundaries and text labels representing their size.
- Color Extraction & Masking: Through histogram analysis, the Red channel proved best for isolation. However, some green bricks blended into the background. To resolve this, green bricks were selectively masked and recolored to red prior to Otsu thresholding.
- Component Morphing: Similar to the traffic pipeline, morphological kernels were used to erode false connections between pieces, and then dilate them back to restore their true shapes.
- Geometric Typing: Using OpenCV's
approxPolyDPandminAreaRect, each connected component was reduced to a polygon, checked for rectilinearity, and its area was evaluated to determine if it belonged to the 2x2, 2x4, 2x6, or 2x8 categories.
- Adaptive Thresholding: Standard binarization struggles heavily with shadows; calculating thresholds dynamically based on background standard deviation greatly improved vehicle extraction.
- Color Channel Manipulation: Exploring histograms revealed that analyzing individual RGB channels (specifically the Red channel) is often vastly superior to grayscale for piece isolation.
- Temporal Tracking Jitter: Calculating speed based on frame-by-frame distance produces huge noise due to bounding-box jitter. Averaging distance over a gap of 5 frames completely resolved the velocity fluctuations.
- Component Masking: Attempting to force one binarization threshold on all colors is flawed. Pre-processing the image by selectively re-coloring problematic pieces (green pieces to red) created a robust pipeline for Otsu thresholding.
Follow these instructions to set up a local copy of the project on your machine.
- Python 3.9+
- Jupyter Notebook
- Clone the repository:
git clone https://github.com/GuilhermeGraca/python-computer-vision.git
- Navigate to the directory:
cd python-computer-vision - Install the required libraries:
pip install -r requirements.txt
- Run the notebooks:
Open either
jupyter notebook
traffic-vehicle-counting.ipynboulego-piece-classification.ipynband run all cells sequentially.
Guilherme Graça - LinkedIn - GitHub
- Martim Ramos - For the collaboration and teamwork in developing this academic project.
- ISEL (Instituto Superior de Engenharia de Lisboa) - For the academic foundation provided during the Image Processing and Vision course.



