Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Autofocus: Contrast, Phase, and Actuator Control

Hello. Last lesson asked how a camera measures image brightness. Autofocus asks a different question of the light arriving through the lens: is the chosen subject sharp at the recording plane, and what adjustment would make it sharp? The distinction matters because a camera can detect that an image is blurred without knowing which way to move the lens.

We will develop two answers. Contrast detection evaluates sharpness while the lens moves; phase detection compares views formed by different parts of the lens aperture to estimate a signed focus error. We will then connect both measurements to an actuator that must position a real lens, not an ideal thin lens. Allow about 40–45 minutes, including the selected readings.


Focus is a property of a chosen subject

From the thin-lens geometry earlier in the course, an object at distance forms its sharp image at distance

behind a lens of focal length . Suppose the sensor is fixed at distance , where is the distance to which the camera is currently set. A subject at is sharp; one at another distance forms its sharp image on one side or the other of the sensor and appears blurred there. Changing focus moves a lens element or group so that the chosen subject’s image coincides with the sensor.

“Chosen” is essential. A scene may contain a nearby face and a distant window frame, each with a different best lens position. An autofocus system first needs a region of interest—selected by you or by a subject-selection algorithm. Its subsequent measurement answers how to focus that region, not which subject ought to matter.

The two methods below extract different information from the incoming light. A conventional image gives access to a sharpness score but, at one lens position, usually not the sign of the defocus. Separating light according to where it passed through the aperture preserves information about that sign.


Contrast detection: find the maximum by moving the lens

Read the introduction and opening focus-measure discussion of the empirical paper An extensive empirical evaluation of focus measures for digital .... It supplies a concrete image statistic and shows why the statistic must be evaluated at different lens positions. You need not read its catalogue of alternative operators for this lesson.

An extensive empirical evaluation of focus measures for digital ...

Read the paper’s account of contrast-detection autofocus to connect a numerical focus measure with a physical lens search.

In “1. INTRODUCTION,” read from the paragraph beginning “Contrast-detection autofocus makes use of a focus measure” through its discussion of live preview and performance: the search mechanism. Then, in “2. FOCUS MEASURES,” read from the definition of image luminance and the squared-gradient example through the paragraph explaining region-of-interest selection: the metric and region. Focus on what changes between measurements: the lens position, not merely the image-processing formula.

For an image captured at lens position , a simple score over a selected region is

Defocus spreads an edge across more pixels. For a suitably textured, unsaturated subject under stable conditions, that tends to reduce adjacent-pixel differences, so tends to peak near best focus. This is a local image-sharpness measure; it is not the global difference between the brightest and darkest parts of the scene.

The actuator can exploit the score without knowing the subject distance. It moves to a trial position, obtains another preview frame, and compares scores. If the score increases, it can continue in that direction; after bracketing a peak, it can take smaller steps or interpolate around it. The result is the lens position that maximizes the measured score within the search region.

Why not determine the direction from the first image? Geometric blur size depends primarily on the magnitude of separation between the subject’s sharp-image plane and the sensor. Similar-looking blur can therefore occur on either side of best focus. One score does not ordinarily reveal whether increasing will improve it. The camera has to probe the lens response. That search can cause visible hunting, particularly if it overshoots and returns; for a moving subject, each probe also consumes time during which the desired position may change.

Contrast detection has a valuable geometric advantage: when computed from the main imaging sensor, it judges sharpness at the surface that will record the photograph. It does not require a separate autofocus sensor to remain precisely registered to that surface. It is not infallible, however. A blank region offers no useful gradients, while noise can create strong gradients unrelated to subject detail. We will return to these limits after examining a measurement that contains directional information.


Phase detection: compare two aperture views

Read the opening of the research paper Learning to Autofocus. Its contrast between a focus search and dual-pixel disparity is useful here; keep in mind that its discussion concerns modern on-sensor phase detection, one implementation of a broader optical principle.

[2004.12260] Learning to Autofocus

Read the paper’s introduction for the distinction between selecting a subject region and estimating the lens setting, and for its description of the two aperture views.

In the opening discussion, start with “There are two main, independent tasks that a camera must address when focusing.” Read the autofocus comparison through the account of dual-pixel disparity and its measurement difficulty. Focus on why displacement between two views can carry information that a single blur magnitude lacks.

Imagine dividing the lens’s entrance pupil into left and right portions. Each portion forms an image of the same subject region, viewed through a slightly different set of rays. At correct focus, corresponding features in the two images line up after accounting for the sensor’s calibrated geometry. If the subject’s sharp-image plane lies in front of or behind the sensor, the features shift relative to one another. Which image shifts which way reverses with the sign of the focus error.

The supplied Dual Pixel CMOS AF Principle diagram depicts this reversal. Its upper panels show ray bundles converging before, at, or after the sensor; its lower panels sketch the corresponding signals from the two photodiode sets. Read it as a schematic of relative feature displacement across an array: an isolated photodiode cannot identify a scene feature or measure a shift on its own.

Three focus states in a split-photodiode sensor: when the ray bundle converges at the sensor, the two sets of photodiode signals align; convergence on either side produces oppositely ordered displacements.

A short ray calculation makes the sign explicit. Put the lens at axial coordinate zero and the sensor at . Let the centres of the two sampled pupil portions be separated by an effective baseline . For a subject at , denote its sharp-image distance by . A ray passing through pupil coordinate and the subject’s ideal image coordinate reaches the sensor at

Subtract the right-pupil feature position from the left-pupil position, with a consistent choice of left and right coordinates. In this idealized geometry the relative displacement is

The result is zero at ; unlike blur diameter, it changes sign as the subject crosses the current focus distance. This is the operational meaning of “phase” here: the camera estimates the relative shift of two image signals, commonly by finding the displacement that gives their best correlation. It is not measuring the optical phase of the light’s electromagnetic field.

There are two important hardware realizations. In a traditional SLR phase-detection system, light passing through portions of the taking lens is diverted via the mirror assembly to a dedicated autofocus sensor. Its detector arrays receive two re-imaged views and compare their patterns. Because that sensor occupies a different physical plane from the film or imaging sensor, their optical registration matters: a small misalignment can produce repeatable front- or back-focus despite a confident autofocus reading.

In on-sensor phase detection, the measurement is made at the imaging sensor itself. In the dual-pixel arrangement illustrated above, paired photodiodes under a pixel’s microlens collect light preferentially from opposite aperture portions. Reading the halves separately supplies two spatial signals for focusing; combining their charge can supply the ordinary image-pixel signal. Other on-sensor designs sample aperture portions differently. The common physics is the paired pupil views, not a particular pixel layout.

The effective baseline also explains a design trade-off. Greater separation between the sampled aperture portions produces more disparity for a given focus error, improving potential sensitivity. But a lens stopped down far enough cannot deliver rays from widely separated pupil portions; less light also weakens the measurements. A sufficiently detailed subject pattern must be present along the direction in which the two views shift. A uniform wall provides no shift to estimate, and some edge orientations are poorly constrained.


From a measured error to a lens command

Real cameras do not directly observe , , or a perfect thin-lens image. They estimate a shift between sampled signals, then use calibration to turn that shift into an actuator target. Read the calibrated correlation method in the appendix of Learning to Autofocus; it makes the transition from optical measurement to a usable focus setting explicit.

[2004.12260] Learning to Autofocus

Read the paper’s practical method for measuring a dual-pixel shift and converting it into a focus-distance estimate. The calibration discussion is as important as the ideal formula.

In “Appendix A.3 Single-Slice Baseline Algorithms,” find “ZNCC Disparity with Calibration.” Read the displacement search beginning “Specifically, to compute disparity” through the subpixel refinement: correlation and refinement. Continue from the paragraph beginning “Under paraxial and thin-lens approximations” through the field-dependent calibration table: model and calibration. Notice that a disparity is measured first; its conversion to a lens setting depends on the camera’s calibration.

In practice, the camera compares the two signals over candidate shifts, often normalizing them so unequal brightness does not dominate the comparison. A simplified correlation estimator is

where the hats indicate brightness-normalized left- and right-pupil images. A fitted peak can locate the shift more finely than one pixel. Calibration then relates the measured shift to focus distance. Writing the ideal relation as

the desired subject focus distance would be , so

Here only in our simplified model. In a real camera, aperture sampling, aberrations, vignetting, and position within the image can change the relationship; a measured calibration can be more useful than a nominal geometric baseline.

Consider the scale of the command. With , current focus setting , target , and ideal effective baseline , the lens-to-sensor image distance at the current setting is approximately . The ideal disparity is about , with the sign indicating a move toward a farther focus setting under our convention. Yet the required change in lens-to-sensor separation is only about , toward the sensor in this simple whole-lens-focusing model. Image displacement and actuator travel are different quantities. An internal-focusing photographic lens needs its own calibrated mapping from desired focus setting to motor command.

A controller must also decide when to measure again. A plausible phase-detection cycle is:

  1. Measure disparity in the selected region and reject a low-confidence match.
  2. Convert a reliable disparity to a commanded lens position, allowing for motor response and limits.
  3. After motion and settling, measure again; correct a residual error if needed.

This can be faster than sweeping many positions to find a contrast peak, but “one-shot” focus is an aspiration, not a guarantee. A stepper mechanism has finite increments and may have backlash. A voice-coil actuator can accept an electrical command without reporting its exact resulting lens position. Temperature, orientation, and lens motion can alter the command-to-position relationship. Re-measurement closes the loop using the image, even when the actuator itself has limited position feedback. A hybrid system can use phase detection for a coarse directional move and contrast detection to refine the final sharpness at the imaging sensor.

The paper’s hardware and image-model caveats are worth a final, selective reading. They prevent a misleading conclusion that either a sharpness maximum or a calibrated disparity is a perfect physical observable.

[2004.12260] Learning to Autofocus

Read three short subsections in “4 Autofocus Challenges” for cases where the optical statistic or the actuator model departs from the idealization.

In “Unrealistic PSF Models,” read blur-kernel limitations. In “Noise in Low Light Environments,” read noise and gradients. Finally, in “Hardware Support,” read actuator limitations. Focus on which failures affect the measurement and which affect the conversion from a correct measurement into lens position.

The distinction between those failures is practical. If a contrast score peaks at the wrong position because a saturated blur disc or sensor noise creates strong edges, moving the motor more precisely will not fix the decision. If disparity is measured correctly but the actuator stops short, a new measurement and correction can help. If two subjects at different depths occupy the selected region, neither method can supply a unique answer until the camera decides which subject to privilege.


Conclusion

Contrast-detection autofocus evaluates image sharpness at successive lens positions and seeks a maximum. Its strength is measuring sharpness on the recording sensor; its fundamental cost is that a single ordinary image generally does not indicate which way to move. Phase-detection autofocus compares images formed from different aperture portions. Their relative displacement supplies a signed focus error and, after calibration, an estimate of the needed lens setting.

Neither signal moves glass by itself. The complete autofocus system must select a subject region, assess measurement confidence, translate an optical estimate into an actuator command, and verify the result when calibration or motion makes an initial move uncertain. In the next lesson, we turn to another controlled lens-or-sensor motion: stabilization against camera shake.

Can't find a good explanation? Sign up and we'll make it for you

Sign up