The Grumpy Editor's hugin experience

Benefits for LWN subscribers The primary benefit from subscribing to LWN is helping to keep us publishing, but, beyond that, subscribers get immediate access to all site content and access to a number of extra site features. Please sign up today!

The free software community has produced a wealth of tools for the manipulation of image data. For simple changes, such as cropping, resizing, or basic contrast tweaking, any of a number of programs can be used. More complex changes will require falling back to tools like the GIMP, krita, or cinepaint. Anybody who has tried to join together two or more independent images in those tools will have discovered, however, that certain manipulations fall into a class of their own. For that kind of work, hugin would appear to be the only choice. Your editor has long intended to play with hugin; the threat of having somework to do finally provided the necessary motivation.

The problem with just gluing two images together is simple to understand: lenses distort. Even the best lens will transform light differently toward the edges of the image than it does in the middle. Multiple images also suffer from parallax problems, even if the camera is mounted on a tripod. The result is that two overlapping images will not normally join together in a straightforward way - the pieces simply do not fit. Resolving this problem requires distorting the images in fairly tricky ways. The key to the value of a tool like hugin is not in putting images together; it is, instead, in the process of stretching and remapping those images (along with some other details like exposure matching) so that they can be put together. As an added bonus, the ability to correct lens distortion makes some other interesting applications possible.

The classic use of a tool like hugin, though, is the creation of panoramic images which cover a field larger than the camera can capture. A photographer wanting to create the best panorama should do a number of things to ensure that a set of images can be combined easily: the camera should be mounted on a tripod, and all settings should be manually selected and should be the same for every component image. A camera set for automatic exposure, for example, will vary that exposure as the camera is rotated to take the pictures; that will create differences from one image to the next. Changes in focus or depth of field will also complicate the task of properly stitching the images together.

That said, hugin does an impressive job of joining images which were not taken in optimal conditions. Feed it a set of handheld cellphone photos and you'll get something reasonable out.

To test hugin, your editor took a series of pictures of the continental divide from the eastern Colorado plains. They are not great pictures - it was not a particularly clear day - but they are sufficient to show what hugin can do. The individual images are:

These images present some challenges; among other things, the tripod was not entirely level, so the horizon appears to tilt from one to the next. Putting them together is clearly going to require some complex manipulations. The nice thing is that hugin manages to hide that complexity from the user - most of the time. For beginning users, there is an "assistant" mode which will step through the process relatively easily. There's also a nice set of tutorials which should really be required reading for any new user.

The first step is to bring the images into hugin; that is done with the usual GTK file-chooser dialog. Depending on the distribution being used, there may be an unpleasant surprise once the files have been selected. Your editor, testing the Fedora hugin package, got a dialog containing the following:

If you see this message then your version of hugin has been configured without support for automatic generation of control points. Probably your system administrator or Linux distribution did this because the SIFT algorithm used by autopano-sift and autopano-sift-C is encumbered by software patents in the United States of America.

Did your editor ever mention that software patents are a pain?

The message goes on to say that hugin remains a useful tool, even without the forbidden algorithm. And, indeed, it does, though the amount of work required is higher. The next step in the process is the assignment of "control points" which tie the images together. The tool presents a pair of images, and the user has the task of identifying points in each which correspond to the same location. The process can be a little painful, depending on the images involved, but it's not that bad, especially if there are a lot of easily-identified, small features to line up. It's just a matter of clicking on one image, adjusting the point, then doing the same thing on the other image. Hugin creates a small, high-resolution window surrounding the selected points which makes it easy to align control points with single-pixel accuracy.

Once a couple of points have been fixed, hugin will do its best to automatically find the corresponding point for a location picked in one image. Often the process works quite well; other times, not quite so well. Sometimes hugin's guess is simply wrong; other times it will conclude that it cannot find a matching point and put up an obnoxious dialog which must be dismissed. In the latter case, it would be better to just pick a nearby point (as it does anyway) and be done with it. Beyond that, though, the process is pretty smooth.

Then, one must go into the "optimize" area. This is where the friendliness of hugin comes closest to falling apart. "Optimizing" is the calculation of a set of parameters describing how the component images are related to each other and how they have been distorted by the camera; it is, essentially, a set of magic algorithms generating magic numbers. A user who doesn't really understand the math behind what hugin is doing (and, remember, we're dealing with photographers here) will have no clue what's happening or how to judge whether the process has worked properly or not. And it doesn't always work properly. The help from the tutorials can make things worse:

If you are lucky you will be able to select Optimize Positions, View and Barrel (y,p,r,v,b), hit Optimize Now! and finish the optimisation process in one go. Otherwise, if the optimiser reduces the field of view to zero, you will find that you have to just Optimize Positions first, before you can optimise the other parameters.

How does one know if the optimizer has reduced the field of view in this way? The screen will not actually say that. So the optimizer is the place where a somewhat naive user (your editor, say) is likely to grope around blindly in the hopes of getting something done.

After that, one can pull up the preview window to see what hugin plans to do with the images. The preview, too, can be confusing; mouse clicks on the image shift it around in ways which are entirely predictable (and even useful), but disorienting to a new user. Sometimes the program comes up with bizarre values for the actual area of the image, leading to a mostly black preview with the useful image data crammed into a corner somewhere. Solutions can include redoing the optimization process or going to the "stitcher" window and asking it to recalculate the image size parameters - including a couple of "field of view" numbers which don't have any clear meaning to the uninitiated. Things usually work, but it can be discouraging when they don't.

Once the preview looks good, the stitcher is invoked to create the final image. That process can take a while, but the end results tend to be good. Usually all that's required afterward is a quick cropping pass in a more traditional image editor to come up with something presentable. Here is your editor's final panorama (please note that the larger version is a 9MB image - and that's after reducing it considerably):

Your editor, being a daring sort of person, decided that he wanted to find out just what sort of functionality is being denied to hugin users by the oppressive US software patent regime. As it happens, Fedora users can get around patent-based repression by installing the autopano-sift-C package from the rpmfusion repository and tweaking the program preferences to use the real autopano tool. The difference is striking: with autopano-sift-C installed, the program proceeds immediately from image selection to a preview window; the whole "control points" and "optimization" process just sort of goes away. This package does a great job of finding control points, at least on your editor's sample image set. Software patents have cost Linux users a highly useful tool here; fortunately, users who are not affected by the American software patent regime can still obtain the autopano-sift-C package. Your editor would highly recommend doing so.

Beyond panoramas

Hugin's uses are not limited to the creation of panoramic images. The image distortion logic built into the program can be put to other uses as well. Consider this image from the 2008 Kernel Summit:

Your editor was constrained to take the picture from an off-center point of view - the professional photographer who was hired to do a proper picture had, naturally, taken the best spot. One might be tempted to point out that your editor's picture got out into the world, while the professional's has never really been seen, but your editor would never think of being so petty. What is worth pointing out here is that the off-center perspective, combined with lens distortion, results in a bit of a strange view; look at the visible bend in the beam at the top of the stage opening over the group of assembled kernel hackers. The sides of the opening also appear to not be parallel. It's a fairly classic case of distortion caused by the combination of an off-center perspective and a zoom lens being pushed to its wide-angle extreme.

It turns out that hugin can fix problems like this. To use hugin in this mode, the user feeds a single image to the application. The process of creating control points is now done a little differently; the task is to identify points in the same image which make up a horizontal or vertical line. Your editor indicated that the border around the stage really should be level and plumb, and picked a couple of other lines as well. Hugin then does its magic and comes up with a new image:

The lines have been straightened and the photograph looks more rectilinear in general. It's still not perfect, of course, and not even hugin can make Al Viro smile, but it's a step in the right direction. This technique can be used for fixing up the perspective on any of a number of pictures which are taken from a less-than-optimal location.

In summary: hugin would appear to be unique in the free software community. Despite the occasional glitch, hugin makes the execution of non-trivial image manipulations easy to the point that even your editor can do it; your average professional photographer should have even less trouble. It is an impressive piece of work, even though it has not yet reached its 1.0 release (version 0.8 came out in July). It definitely belongs on any Linux-using photographer's system.

