Sunday, August 30, 2015

Focus stacking with Magic Lantern and Hugin


I have tried focus stacking before, but I've never really focused on the topic, because I don't have a macro lens.  I tried out Jon's extension tubes while on holiday in South Africa last month, and of course that got me back onto focus-stacking.

I found a co-operative subject: a South African 10c piece, and took 18 pictures at different focus points, using Magic Lantern to change the focus automatically. Here are some examples from that series of images:





Because I had taken the pictures in raw mode, I used darktable to export them to TIFF files (not JPEG, because I wanted all the data to be present in the processing steps). I then used the following commands from the hugin/PanoTools suite to create the focus stack:

First I aligned the images, creating a second set of tiff files:

align_image_stack -m -a zframe_ *.tif

This was to make sure the images were aligned. Besides camera movement, which can be eliminated with a solid tripod, there might be side-effects of focusing, depending on the lens geometry.

Next, I created the stacked image:

enfuse -o coin.tiff --exposure-weight=0 --saturation-weight=0 --contrast-weight=1 --hard-mask zframe*

This tells enfuse to blend the images without trying to select pixels based on their exposure (--exposure-weight=0) or saturation (--saturation-weight=0); but by looking at the contrast of the area around the pixel (--contrast-weight=1), without smoothing out the area around the pixel (--hard-mask).

I then took the resulting .tif file to The Gimp to tweak the final exposure and contrast a bit, and to create a JPEG file. This was the result:




I think that worked pretty well, so I might just be buying a set of extension tubes soon.

Extra: as pointed out in the comments, one can automate this a bit more if you're not going beyond default settings. For example, with minimal effort you could create a very simple-minded script that looks like this:

#!/bin/bash
#Batch-export tiffs from raw images:
for i in *; do darktable-cli $i $i.tif; done
#Align the image stack:
align_image_stack -m -a zframe_ *.tif
#Blend the image stack:
enfuse -o output.tiff --exposure-weight=0 --saturation-weight=0 --contrast-weight=1 --hard-mask zframe*
#Make a JPEG from the stack (note: this is for a medium sized file - tweak the size and quality to taste)
convert output.tiff -quality 75 -resize 4000x4000 output.jpg


Make it executable and put it somewhere that's in your path, and call it something like "focusstacker". Then you can just go to where you've put the raw files and type "focusstacker" and let it run. In my case, it gave the following result:

#NoFilter (except for all the default ones in the camera and the software I used…)
I'd normally want to edit this in The Gimp before publishing anyway, because I like a bit more contrast, and in this case I also applied an unsharp mask, but because I didn't do anything in Darktable this time, I'd have gotten exactly the same result as I posted above if I'd skipped the last step in the script and taken the output tiff into The Gimp.

3 comments:

  1. Beautifully crisp.....being lazy I would prefer that the entire process be automated....

    ReplyDelete
    Replies
    1. It would be possible to automate it more, depending on how much flexibility you need. For example, if you don't want anything more than darktable's default settings, you can make your tiffs directly from the command-line by using darktable-cli. That way, you could easily write a script that combines the three steps of [create tiff] - [align images] - [create focus-stacked tiff] into one command. You could even add the step of creating a JPEG - just add a line like "convert coin.tiff -quality 75 -resize 2000x2000 coin.jpg" to your script. However, I wouldn't bother adding this: you probably anyway want to tweak a few things in The Gimp (or whichever image editor you like) before publishing, so you might as well open the TIFF there.

      But assuming you're doing it all, the script might look like this:

      #!/bin/bash
      #Batch-export tiffs from raw images:
      for i in *; do darktable-cli $i $i.tif; done
      #Align the image stack:
      align_image_stack -m -a zframe_ *.tif
      #Blend the image stack:
      enfuse -o output.tiff --exposure-weight=0 --saturation-weight=0 --contrast-weight=1 --hard-mask zframe*
      #Make a JPEG from the stack (note: this is a relatively small file - tweak the size and quality to taste)
      convert output.tiff -quality 75 -resize 2000x2000 output.jpg

      This gives a pretty acceptable result, and I think it should work OK in general as long as you don't need to do much to what your camera captures in the first place.

      Delete