2009/02/15

HDR and Linux

Este artículo también está disponible en Español: HDR en Linux.

It has been several years now since I decided to completely delete MS-Windows out of my computer at home. Now I use Linux exclusively; this operating system has given me a great flexibility and freedom in my tasks, but it has forced me to search for an alternate workflow to work with my photos. In this article, I will explain my current workflow when dealing with HDR's, using Linux and native software only.

Preparing the shots

Unless there is a good reason not to carry a tripod, I always use one when doing HDR. There exists software that can align the images (more on that later), but the algorithm is not perfect, and sometimes it cannot correct all the differences between shots; in those cases, halos will appear on the final result. When I cannot use a tripod, I activate the exposure bracketing option in my camera, selecting the maximum range that it can do (3 exposures at -2EV, 0EV, +2EV), and shot in a quick sequence.

Using a tripod, on the other hand, allows me to be more creative about the exposure time: for night photography, for example, I usually set the camera at "bulb" mode, put the diaphragm at f/16, and do nine shots between one second and four minutes, doubling the time between shots.

What I always do is shot using RAW, instead of JPEG; partially because RAW format retains more information, and partially because I prefer to do the corrections later at the computer.

Unloading the images

Obviously, the first thing I do back home is unloading the pictures to the computer. I could use a card reader, but Compact Flash cards have flimsy connector (in fact, one of the pins in my reader broke some time ago).

I connect the camera with an USB cable, and download the pictures using gThumb. As soon as I connect the camera to the computer, it is detected and a new window appears in my desktop, where I can select the photographs and download them. This software puts each set of files in a different folder, named according to the date and time they where downloaded; this feature is very convenient to me, because I can use that folders later, as sets pending to be processed. When there are HDR shots in a set, I place each one of them in a separated sub-folder.

Converting to TIFF

Next step is converting the RAW files to TIFF format. I choose lossless 16bit TIFF files because it is the best method to preserve all the information contained in the images. I use UFRaw for this task; from the directory which contains the RAW files (CR2 extension in my case), I do the conversion with the command:

ufraw-batch --wb=camera --gamma=0.45 --linearity=0.10 --exposure=0.0 --saturation=1.0 --out-type=tiff16 --overwrite IMG_????.CR2

That command produces a TIFF file for each RAW file found in the folder; the values of "gamma" and "linearity" are specific to my camera (a Canon 400D, you should consult the program documentation to find the correct values for yours), the rest are pretty standard.

Aligning the images

In case I could not use a tripod, I must align the images generated above. The fastest method I have found is by means of another program distributed with Hugin:

align_image_stack -a AIS_ IMG_????.tif

That command will generate files called AIS_????.tif, which I rename back to IMG_????.tif; that way I do not need to adapt the next step.

Exposure merge

Before entering the HDR realm, I use enfuse to do an exposure merge:

enfuse -o enfuse.tif IMG_????.tif

This software does not try to make a mathematical reconstruction of the scene, as the HDR technique does; instead, what enfuse does is grab the best parts of each image. And it works quite well: in most cases, the result is completely satisfactory, and I do not need to continue with the rest of the process.

Generating the HDR

When I am not satisfied with the result, or when I want to give a more surreal appearance to the picture, I step fully into HDR. First, I need an HDR file, which will contain a reconstruction of the actual light from the original scene. Two programs are required for this step, both of them included in PFSTools:

dcraw2hdrgen IMG_????.CR2 | sed -e "s/CR2/tif/" > hdrgen.txt
pfsinhdrgen hdrgen.txt | pfsclamp --rgb | pfsout pfs.hdr

I have an HDR file now, called pfs.hdr, which cannot be edited directly; instead, I have to bring it back to the LDR realm...

Tonemapping

There are several 'operators' to do the conversion from HDR to LDR; after trying several of them, I selected two: Mantiuk06 y Fattal02.

pfsin pfs.hdr | pfstmo_mantiuk06 -e 1 -s 1 | pfsgamma --gamma 2.2 | pfsout pfstmo_mantiuk06.tif
pfsin pfs.hdr | pfstmo_fattal02 -s 1 | pfsout pfstmo_fattal02.tif 

The version produced with Mantiuk is quite usable; in fact, the results resemble those produced by enfuse. The version produced by Fattal are too strong for my tastes; however, I have found a method to use them.

Layers

Now, I merge these three versions of the image (enfuse.tif, pfstmo_mantiuk06.tif y pfstmo_fattal02.tif) using GIMP. First I place the enfuse version at the bottom; on top of that, I put the Mantiuk version, with an opacity of 50%; and then, at the uppermost layer, the Fattal version, in "overlay" mode.

If I am not satisfied with the results, I generally tweak the levels and the opacity of those layers, until I obtain a good result. I also use GIMP to clone any minor defects that the photos may have.

Then I export the resulting image, again using TIFF to preserve as much detail as possible, and I move on to the next step.

Final retouching

For the final retouching (color correction, sharpness adjustment, contrast and brightness increase, noise reduction, ...), I use RawTherapee; and then export the results to the final JPEG file.

Some links

These are the homepages of the software mentioned in this article:

  • gThumb: Basically, this is a program to capture photos from a camera and manage them into folders. It has been integrated into Gnome, and comes within most Linux distributions.
  • UFRaw: I used this one for a while, as a RAW converted, but I feel that there are better options available; now, I use it exclusively to make batch conversions. It is also available with most major Linux distributions, even thought not all of them install it as a default.
  • Hugin: In my humble opinion, this is the best software available to do panoramas, perspective and lens corrections, and lots of other fancy things to images. In this article, I have used Hugin for a very basic alignment on images. This is also available for most major Linux distributions; but, in this case, I compile the sources myself, which can be obtained directly from the project's web page.
  • PFSTools: The reference in HDR world; it is a set of command line tools, that comprehends all the steps involved in HDR photography. There is a GUI version, Qtpfsgui, but I do not like it.
  • GIMP: Who has not heard about GIMP? Quite similar to PhotoShop, but lacking some functionality; however, amateurs like me have more that we need with GIMP.
  • RawTherapee: My current Raw editor of choice. This is the only non-free-as-in-freedom software that I use, but at least it is free-as-in-beer.

There is a second part to this article at: Script to make HDRs with Linux

2009/07/27: I have updated this article, to enhance the tonemapping step, using parameters that produce a better result.

16 comments:

h67cTYEswfD6LWTCAYpEq0vAJ0Zue3qK said...

Excellent tutorial, thanks :)

Lolo said...

Very good article

Faisal said...

Very good tutorial.
I have been an ubuntu user for almost two years now but after I recently got canon 450d, i had to switch back to windows to user its default DPP.
Can you tell me how that compares with the ones available in Linux? Can I fully give it up and use e.g RawTherapee

eduperez said...

I am glad to know these lines have been useful.

I cannot compare RT with DPP, as I have never used DPP. In my workflow, RT does everything I expect from a RAW converter; and about the quality of the final output, some comparisons are favorable to RT.

You can always try RT, it is free, and works both under Windows and Linux!

Faisal said...

Sure I will, I may be able to get rid of windows forever :)
Thanks a lot!

Anonymous said...

You can play with some parameters of hdr tonemapping in qtpfsgui.

Ghostly said...

This has been the most useful and helpful tutorial I have read. I was never very happy with HDR results, and this method provides the desired look I have been trying to obtain for ages. Many thanks!

Arun Mallikarjunan said...

I had been wanting to step into HDR for a while now, this will be my motivating factor. Thanks for putting this up.

eduperez said...

@Anonymous:

Efectively, Qtpfsgui has some parameters that can be tweaked... too many of them for my tastes, actualy. With this method, I just have two sliders: the oppacity of the Mantiuk06 layer (which controls the "textures"), and the oppacity of the Fattal02 layer (which controls the "colors").

BTW, sorry for the late response.

Dak said...

Very interesting article!
I've tested using Raw files generated by my Nikon D60. I think the best parameters to convert from raw to tiff are:
-gamma=1.0
-linearity= any value (I tested from 0.10 to 0.90)
-exposure=1.0
Had anyone find better solutions?

erra said...

Thank you for very interesting article. I've translated it to Russian and put it to my blog, with proper credits to author.

Anonymous said...

RawTherapee: This is the only non-free-as-in-freedom software that I use...

Guess what? The author just released it under GPL!!!

eduperez said...

@Anonymous:

Yes, I am aware, I have been following their forum. There was a lot of expectation before the announcement, and there is a lot of movement there now.

Great news.

Tudor said...

Great tutorial, thanks for sharing! I had however trouble with the pfsout command, namely the tiff files can not be read with GIMP. Shouldn't the commands in the Tonemapping section output jpegs? I noticed that this is what you actually do in your script.

eduperez said...

@Tudor:

Yes, I also had problems with the TIFF files created by pfsout, and thus decided to move to JPG later (PNG looked worse, IMHO); this article was written before I began experiencing the issue.

As others still use TIFF, perhaps there is something wrong on my part...

Anonymous said...

Your photos are in another realm entirely.

Post a Comment