# Focus stacking

Focus stacking – also called focal plane merging, z-stacking, or focus blending – is a digital image processing technique which combines multiple images taken at different focus distances to give a resulting image with a greater depth of field (DOF) than any of the individual source images. Focus stacking can be used in any situation where individual images have a very shallow depth of field; macro photography and optical microscopy are two typical examples. Focus stacking can also be useful in landscape photography.
Focus stacking offers flexibility: since it is a computational technique, images with several different depths of field can be generated in post-processing and compared for best artistic merit or scientific clarity. Focus stacking also allows generation of images physically impossible with normal imaging equipment; images with nonplanar focus regions can be generated. Alternative techniques for generating images with increased or flexible depth of field include wavefront coding, light-field cameras and tilt.

## Technique

The starting point for focus stacking is a series of images captured at different focus distances; in each image different areas of the sample will be in focus. While none of these images has the sample entirely in focus they collectively contain all the data required to generate an image which has all parts of the sample in focus. In-focus regions of each image may be detected automatically, for example via edge detection or Fourier analysis, or selected manually. The in-focus patches are then blended together to generate the final image.
This processing is also called z-stacking, focal plane merging (or zedification in French).

### In photography

Getting sufficient depth of field can be particularly challenging in macro photography, because depth of field is smaller (shallower) for objects nearer the camera, so if a small object fills the frame, it is often so close that its entire depth cannot be in focus at once. Depth of field is normally increased by stopping down aperture (using a larger f-number), but beyond a certain point, stopping down causes blurring due to diffraction, which counteracts the benefit of being in focus. It also reduces the luminosity of the image. Focus stacking allows the depth of field of images taken at the sharpest aperture to be effectively increased. The images at right illustrate the increase in DOF that can be achieved by combining multiple exposures.
The Mars Science Laboratory mission has a device called Mars Hand Lens Imager (MAHLI), which can take photos that can later be focus stacked.

### In microscopy

In microscopy, high numerical apertures are desirable to capture as much light as possible from a small sample. A high numerical aperture (equivalent to a low f-number) gives a very shallow depth of field. Higher magnification objective lenses generally have shallower depth of field; a 100× objective lens with a numerical aperture of around 1.4 has a depth of field of approximately 1 μm. When observing a sample directly, the limitations of the shallow depth of field are easy to circumvent by focusing up and down through the sample; to effectively present microscopy data of a complex 3D structure in 2D, focus stacking is a very useful technique.
Atomic resolution scanning transmission electron microscopy encounters similar difficulties, where specimen features are much larger than the depth of field. By taking a through-focal series, the depth of focus can be reconstructed to create a single image entirely in focus.

## Software/application

Focus stacking software :
| Name | Primary author | Application type | Platform | License |
| Adobe Photoshop CS4, CS5, CS6 | Adobe | Desktop | Windows, Mac OS X | Proprietary |
| Affinity Photo 'Focus Merge' | Serif | Desktop | Windows, Mac OS X | Proprietary |
| Aphelion with Multifocus extension | ADCIS | Desktop | Windows | Proprietary, 30-day trial |
| Amira / Avizo 'Image Stack Projection' | Thermo Fisher | Desktop | Windows, Mac OS X, Linux | Proprietary |
| CamRanger | CamRanger | Desktop / Mobile | iOS, Android, Mac OS X, Windows | Proprietary |
| Chasys Draw IES | John Paul Chacha | Desktop | Windows | Proprietary |
| CombineZ | Alan Hadley | Desktop | Windows | GPL |
| CUVI Vision & Imaging Library | TunaCode | Desktop / Embedded | Windows, Linux | Proprietary |
| Enfuse (combined with align_image_stack or similar) | Andrew Mihal and hugin development team | Desktop | Multiplatform | GPL |
| FocusFusion | DelphiTools | Desktop | Windows | Proprietary |
| Focus Stacker | Alexander Boltnev, Olga Kacher | Desktop | Mac OS X | Proprietary |
| Focus Stacking Online | Focus Stacking Online | Web application | All | Proprietary |
| Shutter Stream Product Photography Software | Iconasys | Desktop | Windows, Mac OS X | Proprietary |
| Helicon Focus | Danylo Kozub | Desktop | Windows, Mac OS X | Proprietary, 30-day trial |
| ImageJ with Extended Depth of Field Plugin | Alex Prudencio, Jesse Berent, Daniel Sage | Desktop | Unix, Linux, Windows, Mac OS 9 and Mac OS X | Public domain |
| MacroFusion | Dariusz Duma | Desktop | Linux | GPL |
| Mathematica via ImageFocusCombine | Wolfram Research | Desktop / Web | Windows, Mac OS X, Linux | Proprietary, 15-day trial |
| Picolay | Heribert Cypionka | Desktop | Windows | Freeware |
| QuickPHOTO with Deep Focus extension | Promicra | Desktop | Windows | Proprietary, 30-day trial |
| Zerene Stacker | Rik Littlefield | Desktop | Windows, Mac OS X, Linux | Proprietary, 30-day trial |

## Gallery
### Pictures

* Pepper mill, stack of 28 frames
* Stacked image of 3 × 2.5 mm electric wires
* Shaver head, stack of 36 frames, retouched
* Macrolepiota procera, stack of 15 frames
* Stacked image of the inner ridge of an orchid blossom
* Stacked image of two Arecaceae viewed through a hole in a tree trunk
* Pellet, stack of 32 frames
* Alluaudia comosa, stack of 10 frames
* Mold on Litchi chinensis, stack of 20 frames
* Skull, stack of 6 frames
* Sympetrum flaveolum male, stack of 36 frames
* Pinot Gris grape, stack of 12 frames.

### Videos

* Focus stacking images of a lily
* Focus stacking images of a microchip

### Diagrams

* Software creates from the sharpest areas in a stack of sections.