Focus Stacking Macro Photos (Enfuse)

I recently got a new camera to update my aging (gracefully!) Olympus E-P1. Deciding to stay with the µ43rds (or µ43rds, or micro four-thirds - I just wanted to play with some HTML entity codes), I found a really tasty new Olympus OM-D E-M5 waiting for me for Christmas! In deciding which kit lens to go ahead and get with it, the Olympus M.ZUIKO DIGITAL ED 12-50mm F3.5-6.3 EZ was a no brainer. Water-resistant (to match the camera), and a neat macro function!

This article is about focus stacking, for exposure blending, see here:
Automatic Exposure Blending with Enfuse (HDR-ish)

Well, I couldn't help but snap a couple of macro shots of my trusty Warhammer 40k Space Marine to play with this newfangled lens.

The problem, if you haven't done macro photography before, is that the depth of field will become razor-thin. You can alleviate it a bit by closing down the aperture a bit, but at some point you'll hit diffraction limits (and reduced sharpness).

Focus Stacking

So, what if you want a deeper depth of field in your shots? Well, there's a neat technique called "Focus Stacking" where you will take multiple images, with the only thing that varies between them is the focal point. Look again at the lead image, and specifically notice how the focus plane shifts along the length of the gun body.

Basically, compose and capture an image with the focus at one point, then focus a little further into your image, repeat as often as needed to have the range you want all in focus.

Focus stepping across my subject.

The problem, of course, is that you now have multiple images, all with different focus planes. How can you combine them all into one image where the different in-focus areas are all combined?

Well, we're going to use the open source Enfuse to do this dirty work for us. First, though, we need to make sure the images are properly aligned first...

Hugin Align Images

Well, Enfuse will work best if the images that are going to be combined are all as perfectly aligned as possible. To do this, we're going to use align_image_stack from Hugin.

Hugin is a really useful tool to have installed if you haven't already. To follow along with any of this, you'll need to install it anyway - so go do that now. Once installed, you can invoke the align_image_stack script from the bin/ directory of wherever you installed Hugin. It will look something like this (on Windows at the moment, so this is what the Windows command will look like on my machine):
C:\Program Files\Hugin\bin\align_image_stack -m -a OUT FILE1 FILE2 FILE3Don't Panic. To follow along, basically, put all of your focus stacked images into a single directory. Open a command prompt, and navigate to that directory.

Assuming your Hugin installed in the same place as mine, you can run the command as I show above. The -m switch will optimize the field of view for all images except the first. The -a OUT will prefix all the output files as OUT...tif, and FILE1 FILE2 etc... are the names of each of your focus stacked images you are starting with.

When it's done, you'll have a few new images in your directory named OUT000#.tif. These are your newly aligned output images, and will be what we work with from here on out.

Enter Enfuse

At this point you've got your images aligned perfectly (hopefully), and now all that is left is to mask each image so only the sharpest, in-focus portions show through. This is exactly what we are going to get Enfuse to do for us.

Enfuse can be run from the command line (the same bin/ directory where align_image_stacks was located) as part of a Hugin installation, or there is a GUI called EnfuseGUI that will expose all the options along with fast previews.

The EnfuseGUI interface.

I'll cover both here just to be thorough.

From the manual for Enfuse, there is a section that specifically addresses focus stacking. In particular, they refer to a bare-bones call for Enfuse to stack the images:
enfuse \
--exposure-weight=0 \
--saturation-weight=0 \
--contrast-weight=1 \
--hard-mask \
... \
--output=output.tif \
The same options entered in EnfuseGUI are located at "Fusion options":

Base focus stacking parameters in EnfuseGUI.

Exposure and Saturation weighting should be set to 0, while Contrast weighting should be set to 1 and Force hard blend masks should be checked. These options will not be changed as you dial in your focus stacking parameters.

You can actually go ahead and run it at this point to see what your output will look like. It should run reasonably quick, and you can investigate the results. The nice thing about EnfuseGUI is that you can generate a fast preview of the results using the Preview button instead of a full run (this is very useful while you tweak parameters to see what type of effect they'll have).

EnfuseGUI preview of base focus stacking parameters.

If you're happy with these results, then stop here. That's it! Quick, relatively painless, and you're happy with the results. Go have a beer! (Seriously, many people will be fine with these results, and don't have to head any further).

If you're curious whats happening behind-the-scenes, you can pass the option:--save-masksto have Enfuse save each of the masks that it creates. Below, for example, are the masks that were generated for the above image. You can clearly see the in-focus plane in white in these masks:

Masks generated by Enfuse (white areas in-focus).

Enfuse Expert Options

If you're still here, I am going to assume that you are less than satisfied with the results.

If you pay careful attention to high-contrast edges in your image, you may notice some faint "ghosting" on either side. On the dark side of a high contrast edge, you may notice a faint, light seam, and vice-versa on the light side. There may also be some slight "smearing" in areas that obscure some details.

One of the options that I find to produce slightly better results are to use the L* channel of LAB colorspace as the grayscale projector to Enfuse (don't worry about the technical stuff - basically use the L* channel in LAB to create the masks as opposed to averaging).

Using L* from LAB to create masks

If you notice some haloing effects around high contrast edges, you can mitigate them a bit by increasing the size of the box Enfuse uses to evaluate contrast detection. The option to adjust this box size is:
--contrast-window-size=XWhere X ≥ 3. The documentation points out that values larger than 5 might produce blurry images, but I would experiment with values all the way up to 9 to see how it effects your final output.

There is another good option to tweak your final output, and that is to enable the Laplacian Edge Detection algorithm. Laplacian Edge Detection can be enabled by passing a positive value to:
--contrast-edge-scale=XWhere X typically ranges between 0.1 - 0.5 (pixels), and 0.3 is suggested as a good starting point.

Unfortunately, due to a bug in EnfuseGUI, the GUI won't accept values that are commonly used for the correct parameter. The GUI only accepts integer values, with no way to pass a decimal value (0.3 for instance).

No problem! We can just switch gears here at this point and fall back to the trusty command line. So at this point, besides the base parameters, I am going to add in contrast-edge-scale to enable the Laplacian Edge Detection algorithm:
enfuse \
--exposure-weight=0 \
--saturation-weight=0 \
--contrast-weight=1 \
--hard-mask \
--gray-projector=l-star \
--contrast-edge-scale=0.3 \
--output=output.tif \
You can compare the results of adding in the l-star option and using the Laplacian edge detection at 0.3 pixels below:

Base stacking parameters detail (mouseover for contrast-edge-scale=0.3)

In Summary

Whew! That was a lot of information. Let me summarize the steps here. Assuming you have all of your stack images in the same directory:

  1. Use Hugin's align_image_stack to make sure all of your images are perfectly aligned.align_image_stack -m -a OUT FILE1 FILE2 FILE3
  2. Try Enfuse with the base focus stacking parameters to see if you are happy with the results.enfuse --exposure-weight=0 --saturation-weight=0 --contrast-weight=1 --hard-mask --output=base.tif OUT*.tif
  3. You can also try a different type of grayscale conversion, I've found the L* channel from LAB to be pretty good overall ( --gray-projector=l-star ).enfuse --exposure-weight=0 --saturation-weight=0 --contrast-weight=1 --hard-mask --gray-projector=l-star --output=base.tif OUT*.tif
  4. If there are halos around high contrast edges, you can try increasing the contrast-window-size a bit. ( --contrast-window-size=5 ).enfuse --exposure-weight=0 --saturation-weight=0 --contrast-weight=1 --hard-mask --contrast-window-size=5 --output=base.tif OUT*.tif
  5. If details are smeared, try using Laplacian Edge Detection instead. (Adding --contrast-edge-scale=0.3 to the base command), and possibly removing --contrast-window-size.enfuse --exposure-weight=0 --saturation-weight=0 --contrast-weight=1 --hard-mask --contrast-edge-scale=0.3 --output=base.tif OUT*.tifModify the value between 0.1 and 0.5 to see which result is best for you.


If you want a truly sharp macro shot with a large, smooth depth of field, you'll be taking a lot of photos. Seriously. I've seen 100+ images used for a single macro shot. Look at these:

Red eyes
Red eyes by Gilles San Martin, on Flickr. 52 image stack.

Xylotrechus arvicola
Xylotrechus arvicola by Gilles San Martin, on Flickr. 55 image stack.

Try keeping an overlap between each of your shots. It'll avoid any seams in the final result. Remember, Garbage in - Garbage out.

The documentation offers a good tip as well - if the in-focus to out-of-focus transition is too sharp, try taking another image at the transition area with a larger aperture to allow a smoother, more natural falloff to occur.

Thats it! Hopefully this will be helpful to somebody, somewhere! If you found this walkthrough helpful in any way, please feel free to share it with others (or conisder donating 25¢ for my next cup of coffee by clicking here:
- it helps to keep me awake cranking these tutorials out!)

Some References

Help support the site! Or don’t!
I’m not supporting my (growing) family or anything from this website. Seriously.
There is only one reason I am writing these tutorials and posts:
I love doing it.
Technically there is a second reason: to give back to the community. Others before me were instrumental in helping me learn things when I first got started, and I’m hoping to pay it forward here.

If you want to visit an ad, or make a donation, or even link/share my content, I would be absolutely grateful (and tickled pink). If you don’t it’s not going to affect me writing and posting here one bit.

I’ll keep writing, and I’ll keep it free.
If you get any use out of this site, I only ask that you do one thing:
pay it forward.


  1. Very good article, I propose to translate it in French if you agree. I'll also provide a droplet for this purpose, just updating the one provide by Hugin "enfuse_align_droplet". I could send it to you if needed.

    1. Absolutely, that would be fantastic! :)

  2. Hi, this is a very great tool, I got here trough the article on reducing the image noise by stacking photos But I'm having a bit of a problem when running the align_image_stack command, i get the error:
    No feature point
    Bad params
    then it asks to run -p debug.pto which give me that i have "0 control and unconnected images ".
    I'm new Hugins and Enblend and to this kind of tool that runs trough the command prompt. What seems to be the error?

    1. That's odd.

      First, are you quite sure you are running the command as indicated above?

      align_image_stack -m -a OUT FILE1 FILE2 FILE3

      Second, are the images actually a focus stack of the same thing? Sometimes the problem may be that the algorithm cannot find control points common between two or more images in order to align them (which can happen occasionally).

      Third, if needed you can also manually align the image in the full-blown Hugin interface. This way you can visually confirm that the control points are correct, and remove/adjust them as needed. You can still output a set of re-mapped .tif files to continue with Enfuse from there.

  3. Patrick,

    Thank you for all the useful information. I installed and tried to align pictures with Hugin, and by using your command line got good results, which I was able to convert to blend with the stand-alone version of EnfuseGUI. Heureka, my first HDR!

    Depending on your file organisation, the entry of the aligned images as a command line can become complicated for people like me. Is there a possibility with Hugin to enter the images for aligning by drag and drop with the Hugin user interface?

    Thanks and Regards, Herbert

  4. Patrick,

    I drag- and dropped a stack of 3 unaligned images to the Hugin-window and ran "create panorama". Got a nice HDR output image, but with modified proportions and rectified angles - not the original image.

    Hugin does definitely include all nescessary software to both align and blend multiple images. It would be nice to get this working as a HDR creator.


    1. Herbert,

      Once upon a time this was really the only easy way to generate an HDR... :)

      For an HDR, I'll often set the control points manually, and if you're familiar enough with Hugin you can tell it to not do some adjustments to the final output (I should write a tutorial for this at some point). Output to an EXR or HDR is trivial once the alignment is done.

      However, for creating HDR images, I've personally found LuminanceHDR much more handy (particularly if the image are reasonably aligned in the first place). This will let you load up multiple images, align them (using Hugins align_image_stack if you want), create the HDR, and tonemap. All in a single workflow. Might want to check it out! :)

  5. Thanks for this useful article Patrick.

    On my computer (Win 8), the C:\Program Files\Hugin\bin\align_image_stack -m -a OUT FILE1 FILE2 FILE3 didn't work (error message in the command prompt).

    After many attempts i solved the problem by using this corrected command line :
    "C:\Program Files\Hugin\bin\align_image_stack.exe" -m -a OUT FILE1.tif FILE2.tif FILE3.tif (with the quotes marks and the files's extensions).

    If it can be usefull for someone else ;o)


  6. Very useful article - got me up to speed very quickly.

    I've been trying to focus stack 24 tif files. I can do this to preview in EnthuseGUI but run out of memory very quickly when stacking the real thing in the GUI.

    No bother, go to the command line. At least I don't run out of memory but the resulting image is nothing like the GUI preview-too soft.

    The only difference I can spot in the settings between GUI and CLI is the bracket count. ie when I have bracket count in GUI set to anything other than 24 the image is very soft. I can't see any option for setting the bracket count in the GUI. Any thoughts? Am I looking in the wrong direction?

  7. Good article. This was useful. But how do you make the stack? The proprietary Helicon Remote uses a point and click GUI on a tethered camera to electronically twist the focus ring on the camera. The other approach is to use a focusing rail with a manual micrometer-like twist dial adjust or an electronic stepper motor adjustment. I have the proprietary StackShot rail, wich is expensive. But I like it a lot. How did you make the sequence? Is there an open source way? That is reliable? How did you get the tif files? Shoot in raw mode and convert with ImageMagick?

    Your public work is a great resource. When ever I see something from Patrick David I read it. Thank you.

    1. Hi Sandy!

      I actually manually moved my camera through the focus plane for these tests. If I were more into macro shooting in general I would probably invest in a focusing rail (vs. refocusing - to avoid 'breathing' problems).

      Thanks for the kind words! :D

  8. Great article, and worked like a charm on my set of images. Open source software is amazing :)

  9. Hello - I am using your approach for a project with telescopic applications. There is a lot of turbulence in magnified images. Enfuse seems very promising. I've created a public Facebook video (crediting you, of course!) and showing how fragmentary "seeing" conditions that plague magnified images taken at relatively high frame rates can be addressed this way.

    Here is a link to the video. Experiment 7 uses a clean image in which intentional smearing was applied to the five frames (labeled A-E) - the resolution doesn't really do the process justice but it does show that a clean image can be produced from fragmentary blurred images. Again, thanks for your concise article.

  10. You just made that look so complicated I don't think I will go that route.

  11. Thanks for this tutorial. Using a tripod and focusing on different parts of my subject I got quite severe lens breathing problems because of the close focusing. I found that using autopano-SIFT-C in Hugin to generate the control points, combined with assigning a different lens to each image of my stack and optimizing only for Horizontal Field of View using the custom optimization gave me much better results.