AI Glossary / Computer vision and multimodal AI / Advanced

Shape from focus

Definition

Shape from focus estimates depth or surface shape by analyzing local sharpness across images captured at different focus settings or relative camera - object positions. It identifies where each region is best focused and maps that setting to depth through calibration. The resulting depth estimate depends on the focus measure, sampling of settings and consistency of the captured scene.

What it means

A focus stack contains images in which different parts of a scene appear sharpest at different settings. Those changes can help locate surfaces in depth. This differs from merely combining sharp regions into an all-in-focus picture: a visually sharp composite need not contain a calibrated depth map. It also differs from assuming the sharpest-looking region in one photograph is the nearest. The relevant evidence is how the same region's focus response changes across a known sequence of settings.

How it works

Capture a sequence spanning the depth range of interest and compute a local focus measure at each setting. For each region, select the strongest response or fit a peak model to neighboring responses. Convert the selected setting to depth using the system's calibration. Nayar’s cited technical report develops a sum-modified-Laplacian focus measure and compares discrete peak selection with interpolation. Registration, illumination consistency and texture affect the response curve. A flat, noisy or multi-peaked curve may provide weak evidence, and spatial smoothing can hide uncertainty if applied without care.

Three practical examples

  • A controlled inspection setup scans focus across a textured component and estimates the relative heights of visible regions.
  • A microscopy exercise compares a focus-stacked display image with a separately calibrated depth estimate.
  • A teaching experiment changes focus-step spacing to see how sparse sampling affects the location of a measured sharpness peak.

Think of it this way

Imagine tuning a radio through several frequencies and noting where a particular station sounds clearest. The clearest setting supplies a useful clue only if the tuning scale is understood and the signal remains consistent while you scan. Shape from focus similarly looks for a strong local response across controlled settings. The analogy also exposes a limitation: if the signal is weak or several peaks compete, choosing the largest measured value does not automatically make the answer reliable.

Worked example

In an original discrete illustration, three calibrated focus settings correspond to depths of 10,11 and 12 millimeters. A particular patch has focus scores 4,9 and 6 at those settings. A simple maximum-selection rule chooses 11 millimeters because 9 is the largest score. The true peak might lie between sampled settings, so this discrete choice does not establish submillimeter accuracy. Interpolation would require additional assumptions about the response curve. If all three scores were nearly equal, selecting the largest would still produce a number, but the underlying depth evidence would be much weaker.

Illustrative case study

A fictional precision-workshop team measures a textured sample using a focus sweep. Most regions show distinct peaks, while a polished area yields unstable scores and an edge shifts slightly between captures. The team examines registration and reports uncertainty where the focus evidence is weak. It checks recovered heights against a reference sample before using the system for routine work. This scenario illustrates the importance of capture and confidence, rather than suggesting that every pixel in a focus stack can support an equally trustworthy depth measurement.

Common misconceptions and mistakes

Shape from focus is not synonymous with focus stacking. One estimates geometry, while the other may aim only to produce a sharp composite. Another mistake is treating a sharpness score as a calibrated depth without relating it to the capture settings. Noise or texture changes can create misleading peaks, and larger measurement windows can mix surfaces at different depths. Movement during acquisition can also break correspondence. Finally, fitting a smooth curve between samples does not by itself justify arbitrary precision; accuracy must be established for the actual optics, sampling and surfaces.

Business use

The method can support controlled surface inspection when acquisition time, calibration and texture are suitable. Define the required height range and tolerance, choose focus sampling accordingly and validate using independent references. Test smooth, reflective and boundary regions rather than only favorable textured areas. Keep the depth output separate from the attractive all-in-focus rendering so visual quality does not substitute for measurement evidence. Record uncertainty or invalid regions where the focus curve is uninformative. A reliable workflow combines controlled capture with an honest account of what each local response supports.

People also ask

Does the highest score always reveal the exact depth?

No. Sampling, noise and model assumptions limit the estimate.

Can focus stacking alone provide metric shape?

Not necessarily. A sharp composite does not automatically include calibrated geometry.

Why is texture useful?

Local variation often provides evidence that changes measurably with focus, whereas a uniform patch may not.

Check your understanding

Which depth did the example's discrete rule select?

Answer: Eleven millimeters.

Why was that not a claim of exact depth?

Answer: The settings were sampled at finite intervals and the peak could lie between them.

What would nearly equal scores imply?

Answer: The method could still choose a maximum, but the distinction would provide weak evidence for a confident estimate.

Related terms

  • Focus measure
  • focal stack
  • depth from focus
  • depth from defocus
  • focus stacking
  • optical calibration

Learn next

Plot the three stated focus scores against calibrated depth. Add intermediate samples and compare the selected peak. Then construct a flat response curve and explain why a numerical maximum should not be treated as equally reliable, even though the same selection rule returns an answer.

More in Computer vision and multimodal AI

  • Separable filter - A separable spatial filter has a multidimensional kernel that can be expressed as a product of lower-dimensional kernels.
  • Shadow removal - Shadow removal is the process of producing an image or representation in which the effects of selected shadows are reduced or compensated for.
  • Shape from defocus - Shape from defocus estimates scene depth or surface shape from the way optical defocus changes image appearance.
  • Shape from shading - Shape from shading estimates surface orientation or shape from brightness variations interpreted through a model of illumination and surface reflectance.
  • Shape grammar - A shape grammar is a formal system of spatial rules that generates or describes designs by matching and transforming shapes.
  • Sharpening - Image sharpening is processing that increases the apparent definition of edges or fine detail, often by strengthening selected local intensity differences.

Sources

Shree K. Nayar, Shape from Focus, technical report CMU-RI-TR-89-27: https://cave.cs.columbia.edu/Statics/publications/pdfs/Nayar_TR89.pdf Carnegie Mellon Robotics Institute, publication record and abstract: https://publications.ri.cmu.edu/shape-from-focus The three-setting calculation, radio-tuning analogy and workshop case are original educational illustrations. The example demonstrates discrete peak selection without claiming an experimentally validated accuracy or prescribing a universal focus-step interval.

Previous: Shape from defocus Next: Shape from shading

  • Home
  • Daily AI Updates
  • Blog
  • AIFBL Academy
  • AI Glossary
  • Products and Tools
  • Store
  • Free CFO AI Prompt Pack
  • About Shihan Sheriff
  • Privacy Policy
  • Terms of Use
  • Refund Policy
  • Cookie Policy
  • Earnings Disclaimer