Preview — buttons open the staging app

TL;DR: we do all this and more in our app, for free. Try without an account → or Skip to the tool ↓

How to flatten a Z-stack into one sharp image

One channel of a fluorescence Z-stack flattened into one all-in-focus image, shown in greyscale Topography map of the same field, coloured from slice 1 to slice 15 by the slice at which each point is in focus, with grey where there is no signal
Left, one channel of a fluorescence Z-stack flattened into one image. Right, its topography map: the slice at which each point is in focus. Data: a Z-stack of the organ of Corti, the part of the inner ear that turns sound into nerve signals, by Glen MacDonald, University of Washington (source).

Depending on the sample you are imaging, whether it is a thick tissue section or an organoid, chances are it is not flat. It then follows that whatever microscope you use will only keep one region of that sample in focus at a time, while the rest is blurred.

The typical answer is Z-stacking. A series of images of the same field is taken at different focal planes. Between them, every part of the sample is in focus somewhere. It is just spread across the slices.

The same stack played slice by slice. Each nucleus is sharp in only a few of the 15 slices, and no single slice has them all in focus.

Flattening combines those slices into one 2D image.

Why it matters

  • Most analysis expects 2D. Segmentation, counting and morphology measurements are simpler and faster on one image than on a volume.
  • A single slice can be misleading. Manually selecting the "best" slice in the stack in terms of resolution means you lose some resolution where some features are slightly above or below that plane.
  • Everything downstream depends on it. A hazy projection gives hazy segmentation.

The usual techniques and their limits

TechniqueHow it worksWhere it works bestLimitations
Maximum intensity projection Keeps each pixel's brightest value across slices Fluorescence work where measuring signal is key Brightness is not focus, so bright pixels win over ones that are in focus. In brightfield this often gives a poor result, as in-focus details can appear darker than out-of-focus ones
Average or sum projection Averages or adds every slice Fluorescence work where you want the total signal through the full depth of the sample Blurred slices are mixed in with sharp ones, so fine detail and contrast wash out
Single best slice Keeps the one slice that is sharpest overall Largely flat samples, where everything sits close to one plane Anything slightly above or below that plane loses resolution
Deconvolution Uses a model of the optics to put blurred light back where it came from Fluorescence stacks where the optics are well characterised and you need the full 3D volume Needs an accurate point spread function, careful settings and a lot of compute. Wrong settings add artefacts
Focus stacking (our preferred method) Measures local sharpness and keeps the sharpest slice at each point Viewing, segmentation and surface shape, on uneven and thick samples Keeps one depth per pixel, so it does not suit intensity measurement or full 3D work
The same region flattened four ways: the single best slice, an average projection, a maximum intensity projection and focus stacking
One region of the stack, flattened four ways. The best single slice loses the nuclei above and below it. The average is soft. On fluorescence like this, the maximum projection already looks good. Focus stacking keeps every nucleus sharp, but shows speckle in empty background, where no slice is sharp.

Why we prefer focus stacking

Focus stacking, also called extended depth of field, finds where each point of the sample is in focus and keeps it.

  • It selects on sharpness, not brightness.
  • It decides pixel by pixel, so uneven and thick samples come out sharp across the whole field.
  • It keeps real pixels. Every pixel in the result comes from one of your own slices. Nothing is modelled or reconstructed; the images are only contrast-stretched for display.
  • It gives you a height map as a by-product. Knowing which slice was sharpest at each point tells you the shape of your sample.

It is a well-established method with decades of published work behind it.

What the tool in this post does

  1. It opens your file. ImageJ and OME-TIFF hyperstacks, plain multi-page TIFFs and colour TIFFs all open with their slices and channels in the right order. If the file records the spacing between slices, we use it.
  2. It works out what each channel is. A channel with a bright background is treated as brightfield; one that is mostly dark with bright signal is treated as fluorescence. Fluorescence channels are shown in the colours your file uses, or in ImageJ's default colours if it stores none, so the result looks like what you see in ImageJ or Fiji.
  3. It scores sharpness at every pixel of every slice. We use a Laplacian filter, an edge detector that responds strongly to in-focus detail and weakly to blur. The response is averaged over a small neighbourhood to give a local focus score.
  4. It keeps the sharpest slice at each pixel. A light smoothing step then makes neighbouring pixels come from the same plane, which removes speckle at boundaries.
Slice 7 of the stack The focus score of slice 7: bright where its detail is sharp, dark where it is blurred or empty
Left, one slice. Right, its focus score: bright where the slice is sharp, dark where it is blurred or empty.
Building the result. Going through the stack, each point is kept from the slice where its focus score is highest (top). The slice it came from becomes the height map (bottom).

What you get back

  • An all-in-focus image.
  • A topography map. The record of which slice was chosen at each pixel is a height map of your sample, in micrometres when the file includes the slice spacing.
  • A CSV of heights, with position, chosen slice, height and a focus confidence score for each block of pixels.
  • A maximum intensity projection for fluorescence stacks, so you can compare against the view you already know.

Limits, stated plainly

  • Heights are in slice numbers unless your file records the Z step.
  • Depths are nominal. They are the stage step. A refractive index mismatch between the immersion medium and the sample can stretch or compress true depth.
  • It needs texture or signal. Featureless regions are shown as "no signal".
  • There is no drift correction. If the sample moved between slices, edges can double.
  • Height resolution equals slice spacing. The map cannot resolve depth finer than the step between slices.
  • File limits: 25 MB, 64 slices, 4096 pixels per side, 6 channels. Images larger than 2048 pixels are shown and downloaded at 2048 pixels on the longest side; the CSV always covers the full-resolution map.

Flatten a Z-stack now

Don't have images you can use? Try one of these samples. Download it, unzip it and upload the TIFF inside.

  • Confocal Series: 2 channels, 25 slices, 8 MB
  • Organ of Corti: 4 fluorescence channels, 15 slices, 16 MB. The file records 0.315 µm between slices, but its author gives 0.3 µm, so heights read about 5% high.
  • Fly Brain: colour, 57 slices, 11 MB

For all this and more, you can try our app today for free.

Further reading

  • S. K. Nayar and Y. Nakagawa, "Shape from Focus", IEEE Transactions on Pattern Analysis and Machine Intelligence, 1994.
  • S. Pertuz, D. Puig and M. A. Garcia, "Analysis of focus measure operators for shape-from-focus", Pattern Recognition, 2013.