Focus Stacking: Combining Images for a Sharper View

There’s a tradeoff involved when you try to focus on things under a microscope. The higher the magnification the less you’re able to focus on at a time. Images 1, 2, and 3 in Figure 1 show a catalpa pollen grain under 400x magnification. In each image the microscope is focused slightly differently to bring a different level of the pollen grain into focus.

Three images (image 1, image 2, and image 3) are stacked together to create a final, focus-stacked image that is in better focus (has a larger depth of field). The images are of catalpa pollen grains at 400x magnification, stacked using Hugin Tools.

I tried two methods for doing the image stacking. The first was with the command-line programs in Hugin Tools, while the second was by hand using GIMP.

Hugin Tools

The method for focus stacking with Hugin Tools is described here by Patrick David and here by Edu Perez. It requires two commands, one to align the images, because focus-stacking requires very well aligned images, and another to stack them together.

Align the images using align_image_stack:

align_image_stack -m -a als_ catalpa-c[123].tif

This command takes three image files (catalpa-c1.tif, catalpa-c2.tif, and catalpa-c3.tif) and produces three aligned images prefixed with “als_” (als_0000.tif, als_0001.tif, and als_0002.tif).

The images are stacked together using enfuse (details here).

enfuse -o catalpa-pollen-out-b.tif --exposure-weight=0 --saturation-weight=0 --contrast-weight=1 --hard-mask --contrast-window-size=9 als_000?.tif

which produces an output file called catalpa-pollen-out-b.tif.

Focus-stacked image of a catalpa pollen grain using Hugin Tools. (Magnification 400x).

GIMP

Focus-stacking with GIMP requires opening all the files as layers and adding transparency masks to the layers to leave behind just the areas that are in focus. The general method is shown in this GIMP tutorial.

Aligning images by hand and then selecting the areas to cut out can be quite tedious.

400x magnified, catalpa, pollen grain that was focus-stacked by hand with GIMP.

Notes

Two key things to keep in mind are:

  • the better aligned images are to start with the easier they are to focus-stack. Aligning images is tedious even with align_focus_stack;
  • the quality of the camera matters a lot. The images above were taken with a Moticam 2 megapixel camera attached to the microscope to reduce misalignment. The image below, however, was taken with a point-and-click, 6 megapixel camera down the eyepiece. You can see a lot more detail.
Catalpa pollen grain taken with a higher resolution camera.
Higher resolution, focus-stacked image of catapa pollen grain. (magnification 400x)