Page 1 of 1

Automating CA correction?

Posted: 2010-08-06T02:39:08-07:00
by KonfuseKitty
I would like to use ImageMagick to correct chromatic aberration, the kind that is caused by the lens, going from 0 at the centre of image to its max at the edges. I think it should be doable, but don't know where to start.

I'm surprised that my searches on the internet have revealed no IM methods in this regard. All I know is that dcraw offers what seems to be an easy parameter for CA correction, "-C 0.995 1.005" for instance, which works well. Using dcraw for this would be the easiest solution of all, if one built a list of corrections to be made for specific lenses, and then read the lens info from EXIF when doing the RAW conversion. Alas, the "0.995" part of the dcraw parameter interferes with subsequent processing of the file by ImageMagick, namely the "-contrast-stretch" procedure, it causes IM to output wrong colours. Using "-C 1 1.010" in dcraw instead avoids the IM problem, but doesn't correct CA as well.

I'm hoping there's enough interest in this in the IM community, we can collectively work something out.

Re: Automating CA correction?

Posted: 2010-08-06T09:51:51-07:00
by fmw42
From http://en.wikipedia.org/wiki/Chromatic_aberration there are two types: Axial and Transverse.

In my very simplistic conceptual point of view:

Transverse is simply a resize in x and/or y of the r,g,b channels independently of each other. Separate the channels (-separate), use -resize on each with different amounts and then recombine the chnanels (-combine)

Axia shifts in radius, so probably one can do a cartesian to polar transform (-distort DePolar), then separate the channels, then resize the images in radius different amounts, pad as needed so the channels are the same size, then combine and do a polar to cartesian transform (-distort Polar).

I have not given this much real thought nor tested any of this. These are just concepts at the moment open for discussion or for anyone else to experiment.

However, as IM is not a GUI based system, doing this to correct an image, may involve a lot of trial and error until the image looks good.

Re: Automating CA correction?

Posted: 2010-08-06T14:03:36-07:00
by fmw42
On second thought, I believe axial chromatic aberration is better corrected and easier with -distort barrel on each channel separately.

Re: Automating CA correction?

Posted: 2010-08-06T20:08:49-07:00
by fmw42
Alas, the "0.995" part of the dcraw parameter interferes with subsequent processing of the file by ImageMagick, namely the "-contrast-stretch" procedure, it causes IM to output wrong colours. Using "-C 1 1.010" in dcraw instead avoids the IM problem, but doesn't correct CA as well.

I think this may be because you are not correcting colors first and then stretching each channel separately. I would not think this would work consistently on all images. It seems rather ad hoc.

I think you need to find a way to color correct your images first from the camera information (somewhat like using -color-matrix), then perhaps CA correction, then contrast stretch consistently between channels.

This is just my guess at your issue.

I know little about dcraw, but I would think that it could do color correction as well as aberration correction at the same time. Perhaps you need some advice from a dcraw expert.

Looking at the docs for dcraw, I see:

Code: Select all

-C red_mag blue_mag
Enlarge the raw red and blue layers by the given factors, typically 0.999 to 1.001, to correct chromatic aberration. 
But I suspect that these are just typical values and not necessarily calibrated from your specific camera lens. Thus they likely will need adjusting for each camera, unless there are some built-in values in the meta data for a given camera.

This seems to me that all they are doing is resizing the channels separately, which means you do not have axial CA but lateral/transverse CA, which is the simpler form.

dcraw also has color correction by various means:

Code: Select all

COLOR OPTIONS

By default, dcraw uses a fixed white balance based on a color chart illuminated with a standard D65 lamp.
-w
Use the white balance specified by the camera. If this is not found, print a warning and use another method.
-a
Calculate the white balance by averaging the entire image.
-A left top width height
Calculate the white balance by averaging a rectangular area. First do dcraw -j -t 0 and select an area of neutral grey color.
-r mul0 mul1 mul2 mul3
Specify your own raw white balance. These multipliers can be cut and pasted from the output of dcraw -v.
+M or -M
Use (or don't use) any color matrix from the camera metadata. The default is +M if -w is set, -M otherwise. This option only affects Olympus, Leaf, and Phase One cameras.
-o [0-5]
Select the output colorspace when the -p option is not used:
0   Raw color (unique to each camera) 
1   sRGB D65 (default) 
2   Adobe RGB (1998) D65 
3   Wide Gamut RGB D65 
4   Kodak ProPhoto RGB D65 
5   XYZ

-p camera.icm [ -o output.icm ]
Use ICC profiles to define the camera's raw colorspace and the desired output colorspace (sRGB by default).
-p embed
Use the ICC profile embedded in the raw photo.
There are lots of options and approaches. Have you tried any or all of them? Esp. -w or -a or the one about color matrix or even colorspace.

You could also get a tool like PTstitcher or Hugin or Proxel (http://www.proxel.se/index.html) or other tools based upon Helmut Dersch's Panotools code (which is the basis of IMs -distort barrel) that has built in CA correction along with barrel distortion correction and other abberations.

Search Google for Chromatic Aberration Correction and you will find lots of pages on the topic:

http://wiki.panotools.org/Chromatic_aberration
http://www.erik-krause.de/index.htm?./ca/
http://www.youtube.com/watch?v=2EkzaboZWNs
http://hugin.sourceforge.net/tutorials/tca/en.shtml
http://kcd.sourceforge.net/fix-ca.php
http://toothwalker.org/optics/chromatic.html
http://www.isprs.org/proceedings/XXXVII ... pdf/05.pdf
http://cipa.icomos.org/text%20files/TURIN/403.pdf
http://www.prime-junta.net/pont/How_to/ ... tml?page=8
http://citeseerx.ist.psu.edu/viewdoc/do ... 1&type=pdf
http://wapedia.mobi/en/Chromatic_aberration
http://www.imagine-optic.com/downloads/ ... theory.pdf


Try posting on http://studio.imagemagick.org/pipermail/magick-users/. Wolgang Hugemann seems to be the expert on aberrations and their correction. Perhaps he will reply.

Re: Automating CA correction?

Posted: 2010-08-07T03:08:16-07:00
by KonfuseKitty
Thanks for the comprehensive replies, Fred.

A few clarifications:

The CA I'm concerned with is the one caused by the lens, where it's 0 at centre of image and severe at edges. I have seen different and sometimes contradictory terms used to describe it - lateral, linear, transversal - so I'm not sure which terms is actually the correct one. But I'm not that interested in the other kind of CA, where it's more or less uniform across the image and not caused by the lens, but by shooting through a water tank, for instance.

Dcraw doesn't do colour correction well in my tests. It doesn't even read the AdobeRGB profile that is set by my camera. I have to use the setting for dcraw to apply the profile to the image. I also use -w for white balance, and let IM do its magic afterward.

For the above reason, I can't do colour correction before contrast stretching. I don't see the point either, as the contrast stretch does an excellent job of it. The only issue is the highlights, as discussed in the other thread. But even that is only really an issue on the most challenging of files.

I'm aware of some of the links you posted, but I'm after an automated solution, tired of interactive solutions that are invariably poorly programmed! I miss the speed of Photoshop ACR on my old Mac in this regard, nothing I've tried on Linux comes close. People just don't seem motivated to optimise their code. I'm running a CPU now that is 2-8 times faster than my old Mac, and yet the interfaces I'm dealing with are slower. Take for instance the Color Aberration plugin for GIMP or the CA correction feature in Ufraw - rubbish in terms of speed of response.

So, IM to the rescue. I work with 5 lenses, so it shouldn't be that difficult to create a database of their parameters, feed them to IM, and have it done automatically.

The 'only' thing left is figuring it all out! Your pointers will help in that, I'll report back with results. :)

Re: Automating CA correction?

Posted: 2010-08-07T09:49:26-07:00
by fmw42
I have little experience with RAW files or dcraw. But its documentation as I listed above says it can use profiles or a color matrix.

However, if you have tried them and they do not work well, then so be it. I cannot attest to speed.

But it seems rather foolish to try to reinvent the wheel in IM. I don't think there are many users who know the mathematical details that the are used in dcraw or other professional software to do these things the right way. So we are just guessing at techniques and trying to fix problems that arise because the image is not pre-processed properly or in the correct order -- we are just applying patches.

It is unfortunate that tools like GIMP have lots of programmers contributing, but IM has only a very few who have other proper jobs besides working on IM and thus limited time for things like this.

I have to believe that there are better commercial tools that could do the things you want, because they have paid experts to program to specs of cameras and have detailed understanding of the mathematics of image formation and aberrations. And if it is really important, if you are doing professional photography and using RAW formats, then you might invest in them rather than beating yourself to death with guessing and experimenting on techniques to fix things that could potentially be done correctly with the right tools.

On the other hand, if you want to learn more and perhaps get some ideas about what might be needed to program in IM, then I would suggest you contact Wolgang Hugemann on the other forum. He knows a lot about such things and was partially instrumental in getting -distort barrel implemented in IM. He has a database of parameters for different camera systems that apparently came from PanoTools which uses Helmut Dersch's open source code.

With regard to CA, I believe what you need is just to do resizing of two channels per my note above by about the amounts specified by cdraw CA parameters. You can do this referenced to the center of the image using -distort SRT.

At least this is some place to start.