2009/01/19

How to obtain a greater depth of field in macro photography

Este artículo también está distponible en Español: Como aumentar la profundidad de campo en fotografía macro.

One of the most common problems in macro photography is the small depth of field: obtaining a photography where all our scene is focused is not an easy task; when closing the diaphragm is not enough (or convenient), we have to make use of other techniques.

The problem

The depth of field of a photography is the part of the scene which is on focus: everything behind or in front of that zone is out of focus. The size of this zone depends on three factors:

  • The focal length of our lens: a longer focal length will produce a shallower depth of field.
  • The diaphragm: an open-wide diaphragm will result in a shallow depth of field, too.
  • The distance between the camera and the subject: again, a short distance means an small depth of field.

In macro photography, the distance between the camera and the subject is very small: frequently, between the front of our lens and the insect or the flower that we are trying to photograph there are hardly a few centimeters, sometimes even less. And the lenses used do not have specially short focal lengths, usually 60 or 100 millimeters.

Under these circumstances, the depth of field becomes as shallow as a few millimeters, clearly not enough for most scenes. Then, all we can do is close the diaphragm as much as possible, but sometimes even this is not enough. We also have to take into account the consequences of light diffraction: when the diaphragm is very small, there is a significant loss of image quality.

The technique

The technique that I will explain in this article is called "focus stacking". Basically, what we are going to do is take several photographs, changing the focusing point on each shot, and then merge them with our computer.

With this technique, we must always use a tripod, because the photographs must be perfectly aligned; I do not recommend this technique to shot insects and other moving subjects. Our camera must be placed in a position such that we can focus every part of our scene moving just the focusing ring, without displacing our camera; focusing rails are not recommended, too.

We will begin focusing on the far extreme of our scene and taking a picture; then we have to move the focusing ring very slowly, as we keep taking photos. Sometimes, two or tree photographs will do; but other scenes require ten, twenty, or even more photographs.

Even if we used a tripod, and we are completely sure that there was absolutely no movement between photographs, changing the focusing distance produces subtle differences between the images, big enough to ruin the final result. After we have downloaded the files to the computer, we must align the photographs. This step will be performed using align_image_stack, one of the tools included in Hugin. Assuming we have all our photographs in the same directory, all of them as TIFF files, we execute the command:

align_image_stack -m -a AIS_ IMG_????.tif

I suppose we could cut-and-copy the files, to select by hand the focused parts from each image, but that could be very time-consuming and boring. Fortunately, we can do this automatically using Enfuse (also included in Hugin):

enfuse -o result.tif --wExposure=0 --wSaturation=0 --wContrast=1 --HardMask AIS_????.tif

There is also a --ContrastWindowSize parameter to control the quality of the final image: the default value is 5, but it can be increased to 7 or 9, to obtain a better result; however, the time required for the process will increase, too.

An example

I wanted to take a detailed photography of this orchid we had at home:

The flower is about five centimeters wide, and there are almost two centimeters between the front and the back of that flower: far too depth for a conventional macro photography. What I did was take 20 different photographs, as described above; even though I stopped down the diaphragm to f/16, the depth of field was really narrow:

Using the focus-stacking technique, I obtained this final picture, perfectly focused from front to back:

Not the prettiest picture of an orchid, perhaps; but I think is serves the purpose of illustrating this technique quite well.

18 comments :

Anonymous said...

You are misusing the word "swallow." What you mean is "shallow." Please fix, as you are using it repeatedly.

eduperez said...

@Anonymous: Many thanks, it has been fixed now.

Anonymous said...

1. In your align_image_stack command line example, I think you mean:

align_image_stack -a AIS_

The -a option specifies a prefix for the output files. According to the documentation:

"-p file
Output .pto file (Useful for debugging, or further refinement)"

2. On Windows at least, the form "IMG_????.tif" for the input files doesn't work, nor does "*.tif" or any other wildcard I have tried. It gives an error saying that at least two files must be specified. I found that I need to list each of the input files explicitly, giving the full (path and) name. e.g.

align_image_stack -a out_ DSCN0004.jpg DSCN0005.jpg DSCN0006.jpg

3. There is another enfuse command line option that may also be useful:

--ContrastWindowSize=n

I have found a setting in the range 5-7 works better than leaving it to default. The downside is that the bigger you set it, the longer enfuse takes.

Stuart

eduperez said...

@stuart:

1. Thanks, I have corrected the typo.

2. It is strange... Windows uses the same wildcard characters, it should work (you surely tried with DSCN????.jpg or *.jpg, did't you?).

3. Thanks again, the default value was "good enough" for me, but I will give it a try.

Anonymous said...

2. Yes I tried the various wildcard combinations you mention, but I get the error about requiring a min of two files. Listing the file names in full works round this OK.

I am interested in photographing features of insects through a microscope to illustrate identification keys. The problem here is that the depth of field is minimal - a single image gives you an "optical section". So I take a series of pictures at different focal depths and blend them into an enhanced depth of field mosaic.

One consequence of the method is that, as you rack the microscope up and down to take the shots, you inevitably change the magnification slightly for each one.

align_image_stack has a command line switch, -m, which is useful in correcting for this. According to the manual:

-m
Optimizes field of view for all images, except the first. Useful for aligning focus stacks with slightly different magnification

Stuart

eduperez said...

@stuart:

I have also experienced that magnification problem you mention. I could barely see it, but it is definitively there. The "-m" parameter solved the issue completely.

I also made some experiments whit the --ContrastWindowSize parameter: there is a visible enhancement when raised to 7 or 9. I think this is very interesting, even if it increases the time required to process the images.

Thank you very much for those tips, I'll update this article as soon as possible.

Art said...

I have had better luck using (inexpensive) focusing rails to step through the shots. Camera disruption seems to be less for me. I obtained a low cost pair (left/right & forward/back) from a Hong-kong supplier quite cheaply.

eduperez said...

@Art:

Thanks for the tip! Where did you found those rails (cheapest I can find is 60$~70$ at DealExtreme)?

Anonymous said...

I just wanted to say thank you for this tutorial! I found it very useful.

GreyDad said...

Thanks for the tutorial!

I am having a problem with blending a series of four images of a brightly colored neckless of large glass beads. The original images and the aligned images look fine except for the focus; they are in tif format. When I combine them using enfuse the resulting image is very blurry especially the silver chain. I have tried various combinations of the enfuse parameters to no avail.

If you would like to see the images, send me a reply to my gmail account, greydad@gmail.com, and I will send them to you in jpg format (compressed for easier emailing)

Regards,
Frank Goss

eduperez said...

@GreyDad:

Yes, of course: I will have a look at your files; I just sent you an email.

dnebdal said...

There's a good reason the wildcards don't work in windows, btw: In *nix, wildcards are expended by the shell, so when the program looks at its arguments, it sees "program -option file1 file2 file3". In windows, wildcards are passed to the program, so it would see "program -option file*".

The linux way has some obvious benefits (it makes wildcard support completely transparent, for one thing), though the windows/DOS way allows tricks like "mv *.doc *.txt" to rename all .doc files to .txt - both have their benefits.

Richard said...

@eduperez

Is the same offer you extended to GrayDad, to take a look at some images he was having trouble with, still valid? If so, let me know because I could use some assistance. My e-mail is MSteller74@yahoo.com

eduperez said...

@Richard:

Yes, of course: the offer is still valid; you have new email.

hamiltonkieran said...

Thanks so much for this tutorial, I've been having lots of problems with this, didn't realise there was software that could fix it, cheers!

Anonymous said...

I like your block!
so here is a little correction:
your enfuse command is a little depricated,
now its: enfuse -o result.tiff --exposure-weight=0 --saturation-weight=0 --contrast-weight=1 --hard-mask AIS_????.tif

cheers

Anonymous said...

Please video tutorial :) thank you

I do not know much English :D


I am from Czech Republic

joanarling said...

Thanks for the tutorial! As a side note, enfuse also does HDR (exposure-weight=1).

Joan

Post a Comment