Focus-stacking with Raspberry Pi for macro photography

Dave Hunt is on a bit of a roll at the moment. Not content with having engineered the water droplet photography setup behind the prettiest post we’ve featured here, he’s also been working with the Pi and an home-made macro rail for sharper macro photographs without all that woolly depth of field. Bokeh – the fuzzy blur from the out-of-focus parts of a picture – is an effect that can be really beautiful, but sometimes you want a sharper picture, which can be nigh-on impossible in macro photography without special equipment.

Dougal, this cow is small. Those ones are far away.

There’s a way professional photographers deal with this, but, of course, it’s expensive. You can buy a rig which allows you to take many images, each taken a little closer to the object, so different parts of it are in focus with each picture. You can then combine or stack all those images in software, as in the cow picture on the right. There’s an open software solution to the matching and stacking problem called CombineZ (somebody port this thing to the Pi; that GPU is built for just this sort of application), but if you want to buy a rail that automates the moving of your camera, things suddenly start to look expensive. Dave says commercial solutions come in at around $600.

Enter the $35 Raspberry Pi and an old flat-bed scanner from the loft.

If you want to build your own focus-stacking rail, Dave has full build instructions, including circuit diagrams, code and tips on where to get parts at his website; even if you’re not a seasoned electronics hacker, you should be able to follow his very clear instructions if you want to make your own. Thank you Dave, for another great money-saving photography project and a fantastic writeup. We like your cow.

24 comments

Avatar

Good One Ted!!!

Avatar

Dave, you rock!
You have so many good ideas and a good pace, it’s hard to keep up with you – I’m still stuck with the Camera Pi project (extending it with a web interface for libgphoto, so I can use my smartphone as a remote). Keep coming those brilliant ideas!
Thomas

Avatar

Best Father Ted reference ever :)

Avatar

Agreed, just spotted that now! LOL!

Avatar

Father Ted was awesome ….. Shall I boil the kettle again Ted ? ….. No I liked it better the first time …. Inspired ! …oh nice Photos …

Avatar

Isn’t the Pi a bit of an over kill for this? An Arduino would be a more suitable way.

Avatar

Well, if you don’t have an arduino, you have to order one, wait for it to arrive, plug it in to your PC, program it and hey presto..

Or you could use the Raspi you already have.

Point being, you can use whatever you like, but this is the Raspi website, so its fair to expect projects done with Raspi’s, even if they are not the most suitable for the job.

Avatar

Yes, just like dave’s last project, but that’s not the point.
Texy

Avatar

Sweet! Good use of an old scanner.

This will not work with a Nikon camera since you also have to connect the focus pin while shorting the trigger. It’s also common practice to use an opto coupler because you can now easily fry your camera.

Avatar

Yes it will work with a Nikon – you just have to short both the focus and the shutter button wires together. An opto coupler is the usual method for triggering a flash due to the potential high voltages on certain flash units, but is overkill for camera triggering.
Texy

Avatar

This guy needs to stop making stuff. My wife has already asked me to make his water drop rig. I guess I can just hope she dosn’t see this one.

Avatar

Lucky you!

Avatar

mmmm I think I will have to keep an eye out for an old flatbed scanner.

Avatar

This is pretty cool.

This is a good project fo the Pi.. but just because I am a pedantic jerk some times… it should be that difficult to get a deeper depth of field. Just put the camera in aperture priority mode and select the biggest f number (like f22) which is to say smallest aperture size. Instant deep depth of field / minimal bokeh.

I still love this build and I will be probably starting it myself sooner rather than later.. was looking for something clever to do with my Pi.

Avatar

The depth of field is greatly reduced with this level of magnification, hence the reason why mutliple shots at differing focus points and stacking is necessary.
Texy

Avatar

It doesn’t sound like you’ve done a great deal of top-notch macro photography Imran. Even at f/22, the depth of field at <30 cm from the lens (which is where you need to be to photograph small things) is less than a couple of mm.

Avatar

It also depends on the angle of the lens. With a very wide angle lens it would be possible to get sufficient depth of field at f22.

Avatar

Depends what you regard as sufficient really.

Avatar

With a 28mm lens (very normal wide angle lens), at 30cm you have a dof of 15 cm. With a 24mm, you get a dof of 22cm.

Anyway… more than the image you can see. And at least, enough for the little cow we can see there.

Avatar

Wld the lytro be able to output a all focused shot from its raw data?
The data is all there right?

Avatar

Reminds me of the light-field cameras. Nice idea though, and gives me a good excuse not to throw out that old scanner in the loft…although my camera is lacking an electronic shutter release, so might have to look at other options.

Avatar

If your camera has usb, and is compatible with gphoto2, then you may be able to control it via the usb port.

Texy

Avatar

What you need to understand is that when a lens is focused to give a life size image you lose two stops. This means that a marked f22 on the lens is in effect f45 and serious diffraction problems would be apparent long before such a small stop was reached.
Focus stacking enables the sweet spot in the aperture scale of a lens to be used which is usually about two stops less than maximum aperture. That is an f2.8 lens would be best about f5.6 resulting in a very shallow depth of field indeed.

Avatar

I do focus stacking with an MPE-65mm macro lens. It has a magnification of up to 5X. At 5X and F/16, the (theoretical) depth of field is 0.2 mm. You’ll certainly need to stack. Plus at F/16, there will be no detail in the shots and you’d need the batman spotlight to light it up so the stack will be ruined. With stacking, I can shoot at lower f-stop and get amazing detail. Here are some of my stacks, http://scienceinquirer.wikispaces.com/microscope

It is impossible, I repeat, impossible to get this kind of results by adjusting the aperture. I use a stackshot and my stacks are between 30 and 200 images stacked with Zerene Stacker software.

Mike

Leave a Comment

Comments are closed