lapstack

What focus stacking is

Depth of field runs out

A lens can only be focused on one plane at a time. Everything in front of and behind that plane is technically out of focus; there is simply a band around it where the blur is small enough not to show. That band is the depth of field, and it shrinks as magnification rises and as the aperture opens.

At ordinary distances this is rarely a problem — a landscape at f/11 is sharp from a few meters to the horizon. At 1:1 macro the band is a millimeter or two. At a microscope objective it can be a few micrometers, thinner than the specimen. A beetle photographed at 2:1 has a sharp antenna and a blurred abdomen, and no aperture setting fixes it: stopping down deepens the band but diffraction softens the whole frame, so past about f/8 at macro distances you trade one kind of blur for another and come out behind.

A stack is a series of focus planes

The way out is to photograph the subject several times, moving the plane of focus a little each time, so that between them the frames cover the whole subject in sharp slices. That series is a focus-bracketed stack, or just a stack, and each photograph in it is a frame. A macro subject might need ten to fifty frames; a whole insect at high magnification, two hundred or more; a microscope slide, whatever the specimen's thickness divided by the objective's depth of field demands.

The frames come from a focusing rail that steps the camera, a focus motor in the lens driven by the camera's own bracketing mode, or a microscope's fine-focus drive. Shooting a stack covers how to capture a good one.

What a stacker does

Given the frames, a focus stacker has to decide, for every part of the picture, which frame saw it sharp — and then build one image out of those parts without leaving seams where the decision changed.

That is harder than it sounds, for two reasons.

The frames do not line up. Changing focus changes magnification slightly — the image "breathes" — and a rail moves the camera, which changes perspective as well as scale. So the frames must be registered to each other before anything can be compared pixel to pixel. lapstack does this automatically; see alignment.

Sharpness is not a per-pixel property. Whether a pixel is in focus depends on its surroundings, and on the scale you look at. A fine texture and a soft broad gradient are sharp in different senses. Picking the "sharpest" pixel frame by frame produces a speckled mess.

lapstack's answer to the second problem is the one Burt and Adelson proposed in 1984: take the picture apart into spatial frequency bands — a Laplacian pyramid — and make the decision separately in each band, then put the picture back together. Coarse structure and fine texture are chosen independently, and because the reassembly is a smooth reconstruction rather than a paste, the blending between frames happens automatically with no seam to hide. How lapstack works goes through this properly.

And a depth map, for free

If you know which frame was sharpest at each point, and you know the order the frames were shot in, you know something about the shape of the subject: the frame index at which each point came into focus is a measure of its distance. Turning that into a usable, smooth, dense surface is a research problem in its own right — depth from focus — and lapstack runs a full modern pipeline for it on every stack, at essentially no extra cost, because each frame's focus measure is taken as the frame passes through the fusion.

The depth map is useful in itself (it says how deep the subject is, and where), and it is what lets lapstack render a stereo pair, a rocking animation and a textured 3D model from a stack shot with one camera that never moved sideways.

What lapstack will not do for you

  • It cannot invent detail no frame captured. If the step between frames was too large, there is a band of depth nothing saw sharp, and the result is soft there. No setting recovers it; reshoot with a smaller step.
  • It cannot fix a subject that moved. Alignment models the whole frame moving, scaling and rotating together. An insect leg that shifted between frames will ghost.
  • It struggles where one thing crosses another. A hair in front of a background is sharp in one frame while the background behind it is sharp in another, and both want the same pixels. This is where retouching earns its keep, and where the other renderings give you something better to brush from.

Source: sites/lapstack/content/docs/focus-stacking.md