Lens distortion documentation

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
ixussuxi
Posts: 2
Joined: 2011-11-28T18:07:54-07:00
Authentication code: 8675308

Lens distortion documentation

Post by ixussuxi »

I've been using IM for a while to process my digital images (following some great pointers on photo blogs and forums) and have just added lens correction into my processing pipeline. To calculate the lens parameters for barrel distortion I followed http://www.imagemagick.org/Usage/lens/ to determine the correction coefficients a, b and c.

The trick with hugin works brilliantly. It is wonderful to see straight lines snap into place after calculating the parameters and applying the IM filter. But there are a couple of odd points in the documentation. Quote:
The coefficients a and c are set to zero, i.e. the distortion is described only by the second-order term b (as predicted by lens theory).
What lens theory is that? This seems to imply a and b should always be zero. In fact for the lens I used all of a, b, c seemed highly significant, suggesting fairly mathematically complex barrel distortion. In this case setting a=c=0 certainly fails.

More trivial, but adding to my confusion the example given is:
convert DSCN0001.jpg -distort barrel "0.0 -0.019966 0.0 0.980034" DSCN0001_pt.jpg
where the 4th coefficient d is chosen so that the coefficients sum to 1. But clearly -0.019966+0.980034 is not 1! In fact as the main IM docs state that if d is omitted it will be calculated and supplied automatically, which seems like a safer option.

In fact I found the description of how to set up hugin a bit confusing too, but the supplied example PTO file made everything clear. Overall the page was a great find.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Lens distortion documentation

Post by fmw42 »

I believe that many (good) cameras have published coefficients for lens (barrel, pincushion) distortion. You may be able to find tables online for a number of cameras. ( I believe that user Wolfgang Hugemann may have his own tables and some method of computation)

The best resource for this distortion is Helmut Dersch http://web.archive.org/web/200906130408 ... arrel.html. Many commercial tools are based on his panorama tools. http://web.archive.org/web/200906271056 ... /arch.html


You might also look at http://en.wikipedia.org/wiki/Barrel_distortion and references inside

There are also references to calibration techniques you can find by searching Google
User avatar
whugemann
Posts: 289
Joined: 2011-03-28T07:11:31-07:00
Authentication code: 8675308
Location: Münster, Germany 52°N,7.6°E

Re: Lens distortion documentation

Post by whugemann »

Well, I wrote most of this page, so this im my answer:

As far as I remember, IM at first didn't do what it promised: When the parameter d was omitted, it did not use the non-scaling restraint to calculate it. This might have been corrected in the meantime.

The numeric example is a simple mistake in the signs of the coefficients. I erroneously took +0.019966 and this gives one when added to 0.980034, sorry.

Concerning the power series expansion for the lens distortion, you should google for "Brown Conrady" or alike. You will easily find out that lens theory predicts only the even powers to be present. This has been disregarded by Harry Deutsch, the "inventor" of panarama tools and its lens correction model. As wrong as it may be, it can be considered as "the" standard model for lens distortion nowadays, because it provides the needed tools for free.

As you observed, a and c are in general not zero: If you offer the algorithm three parameters to adjust and don't allow to adjust, say, the coefficient of the 4th and 6th power, what else would you expect?
Wolfgang Hugemann
ixussuxi
Posts: 2
Joined: 2011-11-28T18:07:54-07:00
Authentication code: 8675308

Re: Lens distortion documentation

Post by ixussuxi »

Thank you for the very prompt replies!

Fred - the available databases are actually pretty sparse. I am using Pentax and Olympus, which probably don't get the same attention as Canon and Nikon. But even then Wolfgang's documentation (especially the hugin trick) is enough to find the parameters fairly painlessly. A potential problem with existing databases is that there is no single standard correction model, so the interpretation of the parameters may vary. BTW - I am really impressed with your IM scripts - a great resource.

Wolfgang - OK, I see where you are coming from now on the zero parameters. I should look at the theory in more detail, but my first reaction is that the a, b, c, d parameters have nothing to do with lens theory - they are just fitting a 4th order polynomial to the observed distortion, probably by least squares. This is NOT the same as a Taylor expansion, which I suspect is the power series used in the theory.

As suggested I went out to photograph a block of flats :) Then feeding a, b, c straight from hugin into IM's barrel distortion filter (omitting d) certainly appears to work. I have IM 6.6.9. The lens (17mm Olympus M Zuiko) has obvious barrel distortion which appears to be well corrected by the calculated values: 0.025193, -0.095186, 0.048505. (I see these are close to the ratio 1:-4:2, but assume this is just coincidence.)

My older lenses do not have such bad barrel, and this is my first calibration attempt, but am impressed enough I might do some others. I use a python script to assemble a processing pipeline, starting with dcraw, and it is easy to add this extra step.
Post Reply