MayaFlux 0.5.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
OpticalFlow.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "Features.hpp"
4
5/**
6 * @file OpticalFlow.hpp
7 * @brief Sparse optical flow via Lucas-Kanade tracker.
8 *
9 * Pure functions. No MayaFlux type dependencies.
10 *
11 * ## Conventions
12 * - All inputs are single-channel normalised float in [0, 1]
13 * - w and h are pixel dimensions; caller ensures span size == w * h
14 * - Keypoint positions are normalised to [0, 1]
15 * - window_radius is the half-size of the tracking window in pixels
16 * - Points that fail to track are returned at their previous position
17 * with tracked = false
18 * - Parallelism handled internally via Parallel::par_unseq
19 */
20
22
23/**
24 * @brief Result for a single tracked point.
25 */
27 glm::vec2 position; ///< Tracked position in normalised [0, 1] coordinates.
28 float error; ///< Residual photometric error after convergence.
29 bool tracked; ///< False if tracking failed or diverged.
30};
31
32/**
33 * @brief Track keypoints from prev_gray to curr_gray via Lucas-Kanade.
34 *
35 * For each input point, solves the Lucas-Kanade optical flow equation
36 * within a window_radius x window_radius patch using iterative
37 * Newton-Raphson refinement. Iteration stops when the displacement
38 * update falls below 0.03 pixels or max_iterations is reached.
39 *
40 * A point is marked tracked=false if:
41 * - The structure tensor within its window is near-singular
42 * (min eigenvalue below eigen_threshold)
43 * - The tracked position moves outside the image boundary
44 * - The residual error after convergence exceeds error_threshold
45 *
46 * prev_points and the returned vector have the same length and order.
47 * Callers should discard points where tracked == false before passing
48 * to subsequent frames.
49 *
50 * @param prev_gray Previous frame, single-channel float, size w * h.
51 * @param curr_gray Current frame, single-channel float, size w * h.
52 * @param w Image width in pixels.
53 * @param h Image height in pixels.
54 * @param prev_points Points to track in normalised [0, 1] coordinates.
55 * @param window_radius Half-size of the tracking patch in pixels.
56 * @param max_iterations Maximum Newton-Raphson iterations per point.
57 * @param eigen_threshold Minimum eigenvalue for structure tensor validity.
58 * @param error_threshold Maximum residual error to accept a track.
59 * @return TrackResult per input point, same order.
60 */
61[[nodiscard]] MAYAFLUX_API std::vector<TrackResult> track_keypoints(
62 std::span<const float> prev_gray,
63 std::span<const float> curr_gray,
64 uint32_t w, uint32_t h,
65 std::span<const glm::vec2> prev_points,
66 uint32_t window_radius = 7,
67 uint32_t max_iterations = 20,
68 float eigen_threshold = 1e-4F,
69 float error_threshold = 0.3F);
70
71/**
72 * @brief Track keypoints using a pre-computed Sobel gradient of prev_gray.
73 *
74 * Identical to the four-parameter overload but accepts caller-supplied dx
75 * and dy of the previous frame, avoiding recomputation when the gradient
76 * is already cached. dx and dy must be the Sobel gradient of prev_gray
77 * at the same w x h resolution.
78 *
79 * @param prev_gray Previous frame, single-channel float, size w * h.
80 * @param curr_gray Current frame, single-channel float, size w * h.
81 * @param grad_dx Horizontal Sobel gradient of prev_gray, size w * h.
82 * @param grad_dy Vertical Sobel gradient of prev_gray, size w * h.
83 * @param w Image width in pixels.
84 * @param h Image height in pixels.
85 * @param prev_points Points to track in normalised [0, 1] coordinates.
86 * @param window_radius Half-size of the tracking patch in pixels.
87 * @param max_iterations Maximum Newton-Raphson iterations per point.
88 * @param eigen_threshold Minimum eigenvalue for structure tensor validity.
89 * @param error_threshold Maximum residual error to accept a track.
90 * @return TrackResult per input point, same order.
91 */
92[[nodiscard]] MAYAFLUX_API std::vector<TrackResult> track_keypoints(
93 std::span<const float> prev_gray,
94 std::span<const float> curr_gray,
95 std::span<const float> grad_dx,
96 std::span<const float> grad_dy,
97 uint32_t w, uint32_t h,
98 std::span<const glm::vec2> prev_points,
99 uint32_t window_radius = 7,
100 uint32_t max_iterations = 20,
101 float eigen_threshold = 1e-4F,
102 float error_threshold = 0.3F);
103
104} // namespace MayaFlux::Kinesis::Vision
uint32_t h
Definition InkPress.cpp:28
std::vector< TrackResult > track_keypoints(std::span< const float > prev_gray, std::span< const float > curr_gray, uint32_t w, uint32_t h, std::span< const glm::vec2 > prev_points, uint32_t window_radius, uint32_t max_iterations, float eigen_threshold, float error_threshold)
Track keypoints from prev_gray to curr_gray via Lucas-Kanade.
float error
Residual photometric error after convergence.
bool tracked
False if tracking failed or diverged.
glm::vec2 position
Tracked position in normalised [0, 1] coordinates.
Result for a single tracked point.