Focus-Bracketing Scripts for "Macro" Setting and for Closeup Lenses
Abstract: Focus-bracketing for "Macro" setting and for closeup lenses is discussed, as well as the relative merits of using macro-rails or adjusting the camera's focus distance. In order to perform the latter technique automatically, the script brack.bas is cited for the macro bracketing and a new script close.bas is provided for closeup-lens bracketing. The results are demonstrated with sample images.
Why Focus-Stack Macro Shots?
For my SX30 on the "Macro" setting, the DOF drops to 1 mm within a few centimetres in front of the lens at f/2.7 (or around 1 cm at f/8.0). The magnification for macro is greatest near the lens and drops off fairly rapidly with distance so, if you want to maximize detail, the distance should be as small as possible (touching the lens for the SX30). If the DOF is only 1 mm for large magnification, but you want more, the macro images have to be focus-stacked.
Methods of Focus-Bracketing
Focus-bracketing may be performed either by the use of macro-rails or by adjusting the camera's focus distance between shots.
Macro Rails: Inexpensive macro-rails normally have a millimetre scale, which is adequate for bracketing macro shots, but not precise enough for bracketing closeup-lens shots. These inexpensive macro-rails can be modified, however, for use with closeup lenses ( http://www.dpreview.com/forums/post/51484745 ). A potential problem with macro-rails is that, if you try to cover a large range of focus distances, the perspective changes, resulting in an imperfect stacked image. The problem is most noticeable for macro close to the lens and for strong closeup lenses. Macro-rails are normally adjusted manually between shots but, with some practice, this can be done in a few seconds (which is fun). Some enthusiasts have automated their macro rails with computer interfaces and stepping-motors.
Adjusting the Camera's Focus Distance: This method has the advantages that you don't have to carry macro-rails with you, just a tripod; there is no perspective change to worry about; and the shots can be taken automatically (which is fun) by use of a CHDK script ( http://chdk.wikia.com/wiki/CHDK ). A potential disadvantage is a limited range of focus distances for strong closeup lenses. Closeup lenses give the most magnification when the camera is at full zoom with the focus distance set to infinity. As the camera's focus distance is decreased, at any zoom level, the magnification and focus distance of the closeup lens also decrease. On the Canon SX30, the closest camera-focus distance at full zoom is 1.4 m and this also limits the closest focus distance with a closeup lens. For the Raynox 250, the working distance is 121 mm with the camera focus set to infinity and 111 mm with the camera focus set to its minimum value of 1.4 . This means that the focus range is only 10 mm. But "only 10 mm" is actually not much of a limitations. It is rare that more range would be required at full zoom. For full zoom at f/5.8, the DOF for the Raynox 250 is only slightly larger than 0.1 mm so, in using the whole focus range of 10 mm, you would have to stack 101 bracketed shots 0.1 mm apart.
There is an illustrated discussion of the relative merits of focusing by rail and by camera-focus adjustment on the Zerene Stacker website ( http://zerenesystems.com/cms/stacker/docs/troubleshooting/ringversusrail ). They conclude that, "It should be clear in each case that the focus-by-ring stack gives a much better result than focus-by-rail. The by-rail result has big halos and looks smeared; the by-ring result has only some small halos."
The following test images do not have artistic or technical merit. They were made hastily to test the scripts brack.bas and close.bas under the extreme conditions of of small subject size (2 mm spirea bud), largest aperture (smallest f-number for the least DOF) and maximum magnification. For macro, largest magnification means the closest distance, which, for the Canon SX30, is touching the lens. For the closeup lenses, maximum magnification means full optical zoom. The same subject was used for all test images. Click on the images to get a more detailed view and close the tab to return to the article.
Single Macro Shot (Image 1)
|Image 1: Macro at f/2.7 - Single Shot - Spirea Bud Touching the Lens|
Somebody should clean his camera lens once in a while. Even though the subject is very small, not much of it is in focus because the combination of large aperture (f/2.7) and close distance (touching the lens) means that the depth-of-field (DOF) is small. Image 1 is the first shot of the stack used to make Image 2. (For the Canon SX30, Macro is at f=4.3 mm.)
Stacked Macro Image Using the Focus-Bracketing Script brack.bas (Image 2)
|Image 2: Macro at f/2.7 - 22 shots 1 mm apart, focus-bracketed by brack.bas|
The focus-bracketing script brack.bas was written for CHDK ( Some Technical Aspects of Stacking Macro with Canon SX-Series Cameras, by Stephen Barrett ( http://chdk.wikia.com/wiki/Technical_Aspects_of_Stacking_Macro_Images ). The script begins taking shots wherever the manual focus is set and then proceeds away from the camera, taking a specified number of shots that are separated by a specified amount. (The bracketing can go towards the camera if the user specifies a negative step size. The shots of Image 2 were focus-stacked using Zerene Stacker ( http://www.zerenesystems.com/ ) (For the Canon SX30 Macro is at f=4.3 mm.)
Stacked Raynox 150 Image Using the Focus-Bracketing Script close.bas (Image 3)
|Image 3: Raynox 150 with Camera at f/5.8 - 58 shots 0.2 mm apart. focus-bracketed by close.bas|
The shots for Image 3 were taken at the full optical zoom (f = 150.5 mm) of the SX30 using the script close.bas. Some halos were removed in the black regions but others remain, such as around the stem in the foreground. This is not a very good image, with its halo problems, but it demonstrates that the script works. One cause of halos can be that small reflective portions of the subject may move, even indoors, because of air currents or, in the case of flowers with feeble stems, because of sagging. The light source was a single unshaded bulb, which is probably the main source of my problems. A more even source would help to reduce halos.
Stacked Raynox 250 Image Using the Focus-Bracketing Script close.bas (Image 4)
|Image 4: Raynox 250 with Camera at f/5.8 - 42 shots 0.1 mm apart, focus-bracketed by close.bas|
The shots for Image 4 were taken at the full optical zoom (f = 150.5 mm) of the SX30 using the script close.bas. As in Image 3, some halos were removed in the black region. All of the stacking was performed with Zerene Stacker.
At full optical zoom, I find that the Raynox 250 gives a lot of detail but there are bothersome artifacts and noise. This may be a result of improper lighting. My experience has been that I often get more pleasing results with closeup lenses if I do not go to maximum zoom:
Stacked Raynox 150 Image at 1/3 of Full Zoom - Outdoors Without Tripod (Image 5):
|Image 5: Strawberry - SX30 at 47.1 mm with Raynox 150 - 30 shots 0.5 mm apart, focus-bracketed by close.bas|
The strawberry was shot outside without a tripod. I laid the camera on a board on the grass and loaded the script close.bas. After sliding the camera around to focus a little behind the strawberry, I pressed the shutter and let the script do its work. No processing was done to remove halos. There is some halo on the horizontal blade of grass behind the strawberry, probably because of slight wind movement.
Stacked 600 mm Closeup Image at Full Zoom with Halos (Image 6) and Without (Image 7):
|Image 6: Herb Robert with Halos - SX30 at 150.5 mm with 600 mm homemade closeup lens - 20 shots 1 mm apart, focus-bracketed by close.bas|
|Image 7: Herb Robert - Halos cloned away- SX30 at 150.5 mm with 600 mm homemade closeup lens - 20 shots 1 mm apart, focus-bracketed by close.bas|
This article demonstrates that the scripts brack.bas (for macro focus-bracketing) and close.bas (for closeup-lens focus-bracketing) work under extreme conditions. There are other bracketing scripts that work well for macro but I have not found any others that work for closeup lenses, although I did come across people who were looking for such a script. Documentation for close.bas is in Appendix 1 and the script itself is in Appendix 2. I hope that someone finds it useful.
I would like to thank waterwingz for the following improvements that he made to the script:
1) adding exposure lock to ensure that all shots have the same exposure and
2) expanding the text console to make it easier to read.
APPENDIX 1: Documentation of close.bas
The script close.bas is for focus bracketing with closeup lenses.
It was written for my Canon SX30, but it should work without modification on other SX-series cameras.
The following closeup lenses are included:
- Raynox 250
- Raynox 150
- 600mm (Homemade from a cheap telescope objective:
- (See "Modifying the Script to Work for Other Closeup Lenses" below.)
The following definitions are used in in this documentation:
- Focus distance d (mm) can be thought of as the distance of the focus point on the subject from the "centre" of the lens. This value is used in calculations.
- Working Distance h (mm) is the distance of the focus point on the subject from the front of the lens housing.
- Offset k (mm) is the difference between Focus Distance and Working Distance.
- Focus Range is the distance between maximum and minimum working distances. The minimum working distance is limited by the camera's minimum value of focus distance S.D., which, for the SX30 is 1.4 m at full zoom.
The Working distance h is given by:
h = d - k (1)
The measured focal lengths (parallax method) of the Raynox lenses were slightly larger than those calculated from their nominal diopter ratings (Raynox 250: 8 diopters gives f = 125 mm; Raynox 150: 4.8 diopters gives f = 208.3 mm).
Table 1: Measured Closeup Lens Properties
|Raynox 250||127||6||121||10 ( 7)|
|Raynox 150||214||6||208||28 ( 20)|
*1: Maximum working distance is hmax = f - k, which occurs at infinite camera focus.
*2: For the Canon SX30, the maximum range is calculated for full zoom ( with S.D. = inf to 1.4 m) and the minimum range for less zoom (with S.D. = inf to 2.0 m). For other cameras, the focus range may be calculated from: range = (f^2)/(f + 1000 S.D.min) , where S.D. is the minimum value of the camera's focus distance in metres for any given camera-focal-length and f is the focal length of the closeup lens in mm.
In CHDK scripts, the focus distance can be set by using the function set_focus. The command
sets the camera's focus to a distance g (mm). The camera gives subject distance on a bar scale and, for the Canon SX30, this is the same value as subject distance S.D. from CHDK's DOF calculator, except that the latter is more precise (in metres to 3 decimal places):
g = 1000 x S.D. (2)
When a closeup lens is added to the camera, the subject distance bar on the camera and the S.D. value provided by CHDK's DOF calculator do not provide information that is readily useful. In order to write a focus-bracketing script for a closeup lens it is necessary to find a relationship between the actual focus distance d and the camera's focus distance g. A series of focus tests with each closeup lens on my Canon SX30, using the CHDK DOF calculator, provided values of working distance h and subject distance S.D. Using equations 1 and 2, yields d and g. These values were found to be in excellent agreement with the following familiar-looking relationship:
1/d = 1/f + 1/g (3a)
g = fd/(f-d) (3b)
where the values of focal length f are give in Table 1.
Distances Smaller than a Millimetre
With the Raynox lenses on cameras at high zoom, the depth-of-field (DOF) is often a few tenths of a millimetre. This means that focus distances d and working distances h must be specified to a tenth of a millimetre. The problem is that the version of uBasic in CHDK does not recognize non-integer values. For example, values such as 6.3 mm get truncated to 6 mm. The non-integer values also cannot be printed to the camera's display. The solution to this problem was to perform calculations with f and d expressed in units of (tenths of millimetres). For use as the argument of the set_focus command, g must be computed in mm, but its truncation does not cause significant inaccuracy or imprecision of d and h. So Equation 3b becomes:
g = [ fd/(f-d) ] / 10 (3c)
where f and d are in the units of (tenths of millimetres) and g is in millimetres.
Working distances h are printed to the camera display as, for example: 1165 /10 mm (meaning 116.5 mm).
Setup and Operation
The maximum focus distance (and magnification) occurs when the camera focus is set to infinity. During setup for the series of shots, manually set the camera focus to infinity and focus at or beyond the back of the subject (not in <Alt> mode). Check that the number of steps is high enough to give the required focus range and that this range does not exceed the value in Table 1. After setup, enter <Alt> mode by pressing the shortcut button and press the shutter to start the script. The script will automatically begin bracketing at the maximum focus distance and progress towards the camera. It is useful to have the CHDK's DOF calculator running in order to check that S.D. is decreasing with each shot, as it should.
Step Sizes at Full Zoom
For my SX30 at full zoom and f/5.8, I use the following step sizes:
Table 2: Step Sizes at Full Zoom of SX30 at f/5.8
DOF increases at larger f-numbers and so step size may sometimes be increased also, depending on which closeup lens is used. These values should be acceptable for the SX40 and SX50 as well.
Step Sizes When Not at Full Zoom
The required step size is approximately inversely proportional to the camera focal length. For example, SX50 full zoom is 1200 mm effective focal length, so for 600 mm on the camera barrel, step size is 0.2 mm for the Raynox 250 and 0.4 mm for the Raynox 150. Note that the available focus range changes with camera zoom.
Modifying the Script to Work for Other Closeup Lenses
It is a simple matter to change the script to work for other closeup lenses, even if you know nothing about scripting or uBasic. For any of the values of r in the script, simply change the "f= " and "k= " lines near the beginning of the script. If you know the focal length in mm, multiply that number by 10 and put it after "f= ". [f in mm is equal to 1000/(diopter value).] Also replace the "k= " value with your measured offset. A good approximation is obtained by measuring the distance from the centre of the closeup lens to the front of it's housing. Multiply that by 10 and enter it after "k= ". If you experience that the working distances that are printed to the camera display are not quite right, k can be adjusted later. The script will work just fine even if k is wrong.
APPENDIX 2: Focus Bracketing Script close.bas for Closeup Lenses
rem Author: Stephen Barrett (June 26/13)
rem Thanks to waterwingz for the following improvements (June 30/13):
rem 1) adding exposure lock to ensure that all shots have the same exposure
rem 2) expanding the text console to make it easier to read.
rem close.bas is for focus-bracketing with closeup lenses.
rem The Raynos 250, Raynox 150 and a 600 mm homemade lens are included here
rem but any other closeup lens may be accommodated simply by altering "f" and "k" values.
rem It was written for the Canon SX30, but it should work without modification on
rem other SX-series cameras.
@param r 1=Ray250, 2=Ray150
@default r 1
rem 3=600 mm closeup lens
rem Measured Focal Lengths of Closeup Lenses (in units of mm/10)
if r=1 then let f=1270
if r=2 then let f=2140
if r=3 then let f=6000
rem Offsets for Working Distances (in units of mm/10)
if r=1 then let k=60
if r=2 then let k=60
if r=3 then let k=200
@param a Tens of steps
@default a 0
@param b add unit steps
@default b 1
rem s=10*a+b is number of steps Number of shots is s + 1
rem e.g. a=5 & b=3 means s = 53 steps 1 initial shot + 53 steps = 54 shots
@param u Step size mm
@default u 0
@param v add tenths of mm
@default v 1
rem t=10*u+v is step size in (tenths of a mm)
rem e.g. u=3 & v=7 means t = 37 tenths of a mm (=3.7 mm)
rem [to get around ubasic problem: it truncates 3.7 down to 3]
set_console_layout 1 3 44 8
rem Step 0: shot at infinite focus
rem h is working distance
print "Shoot infinity", "(", h, " mm)"
for i=1 to s
rem h is Working Distance approximate distance (tenths of mm) from front of closeup-lens housing
d= f - i*t
h = d-k
print "Step", i, "(", h, "/10 mm)"
rem set focus in mm (g = 1000 * S.D.)
The views and opinions expressed in this article are those of the author and do not necessarily reflect the views and opinions held by dpreview.com or any affiliated companies.