A complete guide to automatic focus stacking with CHDK and a Canon A2400

Focus stacking is pretty awesome, and perfect to take your model photos to the next level. The only problem is that it's pretty slow and time consuming, or expensive, or both. Luckily there is a way to make it quick, easy, painless... and it's cheap. What follows is my approach to focus stacking, built around a bunch of open source/free software.

1: Get a compatible camera

First step is to acquire a compatible camera. Take a look at the Supported Hardware sidebar on the CHDK home page and select a camera. I chose a Canon a2400, which is a nice new, extra small point-and-shoot camera. It has optical image stabilisation which is nice, but most importantly it's small and has a good macro mode. I managed to pick one up brand new for under $NZ100, delivered to my door.

2: Set up CHDK

Using Stick, we can pretty quickly set up CHDK for your camera.

Pretty easy!

3: Install my focus stacking script

To make things nice and quick, we'll use a script I developed to automate the process. Copy the following code to a file called dof.bas in the CHDK/SCRIPTS folder on your SD card.

@title DOF stackmaker
rem Written by Michael Adams, released as public domain code.
rem http://www.utrainia.com/87-a-complete-guide-to-automatic-focus-stacking-with-chdk-and-a-canon-a2400

@param a end dist (x1)
@default a 0
@range a 0 10
@param b end dist (x10)
@default b 0
@range b 0 10
@param c end dist (x100)
@default c 2
@range c 0 10
@param d shoot 1x infinity
@default d 0
@range d 0 1

get_zoom z
if z>0 then gosub "nozoom"

let s = get_focus
let e = 100*c+10*b+a
if e < s then gosub "bad_focus_dist"
print "Starting from: ", s
print "Ending at: ", e
sleep 1000

gosub "countdown"

rem start at 2mm steps, increase as focusing further back as DoF changes as we move focus point
let i = 2

rem starting focus distance
let f = s
let n = 1

	print "Number: ", n
	print "Focus: ", f
	set_focus f
	rem finished shooting
	if f > e then goto "exitloop"
	rem calculate the focus. take more shots at close end, step more at larger distances
	if f >= 500 then let i = 20
	if f < 500 then let i = 18
	if f < 400 then let i = 17
	if f < 350 then let i = 16
	if f < 300 then let i = 15
	if f < 280 then let i = 14
	if f < 260 then let i = 13
	if f < 240 then let i = 12
	if f < 220 then let i = 11
	if f < 200 then let i = 10
	if f < 180 then let i = 9
	if f < 160 then let i = 8
	if f < 140 then let i = 7
	if f < 120 then let i = 6
	if f < 100 then let i = 5
	if f < 80 then let i = 3
	if f < 60 then let i = 2
	let f = f + i
	let n = n + 1
	goto "mainloop"

	rem shoot a shot at infinity?
	if d = 1 then gosub "take_inf_shot"

	print "Taking photo at infinity"
	set_focus 65000

for n=1 to 4
    print "starting in:", 6-n,"secs..."
    sleep 940
next n
for n=1 to 4
    print " "
next n

rem Focus nearer than 5cm requires widest zoom (7.3mm focal length)
rem (I chose not to, but code could override using set_zoom 0)
print "***TO USE THIS SCRIPT***"
print "You MUST set camera"
print "to widest zoom"
print "then RESTART script."
goto "theend"

print "***BAD FOCUS***"
print "End dist & end dist are wrong"
print "Starting from: ", s
print "Ending at: ", e

4: Take some photos

Time to take some photos!

Now just stand back and wait. It'll take a few minutes to click off 50 or so shots.

5: Stack some photos

Time to make some magic.

All done!

It goes without saying that all the shots on this page are done using CHDK and my script. You'll see that some things work better than others; you need lots of light, and reflections/highlights can be difficult. But when it all comes together, the results are great! No go forth and stack some photos of your own...

Newer Older


Saturday, Mar 29 2014, 2:43 PM AK says...
Hi again,<br /> Thank you for this write up.<br /> I tried out this with mine and it works... Yay..!!!<br /> Tried a couple of stacking photos with your script and got pretty good results. But the script works well. I just need to get the lighting right.<br /> Now I know what I did wrong. I had some trouble understanding how to set the camera to Manual focus but figured it out after some tries.<br /> <br /> Thanks again for this write up. Keep up the good work... :)