Monday, November 29, 2010

How to focus a camera

First, if you’re looking for advice on snagging soft focus shots of Kiera Knightly or Cameron Diaz you’re in the wrong place. What I’m going to address here is how to ensure your machine vision system is yielding crisp images.

The challenge I find is in optimizing the lens aperture (f-stop, in photographer speak,) with the focus ring. If you close down the aperture, which makes the image darker, you increase the depth of field. In other words, the image is in focus over a greater working distance range. Sounds like it should help but in my experience, this makes it harder to find the sweet spot of “perfect” focus. Here’s what I do instead.

Start by placing a target at the working distance your system uses. (Working distance = distance from target to front of lens.) A good target is flat and has plenty of contrast and edges. I find something like a business card can work well.

Next, working with a live image (you don’t want to be waiting on triggers,) open up the aperture all the way. This minimizes the depth of field but also makes your image very bright so you’ll need to turn down the exposure time or perhaps even turn off some of the lighting. You need to avoid any saturation or blooming in the image, so nothing should be at a gray level of 255.

Now you can start adjusting the focus ring on the camera. Make sure you can see the biggest possible image on the monitor, and turn the ring slowly. The best focus is achieved when you have the sharpest edges, so you might like to zoom in tight on an edge and watch the pixels as you turn the ring. Also, be sure to concentrate on the center of the image rather than the corners.

As you adjust the focus, remember to go slow enough for the camera to keep up. The higher the image resolution the lower the frame rate. A 5Mp camera might only give 5 frames per second, so if you rush you’ll never see the sweet spot.

Once you’re happy with the image, carefully tighten the locking screw. (If you don’t buy lenses with locking screws, shame on you.) Now you can put the lighting back to where you need it and close down the aperture. The image should now be as well focused as is possible.

Well that’s how I do it. If you think you have a better method please let me know.

Now tomorrow we’ll talk about why you might not want the sharpest possible focus.

1 comment:

Spencer Luster said...

The method you suggest usually works well, particularly when using well-corrected optics designed for flat-field imaging. (Enlarger lenses, printing lenses, etc.) Many lenses, however, don't fall into this category.

There are at least two issues that might make you want to modify the method. The first is field curvature, or lack of flat-field imaging. Working backwards from the image plane (as if imaging the detector surface onto the object surface), the truly best focused surface is not flat, but concave or convex. Very often it is concave from the lens's perspective. Thus, if you use the center of the field of view (FOV) as your focus reference, the edges of the FOV might be more out of focus than optimal. I often choose to use the midpoints between the center of the FOV and the edge of the FOV. In this way neither the center nor the edge of the FOV is too far from optimal focus.

The second issue is variation of the best focus distance with lens f-number. As an opticker's rule of thumb, many compound lenses are designed for best performance around F/8. There are numerous exceptions, but without other knowledge F/8 is a good place to start. Now if the lens is well-designed, especially for good color correction (assuming you are using white light), then the best focus distance at F/8 will be the same as at F/4 or F/16,etc. BUT... many times it's not the case, even with well-designed lenses. Often the best focus distance for a lens will be farther away for higher f-numbers, and closer for smaller f-numbers. This is a separate issue from depth of field.

After saying all of the above I do agree that the method you describe is generally a good one, and should be the default mode for most cases. :-)

Oh, one more suggestion: Print a black and white striped target, one in which the width of each stripe is about two object space pixels wide. Place the target in the object plane and rotate it so that the lines are running at about 45 degrees to the axes of the pixels. This makes for a pretty sensitive target, one in which the out of focus condition may be observed not only by virtue of target lines that blur quickly, but also the observation of moire patterning that comes and goes quickly with change of focus.

Spencer Luster
Light Works, LLC