Skip to content

feat(video): implement buildOpticalFlowPyramid and calcOpticalFlowPyrLK#47

Merged
kalwalt merged 11 commits intodevfrom
copilot/implement-optical-flow-functions
Apr 30, 2026
Merged

feat(video): implement buildOpticalFlowPyramid and calcOpticalFlowPyrLK#47
kalwalt merged 11 commits intodevfrom
copilot/implement-optical-flow-functions

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 27, 2026

Description

This PR resolves Issue #46, implementing the core Lucas-Kanade Pyramidal Optical Flow functionality in a new purecv-video module. It carefully mirrors the OpenCV API and implementation, providing zero-dependency, safe Rust equivalents for video feature tracking.

Closes #46.

Core Features Implemented

  • build_optical_flow_pyramid: Constructs the image pyramid with an option to also build gradient pyramids (with_derivatives).
  • calc_optical_flow_pyramid_lk: Iterative Lucas-Kanade feature tracking with support for initial flow estimates (OPTFLOW_USE_INITIAL_FLOW), minimum eigenvalue thresholds, and configurable search window sizes.
  • Note: TermCriteria was already implemented previously in src/core/types.rs, but is heavily utilized here for configuring the iterative tracking loop termination conditions.

Examples & Documentation

  • examples/optical_flow.rs: A complete, runnable example using butterfly.jpg. It synthesizes a subsequent video frame via rigid translation, detects features via good_features_to_track, tracks them with calc_optical_flow_pyramid_lk, and outputs a visual representation (optical_flow_result.png) alongside the raw coordinate mapping (optical_flow_vectors.csv).
  • Updated README.md to introduce the new purecv-video module and shifted the roadmap strategy to track GitHub Milestones (specifically starting with Milestone 3).

Benchmarks & Performance

  • Added benches/video_bench.rs to validate the performance of pyramid generation and optical flow tracking.
  • Updated benches/benchmark_results.md to record the baseline performance on standard test resolutions.

Code Quality & Interoperability

  • Fixed clippy warnings (option_map_unit_fn, implicit_saturating_sub).
  • Eliminated unnecessary as *mut u8 raw pointer casts in src/core/dynamic.rs (data_ptr, data_ptr_mut), fully supporting our WASM/SIMD zero-copy pipeline design.

Checks

  • Full doc comments on all helper functions (save_flow_image, draw_line, save_flow_csv)
  • cargo fmt clean
  • cargo clippy clean (0 warnings)
  • cargo test — all tests pass
  • CodeQL — 0 alerts

Copilot AI changed the title [WIP] Implement buildOpticalFlowPyramid and calcOpticalFlowPyramidLK in video module feat(video): implement buildOpticalFlowPyramid and calcOpticalFlowPyrLK Apr 27, 2026
Copilot AI requested a review from kalwalt April 27, 2026 18:17
@kalwalt kalwalt added rust-code rust Pull requests that update rust code tests video-module labels Apr 27, 2026
Add optical_flow_video.rs example demonstrating continuous feature
tracking across a video sequence using pyramidal Lucas-Kanade.

- Supports animated GIF as a lightweight video input format (decoded
  via the `image` crate, no new dependencies)
- Falls back to synthetic frames from a static image when no GIF
  is provided
- Auto-generates a test GIF (butterfly_motion.gif) on first run
- Tracks features across all frames with automatic re-detection
  when too few points remain
- Outputs annotated frame PNGs with coloured flow trails and a
  CSV summary of per-frame tracking statistics
@kalwalt kalwalt marked this pull request as ready for review April 28, 2026 07:36
kalwalt added 4 commits April 28, 2026 10:21
- Expose calcOpticalFlowPyrLK and buildOpticalFlowPyramid to WASM.
- Add OPTFLOW_USE_INITIAL_FLOW and OPTFLOW_LK_GET_MIN_EIGENVALS flag constants.
- Implement real-time webcam optical flow demo with trail visualization.
- Add optical flow example to the WASM demo index page.
Integrates changes from PR #44 and PR #45 into the WASM layer.
- Added video_bench.rs to measure calc_optical_flow_pyramid_lk performance

- Updated Cargo.toml with the new benchmark target

- Updated README.md to document new features and purecv-video module

- Added Video performance comparison to benches/benchmark_results.md

- References PR #44 and #45 features
@kalwalt kalwalt force-pushed the copilot/implement-optical-flow-functions branch from 0fc8a55 to 173f71b Compare April 28, 2026 14:06
Copy link
Copy Markdown
Member

@kalwalt kalwalt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, ready!

@kalwalt kalwalt merged commit 1b2a545 into dev Apr 30, 2026
3 checks passed
@kalwalt kalwalt deleted the copilot/implement-optical-flow-functions branch May 1, 2026 20:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

rust Pull requests that update rust code rust-code tests video-module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement buildOpticalFlowPyramid & calcOpticalFlowPyramidLK from OpenCV in video module

2 participants