Skip to content
LiamDGrayPublic

About

Track position of a point in webcam, in realtime, based on OpenCV's Lukas-Kanade demo code. Originally designed to enable a torsion pendulum experiment. Original build is Windows console app using Visual Studio.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

TrackXY Modernized

Track position of a point in webcam or video file, in realtime, using Lucas-Kanade optical flow. Modernized version of the original TrackXY project (2012).

Features

  • Modern C++ Architecture: Uses C++17 and OpenCV 4.x API.
  • Cross-Platform Build System: CMake-based build.
  • ROI Selection: Interactive region-of-interest selection for tracking initialization.
  • Video File Support: Process video files in addition to live webcam feeds.
  • Live Data Visualization: Real-time overlay of tracking status, coordinates, and analysis.
  • Data Logging: Robust CSV logging of raw and calibrated coordinates.
  • Unit Calibration: Mapping of pixel coordinates to real-world units.
  • Dual-Axis Oscillation Analysis: Real-time frequency, amplitude, and phase difference estimation for XY oscillations.
  • Measurement Receipts: Bind a result CSV to its video, calibration, and parameters so the numbers can be checked later.

Documentation

Document What it covers
docs/tutorials/first-measurement.md Ten-minute tour: analyse a synthetic recording, twice, and see what a calibration model changes
docs/how-to/calibrate-a-tilted-camera.md Procedure for a camera that cannot be square-on, with troubleshooting
docs/how-to/measure-rotation-from-a-side-view.md Chord-vs-arc correction for a marker on a circumference, and when it stops working
docs/how-to/bound-the-error-in-a-slow-torsion-balance.md Uncertainty budget: every error source with a magnitude, and what a defensible claim looks like
docs/how-to/instrument-a-torsion-balance.md The torsion-balance protocol: centre shift, spectrum, relaxation constant, and their limits
docs/explanation/the-number-is-not-the-evidence.md Why the numbers can be fluent and wrong: three defects, measured
docs/technical-report.md Method, assumptions, verification status, limitations, roadmap
docs/architecture.md Components, contracts, frame lifecycle, output schema, test strategy
docs/measurement-provenance.md Making a result checkable; composition with RealPeep capture attestation
docs/design/next-iteration.md Measured case for the next version: estimator, homography calibration, axis marker, platform

Start with docs/README.md if you are not sure which one you want.

Build Instructions

Prerequisites

  • C++17 compatible compiler (GCC, Clang, MSVC)
  • CMake (>= 3.14)
  • OpenCV (>= 4.0)
  • Catch2 (automatically downloaded for tests)

Compilation

mkdir build
cd build
cmake ..
make

Running Tests

cd build
ctest

The portable numerical core in js/ (estimators, calibration geometry, marker angle) has its own Node suite:

cd js && npm test && npm run bench

The receipt tool has its own Python suite, which needs no OpenCV:

python3 -m unittest tests.test_measurement_receipt

The C++ suite uses Catch2 (fetched automatically at configure time) and runs eleven cases across eight files: tracker lifecycle and identity, calibration round-trip, CSV formatting, CLI parsing, input routing, both analysers, and the visualiser. Nothing in it needs a camera or a display.

Usage

./trackxy [options]

Command Line Options

  • -i, --input: Input source (camera index e.g., 0, or path to a video file). Default: 0.
  • -o, --output: Output CSV file for data logging. Default: output.csv.
  • -c, --calibration: Calibration file to load/save. Default: calibration.yml.
  • -n, --night: Enable night mode (black background for better contrast).
  • -h, --help: Show help message.

Keyboard Controls

  • Esc: Quit the program.
  • r: Select a Region of Interest (ROI) and re-initialize tracking.
    • Click and drag to draw a box around the feature you want to track, then press Enter.
  • c: Clear current tracking points.
  • n: Toggle "night mode".
  • s: Save current calibration (origin and scale) to the calibration file.
  • o: Set the coordinate system origin (0,0) at the current primary tracking point.
  • k: Set the scale. You will be prompted in the console to enter the real-world distance that corresponds to 100 pixels in the image.

Calibration Guide

  1. Find a reference: Place an object of known length in the camera view at the same depth as the tracked object.
  2. Measure pixels: (Optional) If you know your camera's pixels-per-mm, you can use that. Otherwise:
  3. Use 'k': Press 'k' and enter the distance (e.g., 50.0 for 50mm) that corresponds to 100 pixels.
  4. Set Origin: Move the tracking point to your desired (0,0) location and press 'o'.
  5. Save: Press 's' to save these settings to calibration.yml. They will be loaded automatically next time you run TrackXY with the same calibration file flag.

Output Format

The program logs data to a CSV file with the following columns:

  • samplenumber: Sequence number of the sample.
  • milliseconds: Time elapsed since tracking started.
  • x_px, y_px: Raw pixel coordinates.
  • x_cal, y_cal: Calibrated coordinates in your chosen units.
  • status: Tracking status information.

Note on the legacy schema: the 2012 build wrote samplenumber, millseconds, x, y, numclicks, comments or notes (note the misspelling). The current schema is the one above, written by DataLogger. docs/samples/legacy-format-example.csv shows the old layout; it is synthetic, so nobody mistakes it for a measurement.

Analysing a recording without a camera

js/ contains a dependency-free implementation of the analysis: estimator, calibration geometry and marker angle, plus a CLI.

cd js
node src/cli.js synthesize --out /tmp/run.csv --tilt 25 --radius 50   # known-answer input
node src/cli.js analyze --csv /tmp/run.csv --calib /tmp/run.calibration-homography.json

# A marker on a circumference: give the radius to get an angle, not a chord
node src/cli.js analyze --csv /tmp/run.csv --calib camera.json --radius 175

# Long balance recordings: centre-of-oscillation shift, spectrum, relaxation
node src/cli.js balance --csv run.csv --window 120
  X  f = 0.8000 Hz ± 0.0000   amplitude = 49.993 mm ± 0.017
  Y  f = 0.8000 Hz ± 0.0000   amplitude = 49.993 mm ± 0.018
  phase (Y − X)  -90.00° ± 0.03°

npm test runs 36 tests; npm run bench reproduces the accuracy tables in the docs.

Measurement receipts

A CSV is a claim, not evidence — anyone can edit a row. Bind it to its inputs before sharing:

tools/measurement_receipt.py emit \
  --csv output.csv --calibration calibration.yml --video clip.mp4 \
  --out measurement.receipt.json

tools/measurement_receipt.py verify --receipt measurement.receipt.json

Verification recomputes every digest, the row chain, and the manifest root, so a modified row, an appended sample, or a changed calibration is reported rather than accepted. Pass --capture-root <hex> with a RealPeep session root to bind the numbers to an attested capture. See docs/measurement-provenance.md.

Analysis Capabilities

TrackXY includes real-time oscillation analysis for both X and Y axes:

  • Frequency: Estimated in Hz using zero-crossing detection.
  • Amplitude: Half of the peak-to-peak distance in the current analysis window.
  • Phase Difference: The phase shift between Y and X oscillations (in degrees). Useful for analyzing circular or elliptical motion.

Development harness (agentic)

The 2026 modernization was carried out by a coding agent running in a loop, and the harness is part of the repository:

File Role
GEMINI.md Project constitution: TDD is mandatory, one feature branch per task, C++17/20, modern OpenCV API, CMake only
TASKS.md Durable state. The agent reads it every turn, ticks the first unchecked item, and writes a note
wiggum.sh Loop driver. Feeds TASKS.md to the agent and handles quota exhaustion across three auth tiers
config.yaml LiteLLM router used by the paid fallback tier: ordered pro/flash model chains with per-deployment rate limits
.gemini/settings.json, .gemini/hooks/after_agent.sh AfterAgent hook — the seam where a verifier belongs

Auth tiers in wiggum.sh, best first: cached Gemini CLI OAuth, then a Google AI Studio API key, then DeepSeek through the local LiteLLM proxy. On a quota error the driver parses the provider's retryDelayMs, parks that tier, advances, and restores the free tiers when their windows pass. Create a .wiggum_stop file to exit cleanly between iterations.

Provider keys are supplied via a sops-encrypted file kept outside this repository (~/src/secrets/secrets.enc.env) and inherited through the environment — never as command-line arguments, and never committed.

License

Original lkdemo.cpp (c) 2000-2006 Intel Corporation (BSD). Enhancements (c) 2012, 2026 Liam D. Gray. Released under GNU Affero GPL v3.

About

Track position of a point in webcam, in realtime, based on OpenCV's Lukas-Kanade demo code. Originally designed to enable a torsion pendulum experiment. Original build is Windows console app using Visual Studio.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages