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 | pfshdrcalibrate -b 16 -x  | 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 | pfsoutimgmagick pfstmo_mantiuk06.tif
pfsin pfs.hdr | pfstmo_fattal02 -s 1 | pfsoutimgmagick 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.

Notes

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.

2009/07/27: Thanks to the help of an anonymous reader, the outputs from the tone-mapping step are now TIFF files; I hope this will improve the quality of the final image.

2011/01/20: I have solved a bug in the HDR creation (use "pfshdrcalibrate" instead of "pfsclam"), to produce much better results.

47 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.

Anonymous said...

using pfsoutimgmagick instead of pfsout solves the problem as well (and it allows keeping the TIFF format)

eduperez said...

@Anonymous:

I tried pfsoutimgmagick, and it works perfectly; many thanks for the tip!

Andrew Collier said...

hi,

this is a really brilliant tutorial. i have, however, come up against a snag. the steps:

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

require two codes which do not appear to come in the current distribution of pfstools (i am running the most recent ubuntu with pfstools-1.8.1). they are also not listed in the documentation on the pfstools site. have they been superseded by other applications?

thanks,
andrew.

eduperez said...

@Andrew Collider:

Both dcraw2hdrgen and pfsinhdrgen can be found inside the pfscalibration package (available at the pfstools site). As far as I know, that package is not this distributed with Ubuntu; I am afraid you will have to compile it yourself (my Fedora binaries will not work, either). Feel free to ask if you need any help!

Hope this helps.

Cartola said...

Hi! Many thanks to this valuable tutorial! I was using FDRtools in windows as I had not found a good option to run on my main FreeBSD machine. All of this can be done on FreeBSD also. I can help FreeBSD people with some hints:

To have all mentioned programs you have to install pfstools via ports, making personal configurations. My port configuration have the following options selected:

- EXR
- TIFF
- QT
- IM
- GL

The port installed the version 1.6.4, which worked ok although the actual release is 1.8.1.

You have also to install the pfstmo port/package. This one didn't require any personal configuration, so you can install from packages.

Other general hints for those who don't use raw files:

- I changed the first step to three conversion steps, what I did with ImageMagick's "convert" tool:
convert IMG01.JPG img01.tif
- Then I got the hdr with this command:
pfsintiff *.tif | pfsclamp --rgb | pfsout pfs.hdr

Well, I am still eager to test HDR with raw files. I hope it make some difference. I did't know the RawTherapee tool, which is also available as a port to FreeBSD. I did some tests with raw files on windows and did't found difference from working with JPG. Maybe I have to teach my eyes :)

I also didn't have a very good experience with qtpfsgui

I am trying to use ImageMagick to do what you did with GIMP. By now I got to combine the enfused with 50% of the mantiuk and its ok:

composite 01-enfused.tif -dissolve 50 01-pfstmo_mantiuk06.tif teste1.jpg

but I still didn't realize how to make an overlay with the fattal image. Change GIMP with ImageMagick would make the process scriptable.

Many many thanks again!

Carlos Eduardo.

Anonymous said...

In your "Converting to TIFF" section, you say that "...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)." Where exactly did you find these values?

eduperez said...

@Anonymous: Those values come from somewhere at the UFRaw documentation, but I cannot find it any more. I _think_ that newer versions of UFRaw automatically set those values, but I am not sure.

Hope this helps!

Anonymous said...

Great tutorial.. Thanks a Lot for sharing

Brian Innes said...

Thank you for this tutorial Edu Perez, I have found it very useful indeed as I have recently started experimenting with HDR photography on linux!

This workflow is certainly a lot faster than setting up the HDR and then tonemapping using qtpfsgui. Also it lets me adjust the output easily using the layers in Gimp!

Interestingly I used the same values for gamma and linearity for my Pentax K100D as you used for your Canon 400D!

Again, thank you Edu!

eduperez said...

@Brian Innes:

My pleasure, I'm glad to know this was helpful!

Guy Stalnaker said...

Thank you for this workflow. I confess I've actually been unable to get it to work (my tonemapped images are either completely black or completely white for some reason). But, I was able to get Vincent Tassy's script to work as expected. I'm running with debug enabled to get a better understanding of where I must be entering incorrect information on the command lines. LOVE the command line--that's how to figure out what's going on the best.

DiFermo said...

A big THANK YOU.

Here is my first result:

http://www.flickr.com/photos/difermo/6605272803/in/photostream

Redsandro said...

Very nice indeed!

You mention GIMP for touchup before using RawTherapee though, but GIMP, at least my Xubuntu 11.10 install, does not do 16 bit images. It converts to 8 bit, kind of defying the purpose of RawTherapee.

eduperez said...

@Redsandro:

Yes, GIMP can only work with 8 bits (it happens with all versions), and ideally we would like to have a full 16-bit workflow; but right now I do not know of a better alternative.

Sometimes I do part of the corrections in RT, then go to GIMP for the layers, and then go back to RT for the final touch... still not a perfect solution.

klo said...

Thank you for such an awesome post! You've made my life so much easier :-)

Anonymous said...

Thanks sooo much!

Anonymous said...

You might try cinepaint instead of gimp. It claims to handle 16 bit tiff.

Anonymous said...

I know you said you had a 400d but if since then you've up graded to a 500d or above you may be interested in Magic Lantern. It upgrades the firmware in the camera. Advantages are many but of interest to you might be support for up to 9 HDR exposures with a larger range of EV. Also it supports a focus racking allowing multiple images to be taken with a very slight focus adjustment between each (configurable). That allows the camera to do all the focus racking internally without touching the camera between exposures. Even more it supports remote triggering without additional hardware by sound (clapping, gunshot etc) or waving near the eye sensor on the back of the camera.

eduperez said...

@Anonymous:

I tried Cinepaint some time ago, and I still use for some very specific tasks; but it is too slow and lacks lots of features from GIMP.

@Anonymous:

Or, if you still have a 400D, you could try 400plus, that I happen to be the maintainer.

Redsandro said...

I am playing around with tools and I was wondering, is there a particular reason you are using:

pfsinhdrgen | pfshdrcalibrate | pfsout output.hdr

In stead of:

align_image_stack -o output.hdr *.tif

Since you are using align_image_stack anyway?

eduperez said...

@Redsandro:

I tried to use align_image_stack to produce the HDR files, but the results were not good: all the final images had overexposed zones that I could not explain; this did not happen with the same images using PFStools.

Anonymous said...

Why exactly do you use RT for final retouch of TIFF files? I thought RT was a RAW developing program only, and when not working in RAW realm GIMP would be best suited...

eduperez said...

@Anonymous:

Yes, RT's original purpose is to develop RAW files, and GIMP is better suited for retouching. But I prefer to use RT to do color and curves corrections on the 16bit file; RT can do this perfectly, and GIMP cannot handle the 16bit files.

Ari Makela said...

Thank you for a very useful blog post!

I just noticed that Darktable 1.1 RC1 can do HDR, too. I haven't tried and I've have very little expericence with Darktable, but I am going to have a close look at it.

Ilia Tyker said...

Very nice article. Lots of great information.
Did you considered Photivo raw processor?
It is amazing piece of software to process RAW images.

eduperez said...

@Ilia Tyker: No, I have never use Photivo, but have heard good comments about it; I must give it a try someday. Darktable is another alternative in my list. Many thanks!

Herman Fries said...

hey!

its also possible to use luminance-hdr. It is a nice GUI which uses for example align_image_stack

Ilia Tyker said...

Did you look at Photivo? Very good raw processor.

Anonymous said...

thank you for the tutorial.

but y am running ubuntu 13.04 and does'nt find a .deb packet for installing pfscalibrate to have commands like pfsinhdrgen and pfshdrcalibrate or pfsclamp.

I tried to compile source pfstools 1.8.5 but there is lot's of depends who y can not found for ubuntu like imagemagick or jpeg-hdr.

and when will pfscalibrate will be able to run for ubuntu.

Anonymous said...

finally y have loader pfscalibrate 1.4 and the compilation has been easy to generate and the commands are very good.

Anonymous said...

Hi eduperez, im a beginer in images analysis i just want to convert from tif format into hdr format but i understood that it can be possible with pfstools. i've downloaded and intalled it but till now don't know how it works
thanks in advance

Post a Comment