Easy Focus Stacking With Your CNC Machine

Macro photography is the art of taking photos of things very close up, and ideally at great detail. Unfortunately cameras have poor depth of field at close ranges, so to get around this, many use focus stacking techniques. This involves taking many photos at different focal lengths and digitally compositing them together. To help achieve this, [gtoal] realized that garden variety CNC machines would be perfect for the job.

To focus stack effectively, it’s desirable to move the camera in very small increments of sub-mm precision, in order to get different parts of the subject in focus. For this, a CNC machine excels, as it’s designed to move tool heads in very tiny, precise movements.

To achieve a bargain focus stacking rig, [gtoal] used a Dremel tool mount for cutting discs. It’s repurposed here, used as an easy way to fit a Raspberry Pi camera to a CNC tool head through its mounting holes. From there, it’s a simple manner of stepping the CNC a tiny amount at a time on the Z-axis, while taking photos with the Raspberry Pi along the way. [gtoal] notes that it would be simple for an experienced CNC user to whip up a program to automate the entire process.

We’ve seen other budget focus stacking rigs before, and even a busted 3D printer that was turned into an automated scanning microscope. If you’ve got your own tricks for top notch macro photography, drop us a note in the tipline!

15 thoughts on “Easy Focus Stacking With Your CNC Machine

  1. https://pulsar124.fandom.com/wiki/Open_Source_workflow_for_macro_focus_stacking

    A set of BASH scripts and C programs utilizing open source software (dcraw, Hugin, and ImageMagick) for macro photography post-processing (every step of the way – from converting raw files to focus stacking to multi-scale sharpening). It works under Linux (natively) and Windows (using Cygwin). It might work under MacOS?

    This open source workflow has some advantages and disadvantages when compared to existing commercial solutions.

    The advantages:

    In some respects it produces better quality stacked photographs. In particular, the stacked photos have better sharpness (when compared to Zerene Stacker). See e.g. here: https://www.flickr.com/photos/99745838@N03/25077956624. Also, this package handles properly hot pixels, unlike the commercial Adobe Camera Raw product (part of Adobe Lightroom and Photoshop). This removes all hot-pixels related artifacts from stacked photos.
    It is free.
    It is fully scriptable (as it is based on command line tools), which is great for full automatization of macro photography post-processing.

    The disadvantages:

    It is slower.
    It is more memory hungry.
    It can produce artifacts of its own (e.g., halos around bright features are more pronounced).
    It is not for everyone. (You need to have some minimum bash and command line experience to be able to use it).

    1. I guess the memory requirements comes from ImageMagick which has a super strange memory model. It has multiple layers of caches, from CPU to Disk based … and uses all of them as much as possible. I’ve run out of memory trying to create a grid of a few images. There are options to restrict its memory use … but so far I had little success using those.

      Hugin on the other hand is a great tool to have in the toolbelt … from super resolution over panoramas to focus stacking, ‘align_image_stack’ (from the Hugin suite) is just super useful.

  2. Heeey, I’ve done work with my CNC and a camera. Trying to remove layer by layer ( using the endmill ) of lots of things ( ICs, wood, etc ), but playing with focus is pretty cool too!

    1. There’s a lot a people with a pi camera already mounted looking at the bed – and many softwares can already to do time lapse at various bed (layer) heights… Someone feeling ambitious for an octoprint plugin?

  3. With many of the lenses already electronically controllable. if your using many of the Nikon and Cannon offerings. You can use their development API’s to control the focus from software. Allowing you to step your focus from software. Instead of trying to build an external focusing setup. Like you would do with a cinema lens.

    There are several open source projects to that are automated focus controllers for cannon cameras. Which again just plugin. or you can take a macro lens adapter and control the T all the connections and control it before the camera as well.

    Lets face it the most time consuming thing about stacked macros is the setup, calibration and slowly stepping through the focus stack. So any help is big.

    1. One could take a manual focus lens and add a stepper motor and some gearing on the outside. This would have a benefit of being cheaper and easier than reverse-engineering the protocol that operates the modern lens. Some cameras have option for focus stacking in camera, some hackers add their own firmware to do that (for example there is focus stacking app at OpenMemories (https://sony-pmca.appspot.com/apps) for Sony Alpha cameras – it only takes pictures, so processing must be done one the PC)…

      1. The protocol for controlling canon over USB is wide open, and there’s libraries for it. You can set up an arduino to control it easily. I used it to make an electronic follow focus.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.