How Focus Stacking Combines Multiple Exposures

Focus stacking is a digital image processing technique that merges multiple photographs taken at different focal distances to produce a single image with an extended depth of field. In macro photography, optical physics severely limits how much of a subject can remain in focus at once, often rendering only a paper-thin slice sharp. By systematically capturing a series of exposures across the subject and processing them through specialized software, photographers can eliminate blur and achieve edge-to-edge sharpness that is physically impossible to capture in a single frame.

The Macro Photography Dilemma

At high magnification levels, depth of field shrinks dramatically—often to fractions of a millimeter—even when using narrow apertures like f/8 or f/11. Stopping down further introduces diffraction, an optical phenomenon that softens the entire image. Focus stacking solves this trade-off by keeping the lens at its sharpest aperture while artificially extending the perceived depth of field through software manipulation.

Step 1: Image Alignment

Before any blending can take place, the editing software must align the entire stack of images. Even when mounted on a sturdy tripod, minor shifts can occur due to wind, shutter vibration, or mechanical movement of the focus rail.

Furthermore, adjusting the focus changes the optical magnification of the lens, a phenomenon known as "focus breathing." This causes objects to appear slightly larger or smaller from one frame to the next. The software corrects this by scaling, rotating, and warping the individual frames to ensure that common features match precisely across the entire sequence.

Step 2: Edge Detection and Sharpness Analysis

Once aligned, the algorithm evaluates each pixel across all exposures to determine where the sharpest details lie. It does this by analyzing local contrast and high-frequency edge transitions:

  • Contrast Detection: In-focus areas exhibit steep transitions between light and dark pixels, whereas out-of-focus areas show gradual, soft transitions.
  • Frequency Analysis: High spatial frequencies indicate fine texture and sharpness, allowing the software to isolate the optimal focal slice from each frame.

The software generates a depth map or a set of internal layer masks based on these contrast calculations, assigning each segment of the subject to the specific frame that captured it with the highest fidelity.

Step 3: Blending and Merging Methods

The software applies one of two primary computational methods to composite the sharp slices into a unified image:

  1. Depth Mapping (D-Map): The software assigns depth values to pixels and creates a continuous map of the subject. This method is effective for subjects with smooth surfaces and distinct spatial separation, but it can struggle with complex, overlapping geometries.
  2. Pyramid Blending (P-Max): The software breaks down each image into multiple frequency bands and merges the highest-contrast details across all levels. This method excels at preserving fine, overlapping details like insect hairs or crystalline structures, though it can introduce noise or alter color fidelity in flat, out-of-focus backgrounds.

Modern editing workflows often combine both methods or allow the user to blend the results to balance contrast preservation and clean backgrounds.

Step 4: Artifact Resolution and Retouching

The final phase of the process involves correcting computational artifacts. The most common issue is "haloing," which occurs when a sharp foreground element borders an out-of-focus background, confusing the edge-detection algorithm. Dedicated focus stacking software includes retouching brushes that allow editors to manually clone detail from specific source frames back into the final composite, painting out halos, ghosting, or movement artifacts to deliver a clean, sharp macro photograph.