Monday, August 25, 2008

Part 6: Image Correction Alternatives

After enduring the math and tedium of developing our own corrections for images with vignetting, geometric distortion, and chromatic aberration, it's worth looking at some alternatives not involving such a high pain level.

First, if you're a user of a recent version of Photoshop, you have the Lens Correction filter available. This filter has adjustments for the 3 areas we've been exploring. It works fine for estimating simple adjustments in any of these 3 areas but it doesn't have any kind of built in knowledge of adjustments required for particular lens/camera combinations. If you only need approximate corrections and like the easy-to-use interface, this is a good way to go. If, on the other hand, you have stricter requirements or you have short focal length lenses with complex mustache distortion or other more complex problems, the Lens Correction filter won't handle it.

Another option to consider is DxO Optics Pro which is a sophisticated product handling a number of image problems including those we've been discussing plus many others. This software is tied directly to your camera/lens combination by way of modules that have very specific correction parameters built in that are the result of DxO's expert lab testing. Results can be very good, the product comes with standalone and Photoshop plugin versions, and can be used to batch process large quantities of images fairly easily. On the minus side, it's not inexpensive (although certainly not overpriced for what it does), it's a bit daunting to learn the quirky interface, and it only supports a subset of camera and lens combinations. For example, at the time of this writing, it doesn't support the Canon G9 we've been using as our example in this series.

Finally, there's PTLens which is a much less ambitious effort in terms of functionality but which has a much broader camera/lens coverage than DxO. It comes with a standalone version (handling only JPEG and TIFF images) and a Photoshop plugin. In terms of cost (about 10% of DxO), simplicity, and effectiveness for its strong point, geometric distortion correction, this is a gem. It also handles vignetting and chromatic aberration but you must make those adjustments manually, unlike the corrections for geometric distortion which cover a very broad range of cameras and lenses. PTLens had its genesis from Panorama Tools roots and handles geometric distortion under the covers in the same way that we discussed using the third-order polynomial radial corrections—except you don't have to be aware of it. By the way, the manual corrections for chromatic aberration in PTLens are also analogous to what we developed using corrections for the red and blue channels. In fact, the PTLens sliders show the numeric radial deviation from 1.0 (no change) as you visually tweak the image to your satisfaction. You can interchangeably use the PTLens numbers and the d coefficients we used in hugin parameters. I routinely will use PTLens to get in the ballpark and then transfer the corrections to hugin to refine the correction to as close as I can get to eliminating transverse chromatic aberration. The PTLens Photoshop plugin can be used to handle large numbers of images simply by making a Photoshop Action to run during ACR processing for example. The one thing I don't like about PTLens is it doesn't handle very large images well, having a tendency to bomb out with memory errors. But, other than that, you'll have a hard time finding a bigger "bang for the buck".

Both DxO Optics Pro and PTLens have free trial versions which should be exploited if you're at all interested in looking at their capabilities.

Given you can buy good solutions for correcting image problems we've been discussing, you may wonder why all the trouble doing it ourselves with all those measurements and mathematical manipulations. There are a few reasons. First, none of the products mentioned above handles all camera/lens combinations. For example, DxO doesn't handle the Canon G9 and PTLens doesn't handle the Canon G9 with the Raynox wide angle adapter we mentioned back in the post about geometric distortion. Second, by doing your own measurements you can be more confident that your custom-built corrections are as accurate as possible for your particular images. And third, there's nothing like grinding your nose into the pixel-level image flaws to get a real understanding of what your equipment capabilities and limitations are.

Next up, some final thoughts on compact consumer cameras generally and the Canon G9 in particular, given what I've learned going through these image correction exercises. After that, maybe we'll get back to some real photography discussion instead of all this techie pixel-bending stuff.

Monday, August 18, 2008

Part 5: Putting It All Together

In previous posts, we've looked at the Canon G9 point-and-shoot camera's vignetting, geometric distortion, and chromatic aberration. We've also looked at strategies for addressing these issues, primarily using the fulla command from the hugin panorama photo stitcher package.

The fulla command doesn't have a nice friendly GUI interface but it does allow us to put together arbitrary corrections to be applied to large numbers of images without a great deal of effort. Each of the fulla corrections we've explored in previous posts can be combined into a single command addressing vignetting, geometric distortion, and chromatic aberration.

Your workflow sequence is very important. The corrections we've been discussing should be applied to the images before any cropping, resizing, etc. have been done. I always shoot raw so I use ACR or some other tool for "developing" the image to set the correct initial tonality, color temperature, etc. and then export the image in 16-bit TIFF format (lossless unlike JPEG). I apply the image corrections and then bring the corrected version into Photoshop for subsequent processing.

Let's start with a hypothetical file shot on the Canon G9 at 7.4 mm and f/4, and processed from the raw CR2 file into a 16-bit TIFF file called example.tif. To apply my standard corrections for vignetting, geometric distortion, and chromatic aberration, I would use the following command:

fulla -c 1.0398:-0.1155:0.1954:-0.1605 \
-g 0.028:-0.0871:0.0521:1.007 \
-r 0:0:0:1.00024 -b 0:0:0:1.00041 example.tif

(The '\' characters indicate arbitrary line breaks for formatting here. This is actually all one command line.)

The "-c" option gives the polynomial coefficients for correcting vignetting, the "-g" option gives the polynomial coefficients for geometric distortion correction, and the "-r" and "-b" options provided the polynomial coefficients for transverse chromatic aberration correction. (But you already knew that.)

When fulla is done crunching the numbers, it outputs a file with a "_corr" filename suffix. So our example correction would create a new file called example_corr.tif. It will look substantially better than the original with the corrections applied.

Naturally you don't want to have to type in that command every time you process a file so you can create a MSDos batch command file (on Windows) or a Bash script (on Unix systems) to generalize it. Let's say we want to correct arbitrary numbers of files with one command. We can create a batch file like this:


for %%f in (%1) do \
fulla -c 1.0398:-0.1155:0.1954:-0.1605 \
-g 0.028:-0.0871:0.0521:1.007 \
-r 0:0:0:1.00024 -b 0:0:0:1.00041 %%f

(Once again, this should be all on one line in the real batch file.) Assuming you named this something like G9_74_4.bat (because it's only useful for the G9's 7.4 mm f/4 images), you can process all the TIFF files in a directory with a single command:

g9_74_4 *.tif

Unix, Linux, Mac users can create analogous script files using for iterations to do precisely the same thing.

I must mention something about the fulla command here: it's flaky and not mature in some regards. One frustration is that, when it outputs a corrected image, that corrected image has its metadata stored in an unconventional way that is not visible to Photoshop and many other applications. If you're running any kind of responsible workflow, this is not good, but there is a way around the problem.

ExifTool by Phil Harvey is a superbly-done high-function image metadata management tool. It has an extraordinary number of capabilities and it handles them well. If you download the tool, you can use it to save your EXIF (and other metadata) from your image, apply your fulla corrections, and then restore the original metadata in standard format. For example, you can save the metadata for all TIFF files in a subdirectory with this command:

exiftool -o %f.mie -ext tif .

Process your files using the fulla corrections and then restore the metadata to the corrected files.

exiftool -overwrite_original -tagsfromfile %-.5f.mie \
-ext tif .

You can even skip the intial save to MIE file (Meta Information Encapsulation) and simply rewrite the original file's metadata over the fulla output file:

exiftool -overwrite_original -tagsfromfile %-.5f.%e \
example_corr.tif


Finally, if you're really ambitious and have Perl programming skills, you could use the Perl Image::ExifTool package to determine the focal length and f-stop for the image and then do a table lookup to determine which of the fulla option parameters to use. I've done a rudimentary job of this just for the subset of camera settings I almost always use for the Canon G9.

If all these seems like a lot of trouble to correct the occasional image, you're right. It's most worthwhile if you routinely have large numbers of images you wish to optimize; then the use of fulla in batch or script files makes more sense.

If you only do occasional corrections, want something a bit easier to deal with, or want a commercial solution, stay tuned. Next up we'll glance at a couple of alternatives for correcting images that don't require all the up-front work we've been slogging through here.

Sunday, August 17, 2008

Part 4: Chromatic Aberration

There are many reasons why an image may contain chromatic aberrations, the unexpected color casts or fringes in certain parts of the image. Lenses, particularly inexpensive ones such as found in point-and-shoot cameras, can contribute two main types of chromatic aberration: longitudinal (or axial) and transverse (or lateral).

Longitudinal chromatic aberration is caused by the lens system's failure to focus all wavelengths of light to the same focal plane. If we look, up close, at a piece of an image with sharp high-contrast edges, we can see evidence of longitudinal chromatic aberration in the unnatural blue and purple casts of leaves and branches in this example. This is part of a Canon G9 image shot at 7.4 mm at f/2.8.



The solution to longitudinal chromatic aberration is often quite simple: stop the aperture down, causing a greater overall depth of field and reducing the effect of the wavelength focus differences. When we close the aperture down to f/5.6, we notice a much better color rendition on this same subject.



From a series of tests on this subject, I notice the longitudinal chromatic aberration is mostly gone at f/4, and almost totally gone at f/5.6. There is no noticeable improvement if you stop down further. However, due to diffraction limitations, the image turns to mush, particularly at f/8. So, the rule of thumb is to stick between f/4 and f/5.6 to limit longitudinal chromatic aberration in images.

Transverse chromatic aberration is caused by the lens system enlarging the image on the focal plane to differing amounts depending on the wavelength of light. The effect is more noticeable the farther from the center of the image you look.

To evaluate transverse chromatic aberration, I taped a large (6 feet by 10 feet) sheet of white paper to the side of the garage and placed black electrical-tape X's all over it. This provided a target with a few handy properties for this exercise: relatively neutral tones (not favoring a particular color strongly), high contrast sharp edges between the black tape and white paper, and daylight illumination covering the whole visible spectrum (unlike, for example, fluorescent lights which have spikes at various wavelengths).



I set the Canon G9 camera to aperture-preferred f/4, framed the target to get useful X's to the corners, and shot at several focal lengths from the shortest, 7.4 mm, to the longest, 44.4 mm.

If we take a close look at the upper left corner of the 7.4 mm shot, we can see evidence of significant chromatic aberration around the X. (This sample of the full image is blown up to twice size and the saturation boosted way up to make the color aberrations quite clear.)

To correct this, we're going to use the fulla command mentioned frequently in previous posts. It's part of the freely-available hugin panorama photo stitcher package. The approach taken by the fulla command is to leave the green channel of the image alone and adjust the blue and red channels to line up correctly with the green channel. This makes a lot of sense since green appears brightest to our eyesight, there are twice as many green photo sites on a Bayer matrix sensor as there are either red or blue photo sites, and usually the green channel is the cleanest compared to the other two. Like we've seen in the discussions of vignetting and geometric distortion, fulla uses a polynomial to accurately represent adjustments to be made to each of the red and blue channels depending on radial distance from the center of the image. The polynomial adjustment, once again is of the form

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


There are a few ways we can approach finding the values for a, b, c, and d to use for correction. First, we can break the image into separate R, G, and B channel images and use one of the panorama stitching programs to compare the red and green channel images, select control points, and optimize the corrections for the red channel image required to make it line up with the green channel. Do the same thing comparing the blue channel image with the green channel, and we can get the set of polynomial coefficients that will shape the red and blue channels to line up with the green.

To break an image into separate channels, you can use Photoshop, go to the Channels tab, and select Split Channels. This will create 3 grayscale images, one for each color channel, with "_R", "_G", or "_B" appended to the filename to indicate which color channel it represents.

Another alternative to the tedium of fitting control points in a panorama stitching tool, is to use the tca_correct tool supplied in the hugin package. This command-line program also needs the separate channels broken out into separate image files with each beginning with "red_", "green_", or "blue_" denoting the channel it represents. An additional requirement is that each of the channel images must be an RGB image, not grayscale so that additional step would need to be done in Photoshop if you're going that route.

Another way to get the separate channel images broken out is through the use of ImageMagick a wonderfully powerful set of command-line tools available on all major platforms. Using ImageMagick's convert command we can extract each of the RGB channels into separate RGB image files:

convert %1 -channel R -separate -depth 8 -type TrueColor red_%1
convert %1 -channel G -separate -depth 8 -type TrueColor green_%1
convert %1 -channel B -separate -depth 8 -type TrueColor blue_%1

(This example uses a MSDos batch command file for Windows; the analogous thing can be done with the same parameters on the other platforms.)

The tca_correct command to evaluate your image and determine the polynomial coefficients is

tca_correct -o abc filename

where filename is the root file name without the color prefixes. After crunching things for a bit, it will show the fulla options to be used to correct the transverse chromatic aberration.

This is ingenious, simple, and a great time saver. However, there are a couple of problems with this approach. First, the tca_correct command is not being distributed reliably with the various hugin updated packages. Second, it doesn't work. At least for me, it didn't work well—it seemed to substantially under-correct the test images I used.

Another way to go, the method I chose, is to use the old reliable eyeball-it approach. For the G9 test images I was looking at, the transverse chromatic aberration appeared to be approximately linear with the radius. Logically this makes sense given that, by it's nature, transverse chromatic aberration is caused by the failure to cast different wavelengths on the image plane with the same magnification. We need to shrink or expand the red and blue channels to line up with the green. I used the fulla chromatic aberration correction command options on my test images adjusting only the d polynomial coefficient which is the linear term of the expression and thus causes a linear expansion or contraction of the channel.

Fulla wants 2 options provided for the red and blue channel adjustments ("-r", and "-b" respectively) providing the polynomial coefficients in the usual syntax:

fulla -r a:b:c:d -b a:b:c:d filename


I set the a, b, and c coefficients to zero and start with a small adjustment. By examination of the test image above, it's clear both the red and blue channels need to be pushed out so we start with something like this:

fulla -r 0:0:0:1.0003 -b 0:0:0:1.0003 filename


We examine the output, and adjust blue and red channels up or down as needed. For the 7.4 mm f/4 test image above, I ended up with the following adjustments:






fulla -r 0:0:0:1.00024 -b 0:0:0:1.00041 filename

As you can see in the processed image (magnified and saturation-enhanced the same as the original above), we've eliminated the most egregious transverse chromatic aberrations.

With luck, the rest of the image will be similarly corrected. If not, there may be secondary adjustments required where you have to start tweaking the c coefficients, for example. It's not something to look forward to.

Now that we've addressed longitudinal and transverse chromatic aberrations, our images should be free of chromatic aberrations, right? Sadly, no. There are other chromatic aberrations originating in other parts of the camera system including the photo sites themselves and the microlenses on top of them. Once again, the point-and-shoot cameras tend to be worse, particularly due to the tiny photo sites and their very high density. These aberrations, unfortunately, can not be fixed as easily as what we've been talking about. However, usually these are not nearly as noticeable as the aberrations we do have some control over.

Next, we'll put vignette correction, geometric distortion correction, and transverse chromatic aberration correction together in a more organized way now that the hard work has been done. We'll also look at a couple of commercial alternatives to our labor-intensive solution, one modestly priced and the other more expensive and robust.