Skip to content

Fix crash when a marker is detected on the first frame (#37)#38

Merged
kalwalt merged 1 commit into
webarkit:devfrom
kalwalt:fix/frame0-empty-features-crash
May 30, 2026
Merged

Fix crash when a marker is detected on the first frame (#37)#38
kalwalt merged 1 commit into
webarkit:devfrom
kalwalt:fix/frame0-empty-features-crash

Conversation

@kalwalt

@kalwalt kalwalt commented May 30, 2026

Copy link
Copy Markdown
Member

Summary

Fixes #37. processFrame() could call GetTrackedFeaturesWarped() with an empty tracked-point selection on the very first frame, making cv::perspectiveTransform assert and throw.

Root cause: the optical-flow branch that calls GetInitialFeatures() (which populates _selectedPts) is gated on _frameCount > 0, but MatchFeatures() can set _isDetected = true on frame 0. The pose block (if (_isDetected || _isTracking)) then calls GetTrackedFeaturesWarped() on the empty selection → perspectiveTransform hits CV_Assert(scn + 1 == m.cols) (empty input → 1-channel Mat → 2 == 3).

A live camera masks this (detection essentially never succeeds on the literal first frame); a static image triggers it deterministically.

Changes

  • TrackingPointSelector::GetTrackedFeaturesWarped() — return an empty result when the selection or the homography is empty, instead of calling perspectiveTransform on empty input (fixes the crash at the source).
  • WebARKitTracker::processFrame() — only run pose estimation when there are >= 4 correspondences and the image/object point counts match (solvePnP needs ≥4; avoids estimating from an empty/degenerate set).

Both are defensive and do not change behavior on frames where the selection is properly populated.

Verification

Built to WASM and run in the webarkit-testing static Teblid example with its warmup frame removed, so the real image is processed on frame 0 and exercises the exact crash path:

  • Before: cv::error / perspectiveTransform exception (stack: GetTrackedFeaturesWarpedprocessFrame).
  • After: no exception; the tracker reports not-found on frame 0 and proceeds normally.

Existing examples (with their normal frame flow) are unaffected.

Notes

The webarkit-testing static example currently feeds a blank warmup frame as a workaround; with this fix the library is robust against frame-0 detection for any consumer, so the warmup is no longer required for crash-avoidance (it's retained there only to smooth the static-image detection→tracking handoff).

Refs: webarkit/webarkit-testing#30, #31

🤖 Generated with Claude Code

processFrame() could call GetTrackedFeaturesWarped() with an empty tracked-
point selection on the very first frame: the optical-flow branch that calls
GetInitialFeatures() (which populates _selectedPts) is gated on
_frameCount > 0, yet MatchFeatures() can set _isDetected = true on frame 0.
cv::perspectiveTransform then asserts on the empty input (scn+1 == m.cols,
i.e. 2 == 3) and throws.

Two guards:
- TrackingPointSelector::GetTrackedFeaturesWarped() returns an empty result
  when the selection is empty or the homography is empty, instead of calling
  perspectiveTransform on empty input.
- processFrame()'s pose block only runs solvePnP when there are >= 4
  correspondences and the image/object point counts match.

A live camera masks this (detection essentially never succeeds on the
literal first frame); a static image triggers it deterministically. The
webarkit-testing static example previously worked around it with a blank
warmup frame; this fixes it at the source.

Refs: webarkit#37, webarkit/webarkit-testing#30

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@kalwalt kalwalt self-assigned this May 30, 2026
@kalwalt kalwalt added bug Something isn't working enhancement New feature or request C/C++ code concerning the C/C++ code design and improvements Emscripten labels May 30, 2026
@kalwalt kalwalt moved this from In progress to Review in progress in New markerless image tracking May 30, 2026
@kalwalt kalwalt merged commit 32a6343 into webarkit:dev May 30, 2026
@github-project-automation github-project-automation Bot moved this from Review in progress to Done in New markerless image tracking May 30, 2026
@kalwalt kalwalt deleted the fix/frame0-empty-features-crash branch May 30, 2026 19:36
kalwalt added a commit to webarkit/webarkit-testing that referenced this pull request May 30, 2026
Bumps the WebARKitLib submodule to the canonical dev SHA after
webarkit/WebARKitLib#38 (squash) merged the first-frame perspectiveTransform
crash fix, and commits the rebuilt dist/WebARKit.js and build/ WASM artifacts.

The static Teblid example no longer relies on the warmup frame to avoid the
frame-0 crash (the library now guards it); the warmup is retained only to
smooth the static-image detection->tracking handoff.

Refs: webarkit/WebARKitLib#37, #30

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
kalwalt added a commit to webarkit/webarkit-testing that referenced this pull request Jun 21, 2026
Bumps the WebARKitLib submodule to the canonical dev SHA after
webarkit/WebARKitLib#38 (squash) merged the first-frame perspectiveTransform
crash fix, and commits the rebuilt dist/WebARKit.js and build/ WASM artifacts.

The static Teblid example no longer relies on the warmup frame to avoid the
frame-0 crash (the library now guards it); the warmup is retained only to
smooth the static-image detection->tracking handoff.

Refs: webarkit/WebARKitLib#37, #30

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working C/C++ code concerning the C/C++ code design and improvements Emscripten enhancement New feature or request

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant