Creating lens distorsion models with Hugin’s lens calibrator
If your lens is missing in LensFun database, you can create it thanks to lens calibration app from newly released version of Hugin, a free panorama stitcher. Learn how to do it.
The by far most popular library for fixing lens distorsion is LensFun which relies on panotools model. It's used in darktable, Rawstudio, digiKam, UFRaw and other digital photography apps for Linux. It works quite nicely, but the lens database isn't exactly complete, and there is no dedicated user interface to create new entries for this library. Which is why Andrew Zabolotny, principal developer of LensFun, suggested to everyone to just use Hugin.
Hugin lens calibration app is, therefore, a first step in the direction of providing such a user interface. Download Hugin here, build it, if you are on Linux, and run Hugin Calibrate Lens application. Here is how it works.
First of all, if you need to create a new or better (since some lens descriptions are very simplistic) lens distorsions model, you need a set of images shot with different focus distance (if it's a zoom lens). It can be a printed grid target shot from close distance, or a photo of a building with prominent horizontal and vertical lines.
You have to be careful about initial yaw, pitch and roll of the lens, because it's all about detecting horizontal and vertical lines. The image below is, for this very reason, a somewhat poor example, because it has crippled horizontal perspective. You can fix horizontal keystone in e.g. darktable, but then it's not a clean lens model anymore. Edit: Bruno comments below that this is actually not a big deal.

It's best to have several images loaded for better precision, but let's go for just one in this case.
In some rather rare cases the application won't read focal length and crop factor from Exif. You can easily fix it by setting those manually:

Once you have it loaded and set, you need to find a good combination for edge detection which is how the lens calibration app figures out what the horizontal and vertical lines of an image are. The principle is that the higher edge detection scale and threshold values are, the less edges you get.

Press Find lines button to run lines detection. Then you can switch to edge detection display mode in the top toolbar. This mode is easier for evalutation of detected lines. Here is what it looks like:

If some of the detected lines aren't good enough, single-click on them to mark them for exclusion from calculations. Clicked lines become red:

When you think you're done, click Optimize button in the sidebar to calculate a, b and c values for panotools distorsions model.

To try how it works you can switch display mode to Corrected and see if it works well. If it does, the next step is to create a new XML file in a text editor and copy the calculated values there, then save the file and send it to Andrew for inclusion in the next version of LensFun. Andrew provides a rather detailed manual on creating XML files for LensFun database. Please refer to it for details.
When it's done, you can locally patch an existing database. The XML files of the database are stored in /usr/share/lensfun/ directory on Linux. Then your digital photography application will pick the updated database and will be able to fix distorsions for your lens.
The article logo is based on artwork by Henrik Andersson of fellow darktable project.

Good article. Though you don’t have to be careful finding a subject with horizontal and vertical lines, the lines can be in any direction and the photo can have perspective distortion - The only requirement is that they are straight in reality.
(technically the optimiser adjusts the distortion so that the lines fit great-circles when projected onto the virtual sphere).
Also, to do this properly the tool allows you to process several photos at the same time, you will get much more reliable parameters from the optimisation this way.
Patches that output the lensfun XML format directly are welcome.
Nice PIC. Good Article Thanks for Sharing.
Good article and good to know that calibrating a lens is getting that easy now. But unfortunately the parameters I retrieved were not as accurate as the ones I got by the old control point method in Hugin.
For best results I will still follow this tutorial http://lensfun.berlios.de/lens-calibration/lens-calibration.html
@Sebastian
Oh, I didn’t even know you updated your plug-in in September! Do you know that there is a GEGL operation for using LensFun now?
Hi Sebastian, yes the ‘best’ way to get good lens-correction parameters is to stitch a panorama, preferably with a calibrated panorama head. But this is learning another skill and it shouldn’t be necessary, there are a lot of users who will never do this.
However, the lens calibration tool should give very good results if you use lots of photos simultaneously, i.e. if you just use one photo you might get lucky, but it could also go very wrong.
@Alexandre
I didn’t know the GEGL implementation. Looks interesting, though the GUI is quite simple and does not analyze the exif information. Anyway… a good starting point for a GEGL port of the plugin.
@Bruno:
I tried several photographs but did not succeed to get consistent results. But this could also be the fault of the images I used for calibration. I will take some more photographs from buildings with many straight lines and give it another try. Taking good images suited for reliable calibration is tricky with all the other methods as well.
For something a little different, I found this post very helpful:
http://support.bibblelabs.com/forums/viewtopic.php?f=94&t=15277&start=25
This post refers to this page: http://www.vintz.fr/lenscal/lenscal.html
I calculated a profile for the new Tamron SP 24-70mm F/2.8 Di VC USD lens. where do I send it?
Here below :
<lens>
<maker>Tamron</maker>
<model>Tamron SP 24-70mm F/2.8 Di VC USD</model>
<mount>Nikon F AF</mount>
<cropfactor>1.0</cropfactor>
<calibration>
<distortion model=“ptlens” focal=“24” a=“0.05013” b=”-0.1372” c=“0.07787” >
<distortion model=“ptlens” focal=“35” a=“0.00747” b=”-0.01777” c=“0.0066” >
<distortion model=“ptlens” focal=“50” a=”-0.04004” b=“0.15509” c=”-0.17226” >
<distortion model=“ptlens” focal=“70” a=”-0.02056” b=“0.07751” c=”-0.06572” >
</calibration>
</lens>
I have put some love into getting a distortion profile for the Panasonic “LUMIX G 14mm F2.5”
Not knowing how to submit it to the project, here it goes:
—-%<——-
<lensdatabase>
<lens>
<maker>Panasonic</maker>
<model>LUMIX G 14mm F2.5</model>
<mount>Micro 4/3 System</mount>
<cropfactor>2.0</cropfactor>
<calibration>
<distortion model=“ptlens” focal=“14” a=“0.0116” b=”-0.04802” c=”-0.00435” >
</calibration>
</lens>
</lensdatabase>
—-%<——-
Interesting, that’s two submissions already :)
Did you try registering and submitting a bug report with the file attached?
http://developer.berlios.de/bugs/?group_id=9034