Align by shift and scale · Laplacian sharpness · depth map · pyramid blend

Focus Stacking

Merge a macro or product sequence shot at different focus distances into one picture that is sharp from front to back. List the photos in focus order, from nearest to farthest or the reverse, and the depth map shows which photo supplied each area.

Photos in focus order

2 to 20 photos

Drop 2 to 20 photos here or click to choose

PNG, JPG, WEBP, GIF or BMP, up to 25 MB each. New photos are ordered by file name, with numbers read as numbers.

    Settings

    All photos together are limited to 40 megapixels, so a bigger stack is stacked smaller.

    Sharpness is averaged over this radius. Small values follow fine texture, large ones give calmer decisions.

    Softens the border between areas taken from different photos.

    A majority filter that removes lone wrong picks. Too high rounds off thin features such as hairs.

    Stack and save

    Result

    Add the photos of one focus sequence.

    Focus Stacking

    Operator: Abdulla Al Fahad. How tools are built and checked

    At 1:1 magnification the depth of field is a fraction of a millimetre to about a millimetre, so a beetle photographed from antennae to wing needs a dozen frames, each sharp in a different slice. Focus stacking takes the sharp part of every frame and joins them into one picture that is sharp throughout.

    The hard part is not choosing sharp pixels but making the frames agree: moving the focus ring changes the magnification slightly, and a hand-held or rail-mounted camera shifts a little between frames. The page therefore aligns each frame for shift and scale before it compares sharpness.

    It also shows a depth map, in which every area is shaded by the frame that supplied it. That map doubles as a check that the stack followed the subject and did not pick frames at random.

    Key features

    • 2 to 20 photos in focus order, reorderable, with natural sorting of file names on entry
    • Alignment of every photo to the middle one: median-threshold-bitmap shift, then a search over scale (plus or minus 6%) and shift at three resolutions
    • Per-pixel sharpness from smoothed squared Laplacian energy, with a detail window you set
    • A majority filter on the depth index map and soft per-photo weights from blurred selections that always sum to one
    • Laplacian-pyramid blending in a worker you can cancel, and a depth map view with each photo's share of the frame
    • Crop of the borders that alignment filled in, JPG or PNG export, and a table of shift, scale and share per photo
    • A comparison slider against the reference photo so the gain is visible at 100 percent

    Alignment, sharpness, depth map and blending, step by step

    Photos must share one shape, within 3 percent of the first, and are resized to the same working size before stacking. All frames together are held to 40 megapixels, so 20 frames of 3:2 photos are stacked at about 1732 x 1155 and 8 frames at 2400 px stay at 2400 x 1600. The working-size setting picks 1600, 2400 or 3200 px on the long side, lowered when the budget demands it, and the page says when it has done so.

    Alignment starts from a whole-pixel shift found by median threshold bitmaps, the method also used by the exposure fusion tool here, and refines scale and sub-pixel shift by maximising the correlation of gradient magnitudes at 160, 320 and 640 px. A result is used only if it improves the correlation by at least 0.01 over no change, because resampling softens a picture and a spurious half-pixel move costs more than it gains. Rotation is not corrected.

    The sharpness of each pixel is the squared 4-neighbour Laplacian of a lightly blurred grey copy, averaged over the detail window. The photo with the most sharpness wins the pixel. Where no photo has any detail the choice is noise, so the reference photo is used. Two passes of the majority filter then remove lone wrong choices, and its radius is a setting.

    How it works

    The winning index per pixel becomes one selection plane per photo, each blurred by the softness setting. Because the planes are a partition of 1 before blurring, the blurred planes still sum to one, and each is turned into a Gaussian pyramid. Every photo's three colour channels become Laplacian pyramids, which are multiplied by the weight pyramid level by level and summed.

    Collapsing the blended pyramid gives the picture. Working in pyramids means coarse levels, which carry smooth tones, are blended over wide transitions and fine levels, which carry detail, over narrow ones, so there are no visible seams. The depth map is the weighted mean photo index at each pixel.

    Common use cases

    • Twelve frames of a beetle, shot on a rail with 0.1 mm steps. The depth map runs smoothly from black at the antennae to white at the wing tips, which shows the stack followed the subject.
    • Eight shots of a watch at eight focus distances, 2400 px wide. A clean-up radius of 2 removes speckle on the plain dial, where there is little detail to guide the choice.

    How to use this tool

    1. Drop the photos; they are ordered by file name, with 2 sorted before 10, so check the order and fix it with Up and Down.
    2. Leave alignment on for hand-held or rail-shot sequences and turn it off for a tripod-locked, constant-magnification set.
    3. Press Stack photos and watch the progress; Cancel stops the worker.
    4. Open the depth map and look for noise or the wrong order, then adjust the detail window or clean-up and stack again.
    5. Download the picture as JPG or PNG.

    Example

    A six-frame stack of a circuit board shot at a steep angle, pcb_01.jpg to pcb_06.jpg (illustrative)

    The board's near edge is at the left of every frame and its far edge at the right.
    Focus moves one step between frames, so pcb_01 is sharp at the left edge and pcb_06 at the right.
    Tripod, fixed exposure, 480 x 320 px each. Default settings: alignment on, detail window 3, clean-up 2.

    What the page reports for a sequence like this

    Shares like 18/17/20/17/18/9 % across pcb_01 to pcb_06, with the last frame covering least
    Alignment: scale 1.0000 and no shift for every photo, as expected from a locked tripod
    Depth map: grey rising from roughly 28% brightness at the left edge to roughly 69% at the right, a smooth ramp that follows the board's tilt
    The stacked picture is 480 x 320 px with the whole board in focus

    Common pitfalls

    A moving subject, such as a flower in wind or a living insect, changes between frames and cannot be aligned by shift and scale. Expect ghosting around the parts that moved.

    Halos around bright, high-contrast edges are common in stacks. Raising the blend softness widens them and lowering it makes them sharper but can reveal seams.

    Exposure drift between frames, such as a shifting light on a window sill, shows up as patches whose brightness follows the depth map. Fix the exposure and the light before the sequence, not afterwards.

    • Set the detail window large (6 to 10 px) for smooth subjects such as a petal and small (1 to 2 px) for fur or sand, where fine detail is the thing you want to preserve.
    • If thin features such as hairs vanish or fatten, lower the clean-up radius to 0 or 1; the filter treats a one-pixel-wide choice as an error.

    Common errors and how to fix them

    "has a different shape from the first photo"
    One file is cropped differently or rotated. Rotate or re-export it to the same shape as the others, or remove it.
    "The processing worker stopped, probably from running out of memory"
    Too many large frames for the browser. Use fewer photos, or choose a smaller working size.
    Result is no sharper than the reference
    The frames may be focused at the same distance, or listed out of focus order. Check the depth map: if it is a single flat shade one photo supplied everything.

    Best practices

    • Take one frame beyond each end of the subject, so the nearest and farthest parts are sharp in at least one photo.
    • Shoot with fixed exposure and white balance so the pyramid does not blend slightly different tones.

    Why alignment includes scale

    Most lenses change their effective focal length a little as the focus ring moves, an effect called focus breathing. Between the first and last frame of a macro sequence the subject can grow by a percent or two, which at 2400 px wide is 12 to 24 pixels at each edge. A shift-only alignment cannot fix that, and the edges of the stack would show doubled detail, so the search includes a scale factor.

    Reading the depth map

    Black means the first photo in your list, white the last, and grey the photos between. A smooth ramp across a sloping subject is a good sign. Blotchy or speckled grey on a plain surface means the sharpness signal was weak there; raise the clean-up radius or the detail window.

    Frequently asked questions

    Does the order of the photos matter?

    For the picture, only a little: the sharpest photo wins each pixel whatever the order. It matters for the depth map, whose shades follow the list, and for the reference, which is the middle photo of the list.

    Why is the result smaller than my photos?

    All frames are held to a total of 40 megapixels so a 20-frame stack fits in browser memory, and borders that alignment filled in are cropped. The working-size setting and the page's size line show the exact result size.

    Can I stack handheld shots?

    Small shifts and scale changes, yes; rotation, no. Handheld macro usually works for a few frames shot quickly, and tends to fail for long sequences where the camera has rotated.