Easy Focus Stack in Linux

focus stacked shell, final result

Do you what to know how to align, and blend, a Focus Stack sequence in linux?
I’m going to show you an easy way to focus stack in Linux using a few simple commands in Terminal.
This method only works if you have Enfuse installed but if you have Hugin – Panorama photo stitcher installed it’s already there. If you don’t have Hugin, pop over and get it, it’s free.

Opening Images in Terminal

Create a folder for your images.

Right Click in the folder, and click on Open in Terminal.

The Align Script

Run the following command and press enter

align_image_stack -m -a OUT $(ls)
align command in terminal

Enfuse will align the images and save them in the same folder, they will be labelled OUT and numbered sequentially.

The Focus Stack

We are going to ask Enfuse to blend the aligned images by running the following command in terminal and pressing enter. You can copy and paste them from here.

enfuse --exposure-weight=0 --saturation-weight=0 --contrast-weight=1 --hard-mask --output=baseOpt1.tif OUT*.tif

Enfuse will then output a single blended image with the name baseOpt1

In my case the image looks like this…

I cleaned up a bit of ghosting, tidied up the background and cropped edges in Gimp.
Easy!

focus stacked shell, final result

Other Commands

Remove halo from focus stack

Play with the window size (odd numbers only) to improve ghosting or halo effect. I haven’t used this option apart from testing it and the default value seems works okay in some situations.

enfuse --exposure-weight=0 --saturation-weight=0 --contrast-weight=1 --hard-mask --contrast-window-size=5 --output=baseOpt2.tif OUT*.tif

Increase contrast focus stack

Experiment with the contrast-edge-scale value. The default option gave a softer image but reduced the ghosting a little I haven’t.

enfuse --exposure-weight=0 --saturation-weight=0 --contrast-weight=1 --hard-mask --contrast-edge-scale=0.3 --output=baseOpt3.tif OUT*.tif

Stack and Save Masks

I wanted to see if I could save the “focus masks” that Enfuse uses to blend the images. I want to use the masks to manually blend landscape focus stacks in Gimp.
I added –save-masks into the align code and to my surprise it aligned the images and saved the Hard mask and Soft mask for each image.

enfuse --exposure-weight=0 --saturation-weight=0 --contrast-weight=1 --hard-mask --save-masks --output=baseOpt4.tif OUT*.tif

I would love to hear how you get on, leave a comment or insight below.
There is a Enfuse GUI I haven’t used and would be interested in hearing from anyone who has.

Happy Stacking!

John B

Darran Mountains, Gimp edit

On a recent trip, I found myself at the entrance to the homer tunnel on the Te Anau to Milford Highway. It was very late in the afternoon and I didn’t intend going to Milford Sound so I turned around and pulled over. The mountains surround me and seem to go straight up, in front on me is a very old tree, covered in lichen and looking like something out of Lord of the Rings. I decided to make this tree the focus of an image, the scale of what was in front of me was too much for a single frame so I decided to shoot a 4xframe panorama.

Darran Mountains with tree
Darran Mountains

I did some basic edits in Darktable before creating the panorama in Hugin Panorama Stitcher, both are free and open source. The stitch was pretty good but the image didn’t really work.

Image from Hugin Panorama Stitcher
Image from Hugin Panorama Stitcher

The tree blended into the background and although the detail is there it was always going to be a big ask to get the tree to stand out. The edit was quite complicated but thanks to a few new tools in the latest version of GIMP 2.10.20 I got a result I was happy with.
The silver dot in the sky on the left side is the moon and not a UFO.

I made a small video of the drive to Homer Tunnel with a couple of photos on the way.

They will get better…
This is the shot I took on the way.