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 |
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 |
The next step is to align the images. This will do two things.
- 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.
- 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 |
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.