Repository navigation
Point tracker: refine in a 3x3 neighbourhood when the motion was small - #2351
Conversation
The normalized cross-correlation only had a fast path for an exactly identity shape, which tracked footage never has after the first frame, so every candidate went through Plane::sample. All candidates of a search are the reference centre plus integer offsets, so each template pixel has the same fractional offsets for all of them. Build one table of taps per (level, seed, shape) and score each candidate with integer-indexed lerps, with one bounding-box check up front (outside the plane the old path is used). lk_refine only reads the level-0 gradients around the match, so compute them for that window (with an interior fast path in Plane::gradients_in) and fall back to the full gradients if the iteration leaves it. The results are unchanged: same lerp form and accumulation order. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The fine stage searched a 5x5 neighbourhood per pyramid level and seed. When the best coarse match is reliable (NCC score >= 0.8) and lies within one top-level pixel of the motion prediction, a 3x3 neighbourhood is enough; otherwise the 5x5 search stays. Fixes storytold#2349 Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
|
No actionable comments were generated in the recent review.
|
| Layer / File(s) | Summary |
|---|---|
Candidate scoring and fine search crates/track/src/lib.rs, crates/track/src/tests.rs |
A shared scorer handles shaped-template NCC scoring and is used for coarse and fine candidates. Fine search uses a 3×3 neighborhood when the best coarse score is at least 0.8 and the candidate is within 1.0 pixel per axis of the prediction; otherwise it uses 5×5. Tests compare scorer results with NCC and check tracking through alternating position jumps. |
Windowed gradient refinement crates/track/src/plane.rs, crates/track/src/lib.rs, crates/track/src/tests.rs |
Plane::gradients_in computes gradients inside an inclusive, clamped rectangle. Lucas–Kanade refinement uses local gradients and falls back to full-plane gradients when the refinement window leaves the cached region. Tests compare windowed and full-gradient results. |
Priority:
Estimated code review effort: 3 (Moderate) | ~25 minutes
Change: Other
Sequence Diagram(s)
sequenceDiagram
participant PointState
participant Scorer
participant Grad
participant Plane
PointState->>Scorer: Score coarse candidates
Scorer-->>PointState: Return candidate scores
PointState->>Scorer: Score candidates in selected fine-search neighborhood
Scorer-->>PointState: Return fine-search scores
PointState->>Grad: Build local gradients for refinement
Grad->>Plane: Compute gradients in the requested rectangle
Plane-->>Grad: Return gradient planes
Grad-->>PointState: Provide gradients to Lucas–Kanade refinement
Possibly related issues
- Issue 2318: This PR includes shared tap scoring and gradients limited to the match window, which the issue describes.
- Issue 2349: This PR implements conditional 3×3 fine search for reliable coarse matches near the motion prediction.
Possibly related PRs
- storytold/effectcraft#2339: This PR continues the shared scorer and windowed-gradient changes by adding conditional fine-search sizing.
Suggested reviewers: echelon
Merge Risk: 
Minimal · up to 4b31e
-
No concrete issue remains that should prevent merging after normal checks.
Security architecture risk: 
Minimal · up to 4b31e
-
The reviewed changes optimize image tracking without introducing a new security boundary or privileged operation. The existing full-gradient behavior remains available, and refinement falls back to it before reading outside the computed window. No introduced or worsened material security risk was identified.
Retained concerns
No architecture-level concerns identified.Security review details
-
Security Blast Radius
- inferred — For the inspected paths, caller-supplied image and tracking inputs affect computation, allocations, and returned tracking values within the existing process. No new tenant, service, data-store, or privileged-resource exposure is established. External users of the public library API remain outside verified coverage.
Trust Boundaries and Controls
- observed — The optimized scorer rejects unsupported tap coordinates or candidates whose taps leave the plane and then uses the existing NCC sampling path. The new windowed-gradient entrypoint does not grant additional authority; its result is derived only from the supplied Plane.
Resilience and Maintainability Implications
- observed — Gradient state is local to a tracking step and passed by exclusive mutable borrow, preventing reuse across points or frames. Solver failures remain local; Stop returns before persistent mutation, Extrapolate intentionally advances the center, and successful tracking follows the existing state-update path. No new transactional interruption guarantee is introduced.
-
Pre-merge checks: 
4 · 
1
-
Failed checks: 1 warning Passed checks: 4 passed-
Full details: Linked Issues check
-
Explanation
Issue
#2349requirements are implemented. The fine stage sets one search radius per step. It selects ±1 only when the best coarse NCC score is at least 0.8 and the first coarse seed is within 1.0 top-level pixel per axis of the prediction. It retains ±2 otherwise and applies the radius to all seeds. The added jump-recovery test covers the fallback case. The reported suite, benchmark, and synthetic accuracy results satisfy the stated verification goals; lost-point counts and reported error percentiles do not worsen.
Full details: Out of Scope Changes check
-
Explanation
The final commit changes the fine-stage decision and adds related tests. The shared scorer, windowed gradients, and
Plane::gradients_inchanges are from the disclosed stacked#2339commit in repository history. They provide the performance and validation support for the tracker change and are not unrelated work in this PR context.
-
-
Finishing Touches
-
🧪 Generate unit tests (beta)
-
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Comment @coderabbitai help to get the list of available commands.
|
This PR is one of 17 from a speed audit of
Notes:
|
|
Merged, thank you @vangel76! It landed on |
Summary
The fine stage of the point tracker refined each of up to three seeds at every pyramid level in a 5x5 neighbourhood. When the best coarse match is reliable (NCC score >= 0.8) and lies within one top-level pixel of the motion prediction, it now searches a 3x3 neighbourhood; in every other case the 5x5 search stays. The decision is made once per step for all seeds.
Fixes #2349
This PR is stacked on #2339 (same function). Until #2339 merges, the diff against
mainalso shows its commit; the change of this PR is the last commit only (lib.rs:SMALL_MOTION,RELIABLE_SCORE, 4 lines in the fine stage;tests.rs: one test).Numbers
Single thread (
RAYON_NUM_THREADS=1), 1080p, 16 points x 60 frames,perf stat -e instructions:u, work = total minus frame generation (188.25 G), three alternating runs per variant (counts agree to 6 digits):Why not simply +-1 everywhere
A plain +-1 gave 1.55x but lost tracks about twice as often when the camera jumped between frames (more than 8 px off: 31 -> 62, 93 -> 126 of 3776 measurements). The two conditions above keep +-2 exactly in those cases.
Accuracy
Synthetic ground truth, 64 points x 60 frames, 1080p; camera jitter 1x and 3x, noise 1x and 8x, translation and rotation+zoom. Number of measurements (of 3776) more than 0.5 / 2 / 8 px off:
Median and 90th-percentile errors are identical in all cases. This is synthetic material, not footage; the output is not bit-identical by design (it can differ where 3x3 finds a different minimum), but no case got worse.
Verification
cargo fmt --all --check,cargo clippy -p effectcraft-track --lib --tests -- -D warnings,cargo test --release -p effectcraft-track(62 passed, 3 ignored).recovers_after_sudden_jumps_the_prediction_misses: alternating jumps that defeat the velocity prediction are still tracked to within 0.3 px. It also passes on Point tracker: candidates share one tap table, gradients only where lk_refine reads them (#2318) #2339 alone.🤖 Generated with Claude Code
Summary by CodeRabbit