This script simulates a sliding bar animation and computes optical flow using Lucas–Kanade (LK) tracking, mimicking the behavior of the PixArt PAW3902 optical flow sensor. It outputs flow values in radians and visualizes them in real time.
- Generates a synthetic bar animation (red, green, blue columns) for testing flow detection.
- Implements optical flow calculation using OpenCV’s
calcOpticalFlowPyrLK
. - Scales pixel displacements into radians using PAW3902’s datasheet constants (11.914 CPI per meter, 39.3701 in/m).
- Provides an optional debug plotting window for
flow_x
andflow_y
over time.
SlidingBarAnimation
: Generates the RGB sliding bar test pattern.CalculateOpticalFlow
: Core class that- Computes optical flow,
- Converts raw displacements into radians,
- Plots flow values in real time using the nested
FlowPlotter
class for visualization.
The stimulus pattern (used to drive or “attack” the optical flow algorithm) is controlled by:
num_rgb_sets
→ Number of repeating RGB groups across the frame.switching_frequency
→ Frequency (Hz) at which the frames switch between red, green, and blue patterns.frame size
→ Defined inSlidingBarAnimation
aswidth=900
,height=900
.padding
→ White border around the bars (default: 100 pixels).
Inside CalculateOpticalFlow
, the following parameters are defined to emulate PAW3902 behavior:
SENSOR_RESOLUTION_PIXELS = 35
FPS = 126.0
(Bright mode)PIXART_RESOLUTION = 11.914
(counts per inch per meter)INCHES_PER_METER = 39.3701
SCALE = 1 / (PIXART_RESOLUTION * INCHES_PER_METER)
→ counts to radians
- Python 3.8+
- OpenCV (
pip install opencv-python
) - NumPy
- Matplotlib
Run the script directly:
python3 optical_flow_emulation.py