Focus Stacking: Enhancement of Depth of Field

0
572

The loss of detail in an image due to phenomena related to depth of field is a daily reality that any photographer must contend with. However, there are various computational strategies that can help us challenge these physical properties of our lenses to achieve perfectly sharp images across all possible focus planes.

When working with macros, this technique becomes somewhat indispensable and common, as it can help us extend our depth of field, allowing us to obtain images with a continuous level of detail across all focus planes.

The theory of Focus Stacking

correa focus stackingThe Focus Stacking technique is also known as Focus Blending, Z-Stacking, and even Extended Depth of Field, and is based on the fusion of images taken with different focus planes, and therefore different levels of detail by regions.

The concept of image fusion presents both challenges and different results. It is not enough to simply add pixels or average them, as this would contribute out-of-focus areas to our new hyperfocal image, which is undesirable. Focus-stacking algorithms seek the best way to locate image areas with the most detail, or zones of high frequencies.

pyramid laplacian focus stackingAmong these strategies, one of the most used is the search for image details through high-pass filters, known as low-pass difference pyramid methods or Laplacian pyramid (image on the left). From the result of recursively applying these low-pass filters, aiming to preserve high frequencies by eliminating low frequencies (areas with little detail), Gaussian pyramids or weight maps are derived. From these processes, weighting criteria are deduced, allowing the selection of the most optimal pixels from each image. The image at the top of this article shows a stacking of various images and how each contributes to the final fused image.

hardmaskAlthough the foundation of focus-stacking may be complex to assimilate, the practice is quite straightforward, as there are numerous tools on the market, many of them free to use, that allow us to stack and fuse our image sequences in a very simple manner.

In the image on the left, the areas of maximum sharpness of the image can be seen in light tones, which will contribute more significantly to the resulting fused image.

The practice of Focus Stacking

First, we must obtain a series of images taken at different focus distances, ensuring that the transitions between distances are as smooth as possible.

When working with macros, particularly extension tubes, it is very easy to observe the depth of field phenomenon.

Once this sequence is taken, it must be processed with a stacking and fusion tool.

The concept of stacking and fusion encompasses an image alignment process, since not only would small camera vibrations hinder fusion, but the variation in focus distances produces slight oscillations in the FOV.

One of the most popular free tools, on which many others are based, is ENFUSE by Andrew Mihal. Enfuse, besides improving focus, performs exposure enhancement processes, very similar to the HDR concept.

The difficulty in using ENFUSE lies in the command line operation, and the need for prior alignment with the align_image_stack tool contained in the HUGIN project, also command line based. The steps for aligning an image stack and fusing it with these two tools are very simple:

align_image_stack -m -a OUTPUT *.TIF

Where “OUTPUT” is the prefix that will be added to the aligned image stack. Using the usual wildcard “*” we can load all TIF images in a directory.

enfuse --exposure-weight=0 --saturation-weight=0 --contrast-weight=1 --hard-mask  --output=enfuse.tif OUTPUT*.TIF

Where “enfuse.tif” is the name of the resulting image, and with “OUTPUT*.TIF” all TIF images starting with the prefix OUTPUT, described in the previous step, will be loaded.

With these simple instructions, we have our stack aligned and fused.

If working with the terminal is unfamiliar, we can download numerous graphical fusion tools based on ENFUSE such as ImageFuser or the proprietary tool PhotoAcute, which I have already described in previous articles.

Another alternative for image fusion, since it is a very common process in scientific environments, is to use the program ImageJ and its various plugins:

For focus-stacking with ImageJ we have Stack-Focuser and Extended Depth of Field with numerous configuration options.

However, one must not forget stacking and alignment. ImageJ can load image sequences as stacks but we will need StackReg with the TurboReg library by the same author to align our image stacks.

The result

ceramica focus stacking From the Focus-Stacking fusion processes, we can obtain truly gratifying images, in a very simple way and almost intrinsic to any macro photography work.

In the image on the left, a typical situation can be observed when recording a relief, in this case an archaeological ceramic, under a significant level of magnification, where it is essential to resort to Focus Stacking to achieve optimal sharpness throughout the entire relief. Although two starting images are shown in the example, actually 5 were used.

On certain occasions, the presence of highlights or overexposed areas within our bokeh zone, or “out-of-focus” areas, can generate halos during fusion, so we must pay attention to the behavior of certain textures.