Sunday, December 22, 2013

Easier focus stacking with Magic Lantern


Magic Lantern is really cool software: it allows you to do all kinds of awesome things with Canon EOS cameras. On Thursday I bought a 600D for Surice and myself, and before Friday I had installed Magic Lantern.

I was particularly interested in focus stacking because I sometimes like to take pictures of very small things. The basic principle is that you first make sure all the pictures are aligned, for example using a tool like align_image_stack and then stack them on top of each other, with the out-of-focus parts of each picture erased. This gives you a composite picture that's in focus all over. Of course, erasing the blurry bits by hand would take forever, so you use a tool like Enfuse to do that.

I've tested it, and it works relatively well. For my test picture, I used three Matryoshka dolls that I got from a Russian colleague.

Here's the standard "depth of field" Matryoshka Doll shot:


That was actually the middle picture from a set of 12, automatically taken one after the other by the camera, due to the magic of Magic Lantern. Normally, I'd look online (like on eduperez's website) for the commands to align the images, but when downloading the files off my flash disk, I saw some .sh files… files containing commands like this:

#!/usr/bin/env bash

# FST_0186.JPG from IMG_0186.JPG ... IMG_0197.JPG with aligning first

align_image_stack -m -a FST_AIS_0186 IMG_0186.JPG IMG_0187.JPG IMG_0188.JPG IMG_0189.JPG IMG_0190.JPG IMG_0191.JPG IMG_0192.JPG IMG_0193.JPG IMG_0194.JPG IMG_0195.JPG IMG_0196.JPG IMG_0197.JPG
enfuse "$@" --contrast-window-size=9 --exposure-weight=0 --saturation-weight=0 --contrast-weight=1 --hard-mask --output=FST_0186.JPG FST_AIS_0186*
rm FST_AIS_0186*


"What does that script do?" you ask: well, because Magic Lantern is written by Free Software geniuses, they gave a nice little GNU bash script as a bonus to their Free Software buddies. I just run that script, and got this:
Quite nice really: all three dolls are now in focus. However, if you look closely, you'll see some fringing or halo effect from the focus stack:

This comes from the algorithm that's erasing the out-of-focus areas not quite working perfectly. I found that someone had solved a similar issue by adding the parameters --gray-projector=l-star --contrast-edge-scale=0.3 to the command line - now while writing this up, I stumbled on Patrick David's blog - he explains how it works, and also introduces the GUI for Enfuse. Enfuse is pretty sophisticated software with lots of options: I'll have to RTFM to see what I can find out to make it work better.

Of course, in this case I could have achieved this effect by just selecting a very small aperture and a longer shutter opening. That's not always possible, though: sometimes you can't get everything in focus just by stopping down your aperture, and due to diffraction the resolution of a lens also gets worse if you stop it down too far, so focus stacking is still useful in these situations.