Documentation Index
Fetch the complete documentation index at: https://mintlify.com/dfki-ric/uxo-dataset2024/llms.txt
Use this file to discover all available pages before exploring further.
Overview
Theoptical_flow module provides functions for calculating optical flow between consecutive frames in ARIS sonar video. It supports both dense flow (Farneback) and sparse flow (Lucas-Kanade) methods, outputting overall motion magnitude for motion onset detection and velocity analysis.
Module Path: scripts/common/optical_flow.py
Functions
calc_overall_flow
Flow field array. For dense flow, shape is
(height, width, 2). For sparse flow, shape is (n_points, 2) representing displacement vectors.Overall flow magnitude computed as the L2 norm of the mean displacement vector
Implementation
The function computes:- Mean displacement in x-direction:
dx = mean(flow[..., 0]) - Mean displacement in y-direction:
dy = mean(flow[..., 1]) - Overall magnitude:
sqrt(dx² + dy²)
calc_optical_flow_farnerback
Iterator yielding grayscale frames as numpy arrays. Each frame should have shape
(height, width) with dtype uint8.Parameters passed to
cv2.calcOpticalFlowFarneback(). Common parameters:1D array of flow magnitudes, one per frame transition. Length is
n_frames - 1.Method Details
The Farneback method computes dense optical flow by:- Approximating image neighborhoods with quadratic polynomials
- Computing displacement based on polynomial expansion
- Producing a flow vector for every pixel
- Captures complete motion field
- No feature detection required
- Good for smooth, continuous motion
- Computationally expensive
- Slower than sparse methods
- May struggle with noisy sonar imagery
Example
calc_optical_flow_lk
Iterator or iterable yielding grayscale frames as numpy arrays. Each frame should have shape
(height, width) with dtype uint8.Parameters passed to
cv2.calcOpticalFlowPyrLK(). Common parameters:Parameters passed to
cv2.goodFeaturesToTrack(). If None, defaults are used. Common parameters:1D array of flow magnitudes, one per frame. First element is always 0. Length is
n_frames.Method Details
The Lucas-Kanade method computes sparse optical flow by:- Detecting “good features to track” (corners) using Shi-Tomasi algorithm
- Tracking those features to the next frame
- Computing displacement for successfully tracked features
- Re-detecting features every 10 frames to maintain feature count
- Computationally efficient
- Robust to image noise
- Good for real-time applications
- Only tracks discrete points
- May fail if no good features detected
- Requires periodic feature re-detection
Behavior
- If no features can be detected in a frame, flow magnitude is 0 for that frame
- If all features fail to track, flow magnitude is 0 and features are re-detected
- Features are automatically re-detected every 10 frames to prevent drift
- The iterator is automatically converted if it’s not already an iterator
Example
Output Format
Both flow calculation functions return a 1D numpy array where each element represents the overall motion magnitude between consecutive frames.- Low values (< 1.0): Little to no motion, likely static scene
- Medium values (1.0 - 5.0): Moderate motion, gradual movement
- High values (> 5.0): Significant motion, rapid movement or motion onset
Motion Onset Detection
Optical flow is commonly used to detect when motion begins in a recording:Algorithm Comparison
| Feature | Farneback (Dense) | Lucas-Kanade (Sparse) |
|---|---|---|
| Computation | Expensive | Fast |
| Coverage | Every pixel | Selected points only |
| Noise Sensitivity | Moderate | Low |
| Best For | Smooth surfaces, detailed analysis | Real-time, feature-rich scenes |
| Output Size | Large (H×W×2 per frame) | Small (N points × 2) |
| ARIS Suitability | Good for high-quality frames | Better for noisy frames |
Performance Tips
For Farneback:- Reduce
levelsif frames are small or speed is critical - Increase
pyr_scale(closer to 1.0) for faster computation - Decrease
winsizefor noisy images
- Reduce
maxCornersin feature_params for faster processing - Increase
minDistanceto spread features more evenly - Adjust
qualityLevelif too few/many features are detected
Integration with MatchingContext
Related Modules
- Matching Context - Provides frame iterators for ARIS data
- ARIS Definitions - Frame metadata for contextual analysis