This project implements and visualizes optical flow using the Lucas-Kanade method. Optical flow is a computer vision technique that estimates motion patterns in image sequences, providing valuable information about how objects move within a scene.
- Real-time optical flow visualization from webcam input
- Color-coded flow vectors where color represents direction and intensity represents velocity magnitude
- Customizable parameters for smoothing, stride, and image downsampling
- Support for both image files and webcam input
The implementation follows these key steps:
- Derivative Calculation: Computes spatial and temporal image derivatives
- Structure Matrix Construction: Builds time-structure matrices from derivatives
- Velocity Estimation: Solves the optical flow equation for motion vectors
- Visualization: Renders motion vectors with direction-based color mapping
- Python 3.x
- OpenCV (
cv2) - NumPy
# Clone this repository
git clone https://github.com/arda92a/Optical-Flow.git
cd optical-flow
# Install required packages
pip install opencv-python numpyRun the webcam demo to see optical flow in real-time:
python optical_flow.py --webcam --smooth 3 --stride 1 --div 1Parameters:
--smooth(default: 3): Window size for smoothing the structure matrix--stride(default: 1): Downsampling factor for velocity calculation--div(default: 1): Downsampling factor for input images
Process a pair of images to calculate optical flow:
python optical_flow.py --image1 first_image.jpg --image2 second_image.jpg --output flow_result.jpgThe optical flow algorithm processes sequential frames to detect motion. Below are sample results:
First input frame (starting point)
Second input frame (ending point)
Resulting optical flow visualization showing motion vectors
Below is a comprehensive visualization of different movement patterns and their corresponding optical flow representations:
Various movement patterns and their optical flow signatures
This visualization demonstrates how different types of motion (translation, rotation, scaling, etc.) appear in the optical flow field, providing a reference for interpreting flow patterns in real-world applications.
- Box filtering with integral images for efficient smoothing
- Direction-colored visualization using HSV color mapping
- Aperture problem handling for areas with insufficient texture
- Velocity constraint to avoid extreme flow vectors
You can adjust several parameters to balance accuracy and performance:
# Higher smoothing for noisy images
python optical_flow.py --webcam --smooth 5
# Increase stride for faster processing
python optical_flow.py --webcam --stride 2
# Decrease resolution for better performance
python optical_flow.py --webcam --div 2- Slow performance: Increase the
strideanddivparameters - Noisy flow vectors: Increase the
smoothparameter - Webcam not detected: Check your camera connection and permissions
This implementation is based on the Lucas-Kanade method for optical flow estimation.