Lenses & opticsExplainer

Calibrate a camera with autofocus: lock the focus first

You can calibrate a camera with autofocus only at one locked focus position. What autofocus changes, how big the error gets and when to recalibrate.

LumaSpec Vision9 min read Share on LinkedIn

On this page 8 sections
  1. 01What autofocus actually changes
  2. 02What a calibration stores, and what a refocus invalidates
  3. 03Worked example: a 25 mm lens that refocuses 4% away
  4. 04How to capture a calibration that stays valid
  5. 05When autofocus calibration is good enough
  6. 06Choosing hardware that keeps focus locked
  7. 07Checklist: keep the calibration true
  8. 08Questions

A camera that focuses itself raises an uncomfortable question the first time you set it on a calibration target: if the lens moves between frames, what exactly did the calibration measure? The answer is clean. You can calibrate an autofocus camera, but the result is valid for one focus position only. Refocusing shifts the effective focal length by a few percent, and that rescales every pixel-to-millimeter number the calibration produced — by amounts hundreds of times larger than a good calibration residual. This guide shows what autofocus moves, works the error through with real numbers, and gives the capture procedure and hardware choices that keep a calibration true from the bench to the line.

What autofocus actually changes

Autofocus maximizes sharpness by moving one or more lens groups until the current subject distance forms a sharp image on the sensor. The thin-lens relation ties the distances together, and the field of view follows from the focal length:

1/f = 1/s + 1/s' FOV = sensor width × WD / f

Here s is the object distance, s' the image distance, f the effective focal length and WD the working distance. Which term moves decides how the calibration breaks. Compact modules in phones and board cameras shift a group inside the lens, which changes the effective focal length itself; a 2021 IEEE study of autofocus lenses in camera calibration found that the focal length changes slightly at every refocus and skews a calibration taken at another position. Barrel designs that move the whole lens keep f fixed and change the working distance instead. Either way the pixels-per-millimeter mapping moves, because focal length and working distance are both terms in the field of view.

Machine vision mostly escapes the loop: machine vision lenses are focused by hand and locked, so nothing moves unless a person moves it. The question stays live for phone-class cameras, for area scan cameras fitted with motorized-focus lenses, and for liquid-lens rigs.

How sensor width, working distance and field of view set the focal length Rays from the edges of the sensor pass through the lens and spread to the edges of the field of view. The ratio of sensor width to field of view is the magnification m.CameraField of view (FOV)Working distance (WD)≈ fsensor width slensm = s / FOVf = WD × m / (1 + m)
How sensor width, working distance and field of view set the focal length. Rays from the edges of the sensor pass through the lens and spread to the edges of the field of view. The ratio of sensor width to field of view is the magnification m.

What a calibration stores, and what a refocus invalidates

A calibration describes one optical state, not the camera. The intrinsics are the focal length in pixels fx and fy, the principal point cx and cy, and the distortion coefficients, usually k1, k2, k3, p1 and p2. The pixel focal length is the glass focal length divided by the pixel pitch, so a 4% focal-length shift is a 4% shift in fx and fy, and a 4% error in every pixel-to-millimeter conversion downstream.

The distortion coefficients drift as well, because moving the groups changes the aberration balance; the error is smallest near the image center and grows toward the edges, where measurement edges tend to sit. Two consequences follow. Convert pixels to millimeters only through the calibrated state of the moment — calculate object space pixel size from magnification and lens distortion correction for accurate pixel-to-mm measurements cover that chain. And treat a small refocus as a bigger problem than it looks, because the scale error lands across the whole image at once. The next section puts a size on it.

Worked example: a 25 mm lens that refocuses 4% away

The case: a camera calibrated at one locked focus position covers 100 mm across the sensor width with a 25 mm lens, then autofocus refocuses for a nearer part and the effective focal length lands at 26 mm. The inputs:

  • f = 25 mm, the calibrated focal length, from the lens specification
  • f' = 26 mm after the refocus, a 4% shift — the size of change a refocus produces
  • FOV = 100 mm across the sensor width at the calibrated position, from the calibration
  • 4000 pixels across that direction, from the image format

The rule: at a fixed working distance the field of view scales inversely with focal length, FOV' = FOV × f / f'. The arithmetic:

  • FOV' = 100 mm × 25 mm / 26 mm = 96.2 mm
  • Scale error = 100 mm − 96.2 mm = 3.8 mm, which is 3.8% of the field
  • Pixel scale at the calibrated state = 100 mm / 4000 px = 0.025 mm per pixel = 25 µm per pixel
  • Scale error in pixels = 3.8 mm / 0.025 mm per pixel = 152 px

One refocus of a few percent moves the image scale by 152 pixels. A healthy locked-focus calibration reprojects to a few tenths of a pixel, so the refocus costs about 500 times the residual. The decision follows, with its threshold: for any measurement use, a focus change of any size voids the calibration — recalibrate at the new position and store the result as a new entry. A single stored calibration is justified only by a focus lock that cannot move.

Work your own lens and sensor through the same relation with the focal-length calculator:

Lens focal length, field of view and working distance
Units
Solve for
Focal length
– mm
Field of view
– mm
Working distance
– mm
Magnification
– x
Angle of view
– °
Pixel on the part
– mm

Thin-lens model: m = sensor width / field of view, f = WD × m / (1 + m). Accurate to a few percent above about 150 mm; check the lens's minimum object distance.

Find lenses with this focal length Open the full calculator

A real camera lands on the example's pixel count directly: the LS-CMR-0166 resolves 4000 × 3000 over USB3.

Area scan cameras12 MP USB3 area scan camera, IMX226, 1/1.7" rolling shutter, 30 fps, 4000 × 3000 resolution (LS-CMR-0166)LS-CMR-0166
Sensor format
1/1.7"
Interface
USB3
Resolution
12 MP
Shutter
Rolling
$560.00View partBuild a system with it

How to capture a calibration that stays valid

The capture procedure is a standard intrinsic calibration with one addition: the focus state is frozen and recorded. Give the focus position a repeatable setting procedure first — camera focus during calibration: set a repeatable position covers that in detail.

  1. Fix the state you will run: working distance, f-number, zoom if fitted, and focus. If you let autofocus find focus, let it settle once on the real part position, then switch it off.
  2. Lock what can be locked. Tighten the focus lock screw, set the iris to a fixed value, and clamp camera and target so nothing drifts mid-capture; a camera stand holds the camera side of that.
  3. Acquire 15 to 25 poses of a flat checkerboard or ChArUco target at that one distance, center and corners covered, tilts up to about 45°. Size the target to the field first — calibration target field of view and lens coverage margins gives the sizing margins.
  4. Solve and read the residual. An average residual of a few tenths of a pixel is a healthy result for a locked-focus lens; above about half a pixel, suspect blur, a bent target or a focus move mid-capture. Camera calibration residual values: average and max targets sets out the acceptance numbers.
  5. Store the parameter file with its state: focus position, f-number and working distance travel with the intrinsics as one recipe.
  6. Verify on a feature of known size. A systematic offset above about 0.1% of the dimension means the optical state moved; noise produces scatter, never offsets.
  7. Re-run the full capture whenever focus, zoom, f-number or working distance changes. One confirmation frame is not a recalibration.

When autofocus calibration is good enough

Detection and sorting at a fixed station can live with a moving focus. If the task asks whether a feature is present and never converts pixels to millimeters, a few percent of scale wobble changes nothing, provided depth of field keeps the refocused image sharp — stop down toward F5.6 to F8 and the margin gets generous. The moment the number is the product, the answer flips: dimensional gauging, robot pick offsets and any measurement that inherits the pixel scale need a focus that cannot move.

Motorized-focus and liquid-lens setups deserve a pattern, not an accident: one full calibration per focus position, stored in a table and switched with the focus drive value. Liquid lens autofocus for changing working distances shows the hardware side of that approach. Structure-from-motion pipelines follow the same logic from the other direction: a locked-focus calibration initializes the solve, and focal length is left free so the optimizer refits it per image in tools such as COLMAP.

The cheapest fix is choosing a lens so focus never has to move; how to choose a machine vision lens focal length walks that choice from field of view and sensor size. The rest of the lens material sits on the lenses topic page.

Choosing hardware that keeps focus locked

Machine vision avoids the question by construction: focus is set by hand at the working distance and held by a lock screw, and nothing in the pipeline moves the elements afterwards. On a vibrating line, anti-vibration lenses go further and clamp focus and iris mechanically. The LS-FFL-0475 is a 36.61 mm anti-vibration lens for a 2/3" C-mount camera. The LS-FFL-0567 is a 16 mm, 1.1", 20 MP C-mount lens with an F2.8-22 iris and under 0.3% distortion. Low distortion is worth its place in a calibration rig, because the solver has less aberration to absorb and the residual stays small.

Machine vision lenses36.61 mm, 2/3", C-mount anti-vibration machine vision lens (LS-FFL-0475)LS-FFL-0475
Type
Anti-vibration
Sensor format
2/3"
$299.00View part
Machine vision lenses16 mm, 1.1", 20 MP, C-mount machine vision lens, F2.8-22, <0.3% distortion (LS-FFL-0567)LS-FFL-0567
Type
Machine vision fixed
Sensor format
1.1"
$679.00View part

The mechanical half matters just as much: the LS-MNT-0040 is a slide-table camera stand with a 700 mm column and a 400 x 450 mm base that pins the camera-to-part distance between calibration day and production.

Camera standsMachine vision camera stand, professional slide-table type, height 700 mm, 400 x 450 mm base (LS-MNT-0040)LS-MNT-0040
Type
Camera stands
$629.00View partBuild a system with it

If the application genuinely needs refocusing on the fly — varying part heights, several stations at different distances — pick a motorized-focus or liquid-lens lens deliberately and budget one calibration per position.

Checklist: keep the calibration true

Before capture:

  • Focus locked, screw tightened or autofocus disabled. Any later movement rescales fx and fy.
  • Iris fixed at the production f-number, which belongs to the calibration because it changes aberrations and depth of field.
  • Camera on a stand, target flat and rigid, both at the production distance.

After the solve:

  • Average residual at the few-tenths-of-a-pixel target. Above about half a pixel, hunt for blur or motion before trusting the numbers.
  • Known-feature check inside tolerance. Offsets mean a moved state; noise cannot make offsets.
  • Parameters stored with focus position, f-number and working distance.

In production:

  • Any focus, zoom or distance change triggers a full recalibration, not a spot check.
  • Re-check the gauge regularly. Temperature and vibration move focus slowly over a shift or a season; if daytime blur appears, camera focus drift with temperature: diagnose daytime blur traces it to the focus position.

The decision in one line: calibrate an autofocus camera at one locked focus position, store the state with the parameters, and treat the next refocus as the next calibration. To spec a set-up that never asks the question, start at the lens configurator or the lens finder, or ask us — free expert support comes with every order.

Questions

Can you calibrate a phone camera that has autofocus?

Yes, with two conditions. Lock focus and exposure so nothing moves during the capture, and shoot every target pose at the same camera-to-target distance from that locked position. Expect larger residuals than a fixed-focus machine vision lens delivers, and treat the result as valid for that one distance only. For measurement work, a manual-focus machine vision lens is the better tool.

How much does one refocus change the calibration?

The pixel scale changes by the same percentage as the focal length. A 4% refocus on a lens calibrated across a 100 mm field leaves 96.2 mm, an error of 3.8 mm. On a 4000-pixel-wide image that is about 152 pixels of scale drift, against a residual of a few tenths of a pixel. Any refocus dwarfs the calibration.

Does a machine vision camera have autofocus?

Usually not. Machine vision lenses focus by hand, and most carry a lock screw, so the focus position is fixed by construction. The autofocus question appears with phone and board cameras, with motorized-focus lenses and with liquid-lens setups. Those need either one calibration per focus position or a lookup table keyed to the focus drive value.

What must stay fixed for a calibration to stay valid?

Four things: the focus position, the f-number, the working distance and, on a zoom lens, the zoom setting. Focus and zoom change the focal length, the f-number changes depth of field and residual distortion, and the working distance scales the field of view. If any of them changes on purpose, run the full calibration again and store the new parameters with the new state.

Is an autofocus camera usable for robot guidance?

For coarse guidance, yes: lock focus at the distance the robot sees, calibrate there, and hold that distance fixed in the cell. For millimeter-accurate pick offsets the calibration must not drift, so prefer a manual-focus lens with a lock screw, or run one calibration per focus position and switch parameters with the focus setting.