Showing posts with label vignette. Show all posts
Showing posts with label vignette. Show all posts

Friday, May 8, 2009

Vignetting Revisited

After enduring a little confusion and frustration in correcting some images from my Canon G9 camera, I investigated a little more and found that my process needs to be changed. A while back, in Part 2: Vignette Correction, I described a way to mathematically characterize and correct image vignetting. It was fine theoretically but, in practice, I found a little surprise.

To get a better idea of the vignetting characteristics, and not having a proper optical opal glass, I improvised using some nice polyester drafting film I had laying around. I cut 2 pieces and sandwiched them between cardboard mats to create a nice white diffuse window about 3 inches in diameter. I aim the camera up at a dull gray overcast sky, place the diffuser on top of the lens hood, and shoot at the various apertures. This worked pretty well for a quick test.


Image 1 is an example of a shot using this technique with some post-processing to really draw out the shape of the light falloff. (In Photoshop I posterized the image and boosted the contrast.) This particular shot was using a 24 mm lens at f/2.8 on a Nikon D700 dSLR. It shows the expected radial light fall-off. It is this shape of light falloff that responds well to the mathematical corrections I described earlier. Also vignette tools in Photoshop and other image editors would handle this fairly easily if not quite as precisely.


So, what's the problem with the G9? Take a look at image 2 which was shot with the G9 at 7.4mm focal length (the shortest of the zoom range) at f/2.8. It was processed in a manner similar to the other one to draw out the tonal structure. You can see the problems: the brightest part is not in the center of the image, there is less of the well-behaved radial symmetry seen in the first image, and the shape doesn't resemble the neat bullseye we saw earlier.

Sampling readings in various parts of the image show that each corner has a different tonal value (unlike the better-behaved first image). The lightest corner is more than 12% lighter than the darkest corner. These asymmetries make it impossible to use the mathematical technique, based on radial symmetry, that I was trying to use up to this point, at least not with best results.

The solution is to use this flatfield image (without the adjustments used for illustrative purposes) as an adjustment template to precisely match tonal corrections to actual system characteristics. A tool like the fulla command discussed extensively in earlier posts handles this nicely:


fulla -f flatfield_image target_image


We simply use a flatfield image shot at the same focal length and aperture as the target image and fulla will apply corrections to compensate for the vignetting characteristics no matter how odd they may be. Of course now this means that you need flatfield images for each of the focal length and aperture combinations you use. As I've mentioned earlier, this is a bit simpler for the G9 in that I only use f/4 or f/5.6, the sweet spot between lateral chromatic aberration and diffraction limits.

At this point, I've ordered some opal glass (not expensive) so I can do more careful flatfield images to get optimal corrections of vignetting.

So, what's the deal with the odd G9 characteristics? I don't know really. Based on this sample of one camera, I'm guessing that there are manufacturing variations and less-than-perfect alignment in the system but there are other possibilities in the engineering of the system that may help explain the odd behavior. By the way, the off-center vignetting asymmetry moves and changes shape slightly as you go through the zoom range. Go figure. I'll bet these kinds of variations are common in the compact and consumer class of cameras in general.

I have enough of a grasp on reality to know that this level of fussing is way beyond what most people feel there's a need for. Frankly I haven't lost a great deal of sleep over it either but it's fun to try to squeeze out the best images you can from what you're working with.

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, July 6, 2008

Part 2: Vignette Correction

All camera/lens systems vignette the produced images, some much more than others. Vignetting (or more-correctly light falloff as we're discussing here), the darkening of the created image toward the edges, is caused by a number of factors. We are so used to seeing vignetting in images, we usually don't notice it; in fact many think that vignetting can add to the appeal of an image and will add more in during post-processing. Vignetting in some applications is not desirable: scientific and technical images, cartographic images, repro work, and panorama stitching are among the applications where vignetting is to be avoided.

In this image, the upper left portion is a flat evenly-lit surface showing the effects of vignetting causing light fall-off radiating from the center. It becomes noticeable when compared to the true flat field uniform gray half of the image in the lower right.

We're going to take a look at a couple of options for eliminating or reducing vignetting in images during post-processing, particularly using the fulla command available in the free hugin panorama photo stitcher package. The fulla command is a useful utility we'll explore for other purposes in future posts, as well as using it to address vignetting here.

Before we get started, a couple of things need to be understood. First, vignette characteristics are unique to a particular camera,lens, focal length, and aperture. Change any one of those and the vignette characteristics change. The rate of brightness falloff from center to edges of an image is not uniform; systems that may cause the same center-to-edge brightness falloff may do it in very different ways. It can happen in a gradual linear way or it may have relatively uniform brightness until getting closer to the edges when it drops of quickly. Vignetting is not necessarily radially symmetrical, although in most cases it is close enough for our purposes in this discussion. Finally, this overview is not for the faint of heart; it outlines the non-obvious aspects of what we're doing but if you wish to pursue this yourself, you must be willing to dig in and work through the details yourself.

Using a flat field image as a corrective for other images is a technique that has been around a long time and is still commonly used by astrophotographers. Essentially, you shoot an image of a perfectly uniform empty subject devoid of any detail and uniformly lit from edge to edge. A clear cloudless haze-free sky directly overhead would be an example. You then apply this image to target images using software that cancels out the brightness differences represented in the flat field image. The advantage to this approach is that you are directly using the systems recorded image to cancel out the brightness anomalies in the produced images. The fulla command can use a flat-field image to correct for vignetting by passing the flat-field image's name as a command option.

The trouble comes when you try to produce an accurate flat-field image for your camera, particularly if you aren't doing this for a telephoto lens. It's actually fiendishly difficult to get the featureless uniformly-lit subject you need to do this. The gray image above is an attempt to produce such a flat-field image (upper left portion) using the Canon G9 camera's 7.4 mm focal length and f/2.8 aperture. This is a relatively wide angle (about 50° on the long side), presenting quite a challenge for coming up with a suitable flat-field subject. I was not successful—I came close but never could get an image in which all corners were consistent, for example.

Another approach is to correct the vignetting mathematically. You provide a formula to software that can use to determine what brightness correction should be used at each radial distance from the center of the image. The fulla command provides 2 options for doing this, one in which you provide the formula for the value that should be added to the brightness depending on radial distance, the other method uses a correction in which the brightness is divided by a value provided by a formula. I found the latter worked better for me. The fulla command has very sparse and nearly unusable documentation so I'll fill in the details as I describe the process I used to profile my camera system for vignette correction.

The net result of what we're going to do is to provide a set of 4 numbers to a fulla option that describe an equation fulla uses to determine what adjustment needs to be made at each radial distance in the image. The four numbers are coefficients for an even-powered 6th order polynomial of the following form:

a + b * r2 + c * r4 + d * r6

where r is the radius from the center of the image (0) to the farthest corner (1.0). This formula is applied at each radial distance by dividing the image's RGB values by the calculated factor to determine what the new adjusted values should be. Assuming you haven't left already, our job is to find the values for a, b, c, and d that work best for the particular camera, lens, focal length, and aperture we're interested in correcting.

The first step is to measure the brightness fall-off from the center to the corners of an image produced by your system. Because there's so much trouble trying to get a single flat-field image, we'll shoot multiple images of a uniformly-lit small target, panning the camera from the center to a corner between shots. Our goal is to get about 15 or 20 shots of the target ranging in position from the center to the lower right corner of the image.

I used a gray card illuminated by an incandescent lamp that was turned on about 20 minutes before starting to make sure the light has stabilized. The camera was mounted on a tripod about 15 feet away and set on the focal length and aperture I wanted to profile (7.4 mm at f/2.8 for this example). I adjusted the tripod head so the camera was angled to the left in such a way that I could pan over the subject from the center to the lower left corner without adjusting anything else on the camera. Then I made the series of about 20 shots panning a little more to the left each time until I had images of the target all the way into the farthest reaches of the corner. I made sure I was using manual exposure and the target was approximately gray (midrange) in value. By the way, I was shooting raw, not JPEG, as I didn't want the internal JPEG processing mucking up the precision of my test images.

Next step is process the images precisely how you would normally do it. Do not apply any contrast adjustments to the images unless you use exactly the same adjustments on all your images all the time. I ended up generating about 20 TIFF files that I could load in sequence into Photoshop to sample the brightness of the target at each position.

Next use your Photoshop (or other) eyedropper tool to measure the same relative location of your target in each image. I used the 5X5 averaging for more uniform readings. Record the location in the image that you sample (x and y values) and the readout of the brightness (either the green channel, for example, or gray value if you converted the images to grayscale during processing).

Plug these into a spreadsheet and start crunching the numbers into something more meaningful. First, you want to convert the x and y values into a radius value ranging from 0 at the center to 1.0 at the far corner. The Canon G9 I used makes images 4000 X 3000 so the spreadsheet formula for finding the radius based on x and y readings goes something like this:

=sqrt(($Xn-2000)^2+($Yn-1500)^2)/2500

where X and Y are, obviously whatever column numbers you used to plug in your x and y coordinate numbers.

Next, create a column of your radius numbers squared—this will be used in determining the magic coefficient numbers we're after.

Finally, create a column of normalized brightness levels. At this point you need to decide how you want to correct your image's brightness levels for vignetting. You could brighten the darker outer region, darken the lighter central region, or do something in between. I took the in-between route to minimize the damage I'm doing to pixels that are having their brightness adjusted. So, my normalized brightness values are calculated by dividing the recorded brightness value by the average of the brightest and darkest values. This would give you a range of numbers going from a little over 1.0 to a little less than 1.0 depending on how bad the vignetting is. For my f/2.8 test images, the brightest central reading was 153 and the darkest corner reading was 129 giving me a normalized range of 1.085 to 0.915, approximately.

Next we calculate a polynomial regression line through the values we've calculated. If you are using an Excel spreadsheet, this is easy. Create a XY (Scatter) graph of normalized brightness values corresponding to the radius squared. Then on the chart do Add Trendline and, in the Type tab, select Polynomial and Order 3; in the Options tab select "Display equation on chart" and "Display R-squared value on chart". Once you click OK, a regression line will be drawn through your scatter plot and an equation will show up on your graph. This equation has the a, b, c, and d coefficient numbers we've been working toward. In my f/2.8 case, the regression equation produced was

y = -0.1355x3+0.0443x2-0.0943x+1.0873

The terms are in the reverse order of the fulla format so our a, b, c, and d coefficients for fulla are 1.0873, -0.0943, 0.0443, and -0.1355 respectively. The R2 (squared coefficient of variation) value was 0.9963 in my case, which is a very good fit. (A R2 of 1.0 represents a perfect fit while 0 represents no correlation whatsoever; the closer to 1.0 we get, the more confidence the regression line is a good representation of our measured data.)

So, why did we plot against radius squared rather than radius? It's because we need the even-numbered sixth-order polynomial which isn't available as an option in Excel so we finessed it by using a 3rd-order polynomial regression line against r2.

If you don't have Excel, you may have access to another spreadsheet program with a similar regression analysis capability. If not, there are other programs such as statistical analysis packages that can do it, or you can even use online tools to accomplish the same thing. See for example http://udel.edu/~mcdonald/statcurvreg.html and scroll down to the heading, "How to do the test" which has links to several online tools as well as a spreadsheet you can download for use when you don't have the integrated trend line feature.

Next we look at a quick test. This image is a composite of 2 images shot with a pan between to see how they match up. Notice the seam line where the two images line up. The vignetting is clearly visible. If we process the two images separately using the fulla tool, we can check how effective our calculations were. We use our calculated coefficients in fulla using the command like this:

fulla -c 1.0873:-0.0943:0.0443:-0.1355 image_file_name

When we composite the corrected images together in the same way as the originals, we see the seam is markedly less visible indicating our correction has improved the vignetting situation.

Is it perfect? Not at all. This method can cause color shifts because it's not actually correcting strictly luminance but rather just doing a crude adjustment of the RGB values in the image. It also uses assumptions of perfect radial behavior, an imperfect regression line, imperfect input test data, and so on. However, in most cases it is an improvement as we can see in the corrected image. Is it worth the trouble? Perhaps not for most images but once the calculations are done it can be applied nearly effortlessly to any image shot with the same camera, lens, focal length, and aperture combination such as what we're targeting here for our panoramas.

There are other more advanced techniques evolving that can guess at vignette correction, particularly in multi-image panorama environments and make adjustments accordingly. Of course there's the now-standard panorama tools Smartblend, Enblend, etc. that disguise vignette and other tonal anomalies.

Next up, we'll look at lens distortion and how we can correct that using fulla as well. We're working toward a single fulla command line to correct as many flaws as we can and when we're finished we'll put together some simple scripts to simplify correcting batches of images.

Vignetting Revisited

After enduring a little confusion and frustration in correcting some images from my Canon G9 camera, I investigated a little more and found ...