Skip to content
PreciSim
physics

Optics and defocus

Depth of field, point spread function, the sharpness curve — and why Z focus calibration works the way it does.

Depth of field

Object-side depth of field, geometric-optics approximation:

DOF ≈ 2 · c · N · (1 + M) / M²

c  permissible circle of confusion (1–2 pixels; here 2 × 3.45 µm = 6.9 µm)
N  f-number
M  magnification = pixel size / pixel scale

On demo-2axis-vision: M ≈ 3.45 µm / 19.82 µm ≈ 0.174, N = 4, giving DOF ≈ 2.14 mm.

That number is why the Z focus search defaults to ±3 mm: a little wider than the depth of field, so the sharpness curve shows a complete peak. Search too narrow and the curve is monotonic, with no peak to fit.

Point spread function

Blur diameter for a defocus Δz:

d_blur = |Δz| · M / N

The virtual camera approximates the PSF as a Gaussian with σ = d_blur / (2·sqrt(2·ln2)), i.e. treating the blur circle as the full width at half maximum.

This is an approximation, not a diffraction-limited solution. It is good enough because sharpness measures care about how gradient energy falls off with defocus, and there the Gaussian matches measurement well. If you need a rigorous PSF — super-resolution, phase retrieval — this model does not apply.

Sharpness measures

Z focus calibration grabs a frame at each of a series of Z positions, scores sharpness, and finds the peak. Three measures are available:

Measure Formula Character
Tenengrad (default) Σ (Gx² + Gy²) from Sobel Sharp peak, moderate noise sensitivity
Variance Σ (I − Ī)² Fastest, sensitive to brightness changes
Laplacian energy Σ (∇²I)² Most sensitive to high frequency, and to noise

The absolute value of a sharpness curve means nothing; only the peak position does. Change the exposure or the target and the whole curve moves up or down while the peak stays put. People who threshold on absolute sharpness get caught out the first time the lighting changes.

Peak fitting

Do not just take the maximum sample — that caps precision at the step size. Fit a parabola through the peak and its two neighbours:

with y₁ = S(z_{i-1}), y₂ = S(z_i), y₃ = S(z_{i+1})
δ = 0.5 · (y₁ − y₃) / (y₁ − 2y₂ + y₃)
z_focus = z_i + δ · Δz_step

At a 0.2 mm step this moves focus precision from ±100 µm to the ±10 µm range.

It assumes the curve really is near-parabolic around the peak, so use only those three points — dragging far-defocused samples into the fit makes the answer worse.

How noise affects focus

Sharpness is a sum of squared gradients, and noise has gradients too, so noise lifts the whole curve's baseline — and lifts it more where the true gradient is small, i.e. far from focus.

The result: with more noise the sharpness curve flattens, the peak becomes less distinct, and the fitted focus wanders. The fix is to average a few frames before scoring (avgFrames), or raise exposure for a better signal-to-noise ratio.

Lab 4 does exactly this: push noise up, watch focus repeatability degrade from ±8 µm to ±60 µm, then recover it with frame averaging.

Last updated: Sep 21, 2026
Was this page helpful?