Monday, July 28, 2008

Part 3: Geometric Lens Distortion Correction

All lenses, particularly cheaper mass-produced lenses have some amount of geometric distortion. Some primary lenses, particularly expensive ones, can be highly corrected. Others, particularly with large zoom ranges, are characterized by mixes of barrel or pincushion distortion (or both). The result is images where straight lines appear curved, particularly noticeable in the outer portions of the images.

In 1998 Helmut Dersch created a set of software tools that could be used to, among other things, correct lens distortions so that they could be more effectively stitched together in panoramas. His library of tools caused a cottage industry to grow with many GUI front-ends developed to use the tools. Not everyone is interested in creating panoramas, but many are interested in correcting noticeable distortions for things like ocean horizons, architectural features, and many other distortion-sensitive subjects.



The Canon G9, which we've been using as the guinea pig for our series discussion, has a zoom range of 7.4 mm to 44.4 mm (equivalent to roughly 35 - 210 mm in 35 mm terms). At the short end of the range, it has moderate inner barrel and outer pincushion distortion (the combination commonly called mustache distortion). As the lens zooms out, images acquire slight pincushion distortion. The compound distortions in the lens don't correct well using simplistic generic tools so we'll develop specific corrections to flatten the images at the various zoom settings.

We need to apply a corrective distortion to the image to compensate for the lens-induced distortions. The image distortions are radially symmetric so we'll use a function that determines image distortion based on radial distance from the center of the image. In short, we'll be using a 3rd-order polynomial to correct the radial distance each part of the picture needs to be moved, based on its original radial distance from the center:

a * r3 + b * r2 + c * r + d

Our goal is to find the values for a, b, and c to correct for our lens's distortion at a specific focal length. The d value is only used to adjust the total image size to compensate for the other corrections. It has a value of

d = 1 - (a + b + c)

There are a few approaches to finding the coefficients for the polynomial. One way is to shoot several images and stitch them together into a panorama using one of the commonly-available tools and then note the corrections the software made to fit the images together. In essence, the process of stitching the images accurately requires the software to correct for distortions to complete its job.

We'll use a more targeted approach using one image. In this technique, you shoot an image that contains several horizontal and vertical lines that you know are straight. A large office building is ideal. The objective is to have several lines available in the image at varying distances from the center of the image and stretching all the way to the edges of the image. You then use a panorama stitching tool such as the freely-available hugin panorama photo stitcher package. Using hugin to do this, you load the target image twice, cancel the automatic control point detection, and manually add control points along the lengths of each of the lines you know should be straight. All control points on a line must belong to the same line labeled tn where n is a line number starting at 3 for the first line. (Lower numbers are reserved for other types of lines.)

Once you go through the tedium of identifying a few hundred control points on 6 to 12 lines covering various parts of the image, you run the optimizer to determine what the coefficients should be to cause the distorted lines to become straight.

For the G9, I created test images through the zoom range and here are the corrections I came up with which give me images with the lens distortions largely removed.


Canon G9 Lens Distortion Correction Coefficients
Focal Length a bc d
7.4 mm 0.028 -0.0871 0.0521 1.007
8.2 mm 0.0313 -0.089 0.0474 1.0103
9.0 mm 0.0082 -0.0186 -0.0046 1.015
12.7 mm 0.0187 -0.0558 0.0528 0.9843
16.8 mm -0.0172 0.0541 -0.0477 1.0108
22.0 mm -0.0053 0.0196 -0.0201 1.0058
25.0 mm -0.0038 0.0131 -0.0133 1.004
29.2 mm -0.0065 0.0184 -0.0111 0.9992
36.8 mm 0.0102 -0.0355 0.0391 0.9862
44.4 mm -0.0159 0.0626 -0.0674 1.0207
Special bonus: 7.4 mm with Raynox 0.7X wide angle conversion lens attached:
5.2 mm 0.0513 -0.1509 0.0835 1.0161


To apply corrections to an image, you can use the hugin fulla command we've mentioned previously. This time we use the -g option to pass our correction coefficients:

fulla -g 0.028:-0.0871:0.0521:1.007 image_file_name

The output will be our distortion-corrected image. (In this case the image of the building was shot with the G9 set at its shortest focal length, 7.4 mm, so we use the coefficients corresponding to that focal length.)



This technique will go a long way toward correcting obvious geometric distortion but it's not perfect. It doesn't correct to perfection although in most cases it's nearly impossible to tell without careful measurements of the resulting image. It also doesn't handle radially asymmetric distortions such as might occur in misaligned lenses.

Next up, we'll take a look at chromatic aberration and see how we can correct one type of it.

2 comments:

Me said...

I am back and probably a bunch of others amateur photographers that I sent a link to your blog.

Lisa

Masoud said...

this weblog is very impressive ! two thumbs up to you for having started it. where do you get some detailed information about different aspects of photography though ?

keep up the great work you've started !