Previous Page
PCLinuxOS Magazine
Article List
Next Page

Focus Stacking In PCLinuxOS

by The CrankyZombie

Before we start, I would like to say thanks to both Tuxlink and Dockmakie for their comments, suggestions and help with this article.

Requires: Hugin/Enfuse

One of my favorite areas of photography is macro photography. Taking images of tiny things is just freaking awesome. I don't know why, but it is one of my favorite types of photos to take and to edit.

Macro photography also poses many challenges. For instance, macro lens 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

Let's 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 their 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 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 (High Dynamic Range) in that in HDR one combines the dynamic ranges of many images into one HDR image. In focus stacking, the user 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 open source 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.

The Process

When shooting macro photography for focus stacking, I use a tripod and a macro focusing rail to adjust the area of the shot that is in focus.

For the example used in this post, I shot eight images. In each image, a different section of the image will be in focus. Below are the eight images taken. While this example worked with as few as eight images, some can take many more individual images to complete a single photo.

The eight images were shot in both RAW and JPG on my Canon SL2/200D. I copied them off my SD card into a directory on my PCLinuxOS box. You can use either format. If you choose to use the RAW format, you want to run the following command. In a console window enter:

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.

I found using the CR2 (RAW) files from my camera, I ended up with an overall green tinge when they are converted, so I chose to use the JPG images and convert them to TIFF format using GIMP.

NOTE: I found by adding the -a attribute (calculate the white balance by averaging the entire image) to the command, it solved the green tinge for my camera.

Example: dcraw -a -T *.CR2

Next we need to align the images so that they are all exactly over each other. To do this we will use the following command:

align_image_stack -a aligned_ -v -m -g 15 -c 18 *.tif

This command makes a new aligned_* image files that are cropped and stacked on top of one another. Now we just need to do the dirty work of actually focus stacking of the image. This could be done manually in Gimp by carefully erasing and layering the images, but that not only sounds like a pain, it is a pain and I am lazy, so instead I just use Hugin again to focus stack the images using the following command. In a console window enter

enfuse -o result.tif --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, called result.tif. Hopefully everything in focus. As with align_image_stack and dcraw, enfuse has many command line options that you can use to tweak its results.

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

Previous Page              Top              Next Page