Distortion Correction

# Distortion Correction

Building a lens always consists of tradeoffs. One important tradeoff is complexity versus price - the simpler a lens is made, the cheaper it can be manufactured. The downsides are usually increased distortion, lower sharpness etc. With the increased processing power of today's cameras, however, a lot of the disadvantages of simple lenses can be made up for in software. Some systems, like the Micro Four Thirds[a], rely on in-camera image correction - for examples, see the excellent article In-camera distortion correction[b] over at Digital Photography Review. Packages like DxO Optics[c] has correction of lens defects as their main selling point.

So how can this be done?

Well, distortion is basically pixels ending up where they shouldn't. So we can, for each pixel in an image, define a vector that tells us where this pixel should have ended up. The correction process then involves simply moving each pixel to its right spot. Since the distortion is mostly dependent on the focal length of the lens, we can re-use the vectors for all photos taken with that lens at that particular focal length setting.

In order for us to define the vector field we have to have some reference image. First I tried taping graph paper to the wall, but for the 10mm lens I was using I realized I would end up taping up a lot of paper, and I couldn't get the lines on the paper to quite match up. Finally it struck me - I already had a device that could produce exactly straight lines: my monitor. So I sketched up a grid in Photoshop and snapped two shots of it.

The distortion is measured using two test targets - one for horizontal distortion and one for vertical:

The measurement process is the same for both, and I'll describe how the horizontal distortion measurement happens. The vertical measurement is the same, except we rotate the test target 90 degrees and then rotate the vectors back 90 degrees.

1. We scan the center scanline of the image. For each black line detected, we note its x-coordinate. This is the reference x-coordinate for the whole line. It is assumed that a perfect lens would have put all pixels of this black line at this x-coordinate, but that lens imperfections have caused it to end up elsewhere.

1. Then we follow the black line outward from the center.

2. For each scanline we re-acquire the black line. Since we know where the line ought to be (the reference x-coordinate) we now have the horizontal distortion for the line at that scanline.

3. We now have distortion values for a full column - almost.

4. For the scanlines where we couldn't find the black line (bad contrast, noise), we interpolate the values.

5. The values are smoothed using a box blur filter with radius 8 to get rid of measurement noise.

6. Store the distortion values for this column.

2. We now have distortion values for selected columns in the image.

3. These are interpolated across each scanline, to fill in the blanks between the columns. For this test, I used linear interpolation, but more complex interpolators can be used.

4. The result is a vector field giving the distortion at each pixel of the image.