Lens Correction: LensFun, Hugin, and ImageMagick

0
827

One of the aspects of heritage digitization or photography with a scientific use orientation that is often neglected or subjectively estimated is the correction of aberrations produced by our lenses, or what is colloquially known as lens correction.

Lens corrections are essential to be able to make precise measurements on the image; moreover, they are an essential process and normally included in any panorama or 3D model creation process by photogrammetry, since otherwise any geometric estimation at the edges of our photograph would be clearly erroneous.

Since version CS5, Adobe integrated lens corrections among its tools based on objective data obtained from a particular lens, calling it “Lens Profiles” which can be obtained from their own databases, downloaded with the Adobe Lens Profile Download tool, or generated by ourselves with the Adobe Lens Profile Creator utility, tools that I will try to address in later articles (“Lens Correction with Adobe Lens Profile“), as they seem to me a good geometric complement to workflows with camera profiles.

In this article I will try to introduce some aspects of lens correction with Open Source tools.

Pathologies related to lens aberrations can be multiple, from geometric, chromatic, vignetting, etc., however, due to the frequency and, I think, practicality of the process, I will focus exclusively on the typical barrel distortion that occurs in most lenses when their focal length begins to drop below 40-50mm.

Barrel distortion is characterized when the straight lines captured in our image curve convexly relative to the optical axis of the image, caused by a certain increase of the lenses in the center compared to their edges, something very common in wide-angle lenses, which is taken to the extreme in fisheye lenses.

The simplicity of the algorithms for correcting this type of optical aberration, and the high design costs to optically compensate for this phenomenon, are not great incentives for the industry to put much effort into solving these types of aberrations in lenses.

This type of distortion is properly a radial deformation of the image which was described by Professor Helmut Dersh from the University of Furtwangen at the end of the 1990s. It is widely used in lens corrections of programs oriented to panorama creation or photogrammetry.

The function itself is a third-order polynomial where the distance of a pixel from the center of the original image (rsrc) is related to the corresponding distance in the corrected image (rdest)

rsrc = ( a * rdest3 + b * rdest2 + c * rdest + d ) * rdest

Introducing ourselves to this formula is fundamental to understand many of the tools intended to make estimations about this pathology so that many of them will work with the parameters a, b, c, and d presented in the previous formula. Such that a+b+c+d = 1, although this relationship is not always strictly fulfilled, so often only a, b, and c are used since d is easily deduced.

Obtaining the characterization factors a, b, and c

To obtain the parameters a, b, and c that describe the amount of barrel aberration of our lens, we can resort to two strategies:
On one hand, resort to LensFun, an important open-source database where these attributes are described for most optics on the market. LensFun is a database and a library, so if we download it, it may be confusing to obtain this information. However, if in the LensFun distribution we go to the directory “data>db”, we can see a good number of XML files with the names of the main lens manufacturers.

For example, my Nikon 35mm 1.8 DX lens can be found in the slr-nikon.xml file under the following format:

  <lens>
  <maker>Nikon</maker>
  <model>Nikon Nikkor 35mm f/1.8G AF-S DX</model><
  <mount>Nikon F AF</mount>
  <cropfactor>1.5</cropfactor>
  <calibration>
  <distortion model="ptlens" focal="35" a="-0.00899" b="0.02507" c="-0.04009" />
  </calibration>
  </lens>

Where the aforementioned parameters can be easily retrieved: a=”-0.00899″ b=”0.02507″ c=”-0.04009″. These parameters are quite precise, however, if our lens is not in this database, we can proceed to calculate these parameters ourselves.

For this, we can apply a second strategy by resorting to the well-known multiplatform open-source tool Hugin. This program is composed of several tools: on one hand the Panorama Stitcher, the Lens Calibration GUI, and the PTBacherGUI. Our calibration can be done either with Lens Calibration or with Panorama Stitcher; the procedures are as follows:

Lens Calibration GUI huginIf we use Lens Calibration GUI, the process is quite simple: first, we load an image containing a pattern of sufficiently evident lines for the application to detect them. At this point, I have seen certain problems; what worked best for me is a card with 4 lines forming a perimeter drawn on foam board, these lines I made with a marker about 3mm thick. In the options parameters, you have to adjust a bit until it fits our image; in my case, I used: Edge Detection Scale = 4, Threshold=3, Line Length= 0.1, and Image Size, the width of the image. If we then activate Find Lines and the tool has detected a certain number of lines, we can activate Optimize.
As can be seen in this tool, we will generate the parameters “a” or distortion, “b” or barrel, and “c” or distortion. In this tool, we can load multiple images containing lines and process them all together to improve the accuracy of the process.

Another somewhat more tedious procedure will be to use Panorama Stitcher so that first we load an image that, as in the previous case, contains a series of especially evident horizontal and vertical lines. Next, we go to Control Points and establish series of points on the left screen correlating them on the right screen; we must activate “auto add” and classify each point as a vertical or horizontal line or add a new line so that we have Line 3, 4, etc. Finally, we go to the Optimizer, in the dropdown menu we choose the last option and activate the corresponding parameters: distortion a, barrel b, and distortion c, so that if our points are well placed in the Camera and Lens tab, in the Geometric section we will have these values available.

puntos control optimizador

This procedure, although quite effective, as long as our test line card does not present distortions, as well as the shot itself, I personally have not achieved results as good as with the LensFun database.

Correcting an image:

Once these parameters are obtained, we can proceed to correct our images; for this, the easiest is to use the well-known suite ImageMagick. This powerful digital image processing suite is very easy to install since it is usually found in all repositories of any Linux distro, or on Mac OS X it is available via Ports. The tool we will need is called Convert, and like the rest, it is only accessible via command line, so we can use the following proposal:

convert DSC0005.tif  -distort barrel "-0.00899 0.02507 -0.04009" _DSC0005B.tif

Where we can easily observe the use of our parameters a, b, and c. In this case, -distort would also accept the fourth factor or “d” but it is optional.

Automating the process:

Since doing this process file by file, we can automate the work with a small script. A proposal to do it with PHP:

  <?php
  $a = -0.00899;
  $b =  0.02507;
  $c = -0.04009;
  $ruta = "/path to my directory/";
  if (is_dir($ruta)) {
  if ($dh = opendir($ruta)) {
  while (($file = readdir($dh)) !== false) {
  if ($file != ".." && $file != "." && $file != ".DS_Store"){
  exec("convert ".$ruta.$file."  -distort barrel '".$a." ".$b." ".$c."' ".$ruta.$file." ");
  }
  }
  closedir($dh);
  }
  }else
  echo "Invalid path"; 
  ?>

Then we can run the script from the terminal with:

php -f myscript.php

In this way, we can automate the process over large amounts of images without great effort. Finally, note that LensFun is already integrated into the lens correction options of RAW development tools such as Darktable or Ufraw