Focus Stacking in Linux

One of my favorite areas of photography is macro photography.   To me taking images of tiny things is just freaking awesome.  I don’t know why I love it, but so far it is my favorite type of photos to take and to edit.

Macro photography also poses many changes.  For instance, macro lenes and macro methods tend to produce a very shallow depth of field.  This means that the amount of stuff actually in focus in the picture is very thin.  If you want to take photos that have lots of stuff in focus, you have two options:

  1. Shoot with a very small aperture
  2. Focus stacking

Lets look at both of these options a little closer.

Shoot with a very small aperture: As the hole in the camera/lens gets smaller, the depth of field gets deeper.  This means that shooting at f11 will have a much larger depth of field than shooting at f2.8.  Perfect.  Just shoot at small apertures and stuff will be in focus.

There are some issues with this though.  You still may not have enough depth of field.  So if you are shooting something really thick and you want it all in focus f11 (or smaller) may not be enough.

Some lenses may also not be at there best at f11.  Maybe your lens looks best at f5.6 and you don’t want to go all the way to f11.

Add to this the fact that f11 takes a lot of light and you realize that shooting with a small aperture may not get the result you want.

Focus Stacking: Focus stacking is the process of taking many pictures with different elements of the image in focus and then combining the images so that the resultant image is in focus.

It’s a little related to HDR in that in HDR one combines the dynamic ranges of many images into one HDR image.  In focus stacking, one combines the in-focus regions of various images to render one image.

In this post we will talk about how I focus stack in Linux.

The Software: I do all my photography work in Linux (Gimp/Aftershot Pro/DarkTable/Hugin/QtPFSGui/etc).  I prefer to work with opensource software, but I am not against purchasing software when a good alternative doesn’t exist in the OSS world.  As to focus stacking directly the only software package I use is Hugin.  Although used primarily for panorama stitching (which I used to make the Venice stitch) it also works well for focus stacking.

The Process: When shooting Macro photography for focus stacking I use a tripod and rails to adjust the area of the shot that is in focus.  For the example used in this post, I shot 9 images:

These nine images were shot in RAW on my Canon 30D.  I copied them off my CF card into a directory on my Linux box and ran the following command:

dcraw -T *.CR2

There are lots of different options you can pass to dcraw to make it do different coolness for you, but for the sake of this post I kept the defaults.  This command reads in all the CR2 files and generates TIFF files from them.  This is needed because Hugin works best with tiff files.

Next we need to align the images so that they are all exactly over each other.  To do this I invoke:

align_image_stack -a aligned_ -v -m -g 10 -C *.tiff

This command makes new aligned_* images that are cropped and stacked on top of one another (I had to add -g 10 for align_image_stack to be able to align my image).

Now we just need to do the dirty work of actually focus stacking the image.  This could be done manually in Gimp by carefully erasing and layering the images, but that sounds like a pain and I am lazy so instead I just use Hugin again to focus stack the images for me using the following command:

enfuse -o result.tiff --exposure-weight=0 --saturation-weight=0 --contrast-weight=1 --hard-mask aligned_*

Hugin/enfuse now does the heavy lifting for us and combines our individually focused sections into one masterpiece image; hopefully everything in focus. As with align_imge_stack and dcraw, enfuse takes many command line options that you can use to tweak its results.

So how does the output look?  Well this is the result:

Not too shabby but it has some halo effect around it.  I am still working on figuring out how to get rid of this.  Any input is welcome and I will update this article as I learn more.



One thought on “Focus Stacking in Linux

Comments are closed.