Focus Stacking

Dept of field is usually too shallow if we take macro photos.

In this example I'll take a photo of my extension tube. It is not too spectacular but quite on topic. The distances of the pins from the sensor are not equal.Even if I use the smallest aperture (F22 for my lens) only the middle pins are sharp. The ones too close to us or to far from us are out of focus.


You might think (after checking the images) that the extension tube is in a very bad shape. In fact it works great, the small scratches do not affect it.

One solution to the problem is the focus stacking when we create multiple images (using different focus distances) and merge the images later to create a new image with a much greater depth of field.


I was using a prime lens (SMC Pentax-A 1:2 50mm) and an 56mm extension tube (the other two elements of the set). This setup has 1.2:1 magnification.

I took 45 pictures.

I don't show all the 45 pictures, but here you can find three:

vaz vaz vaz

Image alignment

If we check the images we will can realize that the object "moves", it does not appear on the same place. First we need to align the images.

I'm using align_image_stack for this. We give the original images as input images (extensiontube_* files in this example) and the result is the exact same number of (slightly resized and repositioned ) images (ais_extensiontube*.tif in this example).

In theory we need the following command:

align_image_stack -m -a ais_extensiontube_ extensiontube*.png

In the real life image alignment will not always work so I was using the following command:

align_image_stack -c 16 -s 2 -m -a ais_extensiontube_ extensiontube*.png

A few explanations:

Probably it is possible to find the optimal values using some kind of algorithm, I was using (lots of) trial and error.

I show the previous three images again. Moving the cursor above the images will show the aligned version (there is no change for the first image):

Eredeti 00 Eredeti / 15 Eredeti / 40

Fusing the images

After the image alignment we need to merge the images by choosing the sharp part of the different pictures. I'm using enfuse 4.0.

In theory we need this:

enfuse -o output.tif --exposure-weight=0 --saturation-weight=0 --contrast-weight=1 --hard-mask ais_extensiontube_*.tif

Enfuse can use several criteria to check the pixels and choose the optimal source image. For focus stacking the most important is the contrast, that's why the weight for this is 100% (and 0% for exposure and saturation).

We also need --hard-mask otherwise the result is too soft.

In practice I used this command:

enfuse -o output.tif --contrast-edge-scale=0.3 --gray-projector=l-star --exposure-weight=0 --saturation-weight=0 --contrast-weight=1 --hard-mask ais_extensiontube_*.tif

The documentation of enfuse describes all the parameters and gives us a few hints how to choose the optimal values. Trial and error is also very useful.

The final image can be seen here:


It can be seen that the edge of the image (specially the top and the right edge) is not sharp because no input image shows this part in focus. We need to crop the image, I just show it for test purposes.

Demonstration video

The following video shows the original images, the aligned images, and the final result. It is much easier to see the the alignment on a video: