Tuesday, September 17, 2013

Using Hugin for focus stacking

Hugin is free and open source software that describes itself as a "Panorama photo stitcher" and, indeed, that is what the GUI provided as part of the package does. But, under the bonnet, it is a set of command line tools for manipulating and combining graphics files and it can also be used for combining shots to produce High Dynamic Range images and focus stacking.

For the purposes of focus stacking (i.e. combining images taken at different focus points to produce a single image with extended depth of field), two of the command line tools are key:
align_image_stack.exe
As its name suggests, this tool aligns images so that the same features are in exactly the same place in each.It automatically finds features.
enfuse.exe
Merges images together. It has a wide range of options which allow it to be used in all sorts of ways including focus stacking.

These tools can be found in the \Hugin\bin directory of your Hugin installation. Another significant file is enfuse.pdf, which can be found in the \Hugin\doc\enblend directory of your installation. This documents enfuse's options and has a useful section on focus stacking.

Chrysogaster solstitialis female
Take this as a reference image. It is a combination of 16 shots of a fresh, dead specimen taken at roughly twice life size (I intend to cover the equipment and techniques used in future posts). The original images are in RAW format from my Canon EOS 60D. This image was produced using Zerene Stacker 1.0.4 (my favourite stacking software and, in my opinion, "best of breed" at the moment).

The first step is to "develop" the RAW files to TIFFs. Most stacking software (including Zerene Stacker) works from TIFFs (the exception I know of is Helicon Focus which will handle the RAW conversion itself). I use Canon's Digital Photo Professional software (supplied with the camera) to do batch RAW conversions like this.

Bottom, middle and top original images from the stack
Here are three TIFF images from the top, bottom and middle of the stack. Note the somewhat grey background, the stage on which the insect is mounted (blue) and the head of the pin showing just left of the middle of the thorax. These have been cleaned up in post-processing the finished image above (using GIMP).

The next step is to align the images. This will do two things.
  1. Shift the images in the x and/or y axes to correct for any changes in their position. This will occur if the axis of the camera is not exactly aligned at 90°to the plane of the specimen. In practice, it is almost impossible to align things that accurately and, at these magnifications, even a very slight deviation off true will have a visible effect.
  2. Corrects for the inevitable change in magnification between images. As we move the camera nearer and further from the specimen, or change the focus of the lens, to cover the different focal planes, we inevitably change the magnification slightly.

Assuming that the \Hugin\bin directory is in your PATH environment variable (so that Windows can find Hugin's executable files) and that you are running the command from the directory where the TIFF images are located, the command will look like this:

"align_image_stack.exe" -a "\work\align_" -m -v <comma separated list of the .TIF file names>

This will probably take a minute or two and result in a set of files called align_nnnn.tif in a subdirectory \work from the directory where the original image files were located.

The -a switch allows you to set a prefix for the output images (otherwise they will just over-write the originals), -v means "verbose output messages" (not necessary, but allows you to watch progress as it runs) and -m tells it to correct the magnification of all but the first image (otherwise it only considers x,y shifts and rotations).

We can now combine the images using enfuse. The command is like this:

"enfuse.exe" -o "result.jpeg" --compression=100 --contrast-weight=1.00 --exposure-weight=0.00 --saturation-weight=0.00 --contrast-window-size=5 --hard-mask --gray-projector=luminance <comma separated list of the align_nnnn.TIF file names>

The key setting here is that we are telling it to use contrast-weight=1.00 but not exposure-weight or saturation-weight - this is what makes it do focus stacking. The contrast-window-size=5 sets the size of the window it uses to scan over the images to determine which has the highest contrast (i.e. is the most focussed). It should be an odd number and 5 or 7 are common values. See the documentation in enfuse.pdf for more details.

Here is the result.jpg that it produced (scaled to the same size as the reference image above):


Here is a comparison of the Hugin result to the output from Zerne Stacker (before post-processing) taking just a small part of the image around the base of the left wing:

Hugin left, Zerene Stacker right
There really is not a lot of difference that I can detect. Perhaps a little more hallowing around the knee of the hind leg in the Hugin result.

In conclusion, I would say that Hugin provides a good, free set of tools for focus stacking and the results can be excellent. The major downsides are:
  • align_image_stack.exe is slow! It does not take advantage of a multiple core CPU (and this is a job which is made for parallel processing!). Software such as Zerene Stacker and Helicon focus have optimised this step for mutliprocessors and it really shows! (enfuse.exe does have a multiprocessor optimised option though).
  • it is quite "techie" and means you have to dive in and get your hands dirty at the command prompt. I tackled this by writing my own GUI (in Delphi) which acts as a front end for these two command line tools and makes the whole process much easier.