Autofocus stands or falls with the number you feed it. Move a C-mount barrel, drive a liquid lens or turn an M12 holder, grab a frame at each step, and something must turn each frame into a single value that peaks exactly where the image is sharp. Pick a poor score and the lens parks a few turns off focus, oscillates around the peak, or chases sensor noise. Five focus scores cover nearly every machine vision implementation: Brenner, Tenengrad, Laplacian energy, energy gradient and plain variance. They do not behave alike. Three give a single honest peak, one needs the right threshold, and one can peak on blur. Below are the five formulas, the sweep behaviour of each, a worked 11-frame sweep with the arithmetic shown, and the decision rule to run in your station.
What a focus score has to do
A focus score maps one image to one number: higher means sharper. Sweep the optic across focus, grab a frame at every position, and plot score against position. That plot is the focus curve, and autofocus reduces to finding its maximum and moving the optic there. Any area scan camera behind a machine vision lens can produce the frames; the score itself is pure image math.
Three properties decide whether the curve is usable. Unimodality: one peak, because a curve with two humps defeats any peak finder. Unbiasedness: the peak sits exactly at best focus, not one step to the side. Sensitivity: a steep slope near the peak, so a step of one depth of field moves the score enough for the search to localize. These are the classic criteria from autofocus research in computer microscopy, where sweep scoring has been standard practice for decades.
Treat every score as relative. Lighting, gain and target texture scale all of them, so an absolute value means nothing between scenes. Compare positions within one sweep; never compare a score against an absolute cutoff.
Judging whether a single frame is sharp — a pass or fail check rather than a lens move — is a different problem. The Image sharpness measurement: choose a reliable focus metric guide covers it.
The five scores and their formulas
All five sum over the scoring region: the whole frame, or better, a region of interest on the part.
Brenner adds the squared gray differences between pixels two columns apart. The two-pixel lag responds to edge width and damps single-pixel noise, which a one-pixel difference would amplify. Proposed in 1971, it is still the cheapest score with a reliable peak.
Energy gradient is Brenner with a one-pixel lag in both directions, so it counts more edges per frame and still costs little — a natural fit for a real-time loop.
Tenengrad squares the Sobel gradient magnitude and counts only pixels above a threshold T. Keep T relative — for example, the 80th percentile of that frame's gradient values — because a fixed gray-level threshold breaks the moment exposure or lighting changes, and with some settings it inverts the whole curve.
Laplacian energy swaps Sobel for a second-derivative Laplace mask, squares the response and sums. The curve is the steepest of the five near the peak, but squaring a second difference also squares noise.
Variance measures the spread of gray values about the mean. It tracks overall contrast, not edges, and that is exactly why it misfires as a focus signal.
Which score gives one honest peak
| Score | Sweep curve | Noise behaviour | Cost | Use it for |
|---|---|---|---|---|
| Brenner | one sharp peak | tolerant | lowest | the default on textured parts |
| Energy gradient | one peak | tolerant | low | real-time focus loops |
| Laplacian energy | one steep peak | moderate | low | fine search on low-noise sensors |
| Tenengrad | one peak with a relative threshold | best of the five | medium | noisy sensors and line scan |
| Variance | off-focus peaks, double humps | worst | lowest | a sanity check, never the signal |
Published evaluations that score dozens of focus measures over real sweeps consistently rank the gradient-energy family — Brenner, Laplacian energy, energy gradient and a well-thresholded Tenengrad — at the top, and variance near the bottom wherever noise or background joins the region. In line scan autofocus tests with an auxiliary focus sensor, Tenengrad scored best in both global and local search. Sweep tests on calibration plates keep showing the same two failures: variance peaking on a visibly blurred frame, and Tenengrad inverting under a fixed threshold.
The decision rule by case:
- Textured part, low-noise area camera: Brenner, no threshold.
- Noisy sensor, high gain, or line scan: Tenengrad with a percentile threshold.
- Tight cycle time, stable part: energy gradient in a hill-climb loop.
- Microscope-class fine focus: Laplacian energy.
- Variance: report it if you like, but never let it drive the lens.
A worked focus sweep: 12 MP USB3 camera with a 35 mm C-mount lens
The case: a bench station focused by hand at setup, with the same routine planned under motor control. Inputs:
- Camera: LS-CMR-0166, a 12 MP USB3 camera, 4000 × 3000 pixels, 30 fps, rolling shutter.
- Lens: LS-FFL-0679, a 35 mm C-mount lens for 2/3" sensors, 6 MP.
- Scoring region: 300 × 300 pixels on the knurled edge of the part.
- Coarse sweep: 11 frames, 0.5 mm of barrel travel per step, grabbed in one direction.
- Gray data: 8-bit.
- Sensor format
- 1/1.7"
- Interface
- USB3
- Resolution
- 12 MP
- Shutter
- Rolling
- Type
- Machine vision fixed
- Sensor format
- 2/3"
- Mount
- C-mount
- Resolution
- 6-10 MP
The rule, in words: Brenner adds up, over the region, the squared difference between every pixel and the pixel two columns to its right.
Take six pixels across one edge of the knurl. In the sharpest frame the row reads 100, 104, 112, 118, 124, 130. The lag-2 differences are 12, 14, 12 and 12 gray levels, so this stretch contributes 144 + 196 + 144 + 144 = 628. In a blurred frame the same edge spreads to 106, 106, 110, 114, 118, 122; every lag-2 difference is 4, so the contribution is 16 × 4 = 64. One sharp edge outscores the blurred edge by 628 / 64 ≈ 9.8 times.
Grabbing the full sweep at 30 fps and normalizing to the best frame gives:
| Barrel position | Brenner score, normalized |
|---|---|
| 0.0 mm | 18 |
| 0.5 mm | 30 |
| 1.0 mm | 52 |
| 1.5 mm | 71 |
| 2.0 mm | 88 |
| 2.5 mm | 97 |
| 3.0 mm | 100 |
| 3.5 mm | 93 |
| 4.0 mm | 76 |
| 4.5 mm | 47 |
| 5.0 mm | 21 |
Eleven frames at 30 fps cost 11 / 30 = 0.37 s of cycle time.
The decision: the argmax sits at 3.0 mm of barrel travel, so the station moves there and holds. The curve passes both checks — a single hump, and a peak of 100 against 18 and 21 at the travel ends, more than twice. A fine sweep from 2.75 to 3.25 mm in 0.05 mm steps (11 more frames, another 0.37 s) narrows the park position to a tenth of the coarse step. Had the coarse peak landed on a travel end, the true peak would lie outside the sweep, and the travel must be extended before anything else.
The gray values and sweep scores here are illustrative; on your part expect the same curve shape, not the same numbers. Because this camera uses a rolling shutter, score with part and optic fully stationary — motion smears rows and fakes defocus.
Running the score in a station
Region first. Score a 256 × 256 pixel window or larger that sits on the part's texture, and keep the background out of it. A window on a blank region scores sensor noise, not focus.
Lock the exposure. Auto exposure and auto gain rescale frames between grabs, which tilts the curve and moves the variance baseline; the same feedback loop drives the failure in the auto exposure ROI instability guide. Run the sweep in manual exposure. The background in automatic iris, exposure and gain control explains what those loops do between frames.
Score mono images where the choice exists: a mono camera avoids demosaic smoothing, so gradients stay per-pixel. MV-CS200-10UM is a 20 MP mono camera over USB3.0 with a 1" sensor, and carries sweep scoring at setup and in production equally well.
- Sensor format
- 1"
- Lens mount
- C-mount
- Interface
- USB3
- Resolution
- 16-25 MP
Move in one direction. C-mount barrels have backlash, so approach the peak from the same side every time and the park position repeats. Let the optic settle before each grab: a liquid lens needs its response time after every command, and scoring mid-motion reads motion blur as defocus. The liquid lens autofocus guide covers those timings, and machine vision autofocus and image stabilization explained covers the actuation side.
Default to coarse-then-fine sweeps and take the argmax, as in the worked example. Hill-climbing — advance while the score rises, reverse once it drops — is faster but can stop on a noise hump; run it only with Brenner on a stable, well-textured part.
If the whole application runs on a smart camera, score the live stream in its setup tool and focus the adjustable holder by hand the same way. LS-SMC-0063 is a 1.6 MP smart camera with a 16 mm lens at 60 fps, and MV-SC3050XC-16M-ATN-H is a 5 MP color vision sensor; both deliver live frames fast enough for a setup sweep.
- Interface
- GigE
- Resolution
- Up to 3 MP
- Interface
- Fast Ethernet
- Resolution
- 5 MP
When the score cannot save you
No texture, no autofocus. A blank machined face, clear film or uniform plastic gives every gradient score nothing to track, and all five peak on noise. Fix the scene by moving the light off-axis to create texture, or measure the distance directly instead of through focus.
Motion defeats scoring. A conveyor that keeps running, a rolling shutter grabbing mid-motion or a vibrating mobile platform all smear edges and pull the peak toward blur; the Autofocus vs fixed focus lenses on mobile robots: vibration and blur guide covers the vibration case. Trigger the sweep with the line stopped.
Deep depth of field flattens the curve. Stopped down to F11 or beyond, the focus peak widens into a plateau and the argmax gets sloppy. The fix costs nothing: sweep and focus with the iris wide open, where the peak is sharpest, then stop to the working aperture for production. The depth of field simulation guide shows why a sharp preview at the working aperture misleads.
A peak that lands on a travel end is not a peak: extend the travel and sweep again. If the lens runs out of close-focus range first, the lens focuses only at close distance guide walks that diagnosis.
Checklist before you trust the number
- Region of interest on the part's texture, 256 × 256 pixels or larger, background excluded — background flattens the peak.
- Exposure and gain in manual — auto loops rescale the curve between frames.
- Mono or luminance scoring — demosaic smoothing softens gradients on color cameras.
- Brenner by default; Tenengrad with a percentile threshold on noisy sensors; variance never drives the lens.
- Coarse sweep over the full travel, one direction, frames grabbed after the optic settles.
- Single hump, peak at least twice the score at both travel ends — otherwise fix the region or extend the travel.
- Fine sweep at one fifth of the coarse step; park at the fine argmax.
- Focus wide open, then stop down to the working aperture for production.
Pick the hardware around the score with the camera finder and the lens finder, or start at the lens step of the configurator. More on scoring, actuation and station design sits on the machine vision systems topic page. Every order includes free expert support, so if a sweep curve looks wrong, send the frames and we will read it with you.
Questions
How many frames should an autofocus sweep grab?
Cover the whole focus travel with coarse steps about as wide as your depth of field, then rescan around the coarse peak with steps one fifth as wide. Ten to twenty coarse frames cover a typical C-mount barrel. At 30 fps a 15-frame sweep costs 0.5 s of cycle time, so sweeping every frame and taking the argmax is usually affordable.
Why does my variance score peak on blurred frames?
Variance measures gray-level spread, and many things change spread without moving focus: sensor noise, a gain step from auto exposure, a bright background entering the region. The curve then peaks off focus or splits into two humps. Score edges with Brenner or a squared Laplacian on a locked exposure and treat variance as a rough check, never as the autofocus signal.
Should I score the whole frame or a region of interest?
Score a region of interest that sits on the part's texture. Background pixels add spread that does not track focus, which flattens the peak, and on a small part in a large scene a full-frame score can peak wherever contrast is highest instead of where your part is sharp. Start around 256 × 256 pixels and keep the part filling most of it.
Do these scores work with liquid lenses and motorized barrels?
The scores only read images, so they work with any movable optic. Let the optic settle before each grab: liquid lenses need their response time after every step, and motorized barrels should approach the peak from one direction to cancel backlash. Scoring during motion reads motion blur as defocus and pulls the peak toward wherever you stopped.
Why did my Tenengrad curve run the wrong way?
Because the threshold was fixed. A hard gray-level threshold on the Sobel amplitude changes which pixels count as focus shifts, so the counted set, not the focus, drives the curve; with some thresholds it even inverts. Keep the threshold relative, for example a percentile of the frame's gradient values, or drop it and sum the squared gradient over the whole region.




