← All projects

FastFocus

OpenCV-only fast autofocus: coarse-to-fine bracketing plus parabolic interpolation, ~5-12 images instead of 30-100.

FastFocus

Overview

FastFocus is an AOI-style autofocus refinement tool that typically needs only ~5-12 images (or as few as 2-3 interactively) instead of the usual 30-100. It uses coarse-to-fine bracketing plus parabolic interpolation, with OpenCV + NumPy only (no HALCON).

Why it works

Focus curves are unimodal - a single peak. Bracketing quickly finds points that span the peak, then a parabola fit gives a sub-step estimate of the best Z with very few evaluations. The default tenengrad metric (Sobel gradient energy) is robust to illumination and works well on high-contrast dot grids.

The GUI has four tabs

  • Folder (stack) - run a strategy across a whole image stack and report the estimated Z*.
  • Fit from images - parse Z from filenames, toggle which images to include, and fit (2-point estimate or parabola for 3+).
  • Optimize - brute-force all metrics x ROIs against a known ideal Z, and analyze how many images are actually needed.
  • Interactive Focus - add images one at a time; the app suggests the next move and reports trend, confidence, and whether the peak is bracketed.

Quick start

pip install -r requirements.txt

# Synthetic sanity check (no real images)
python simulate_from_dataset.py --generate-synthetic data/synthetic_dots \
  --strategy bracket_parabola --metric tenengrad --roi center:0.4 --plot

# Real images, Z parsed from filenames like 46.147.jpg / 46,297.jpg
python simulate_from_dataset.py --folder data/dots --z-from-filename \
  --strategy bracket_parabola --metric tenengrad --roi center:0.4 --plot

Output

An estimated best Z\* with a confidence (0-1) from peak sharpness and fit residual, plus a recommended final approach direction to avoid stage backlash.