HDR imaging on the Nokia N900

This entry was posted on Apr 15 2010

Ever snapped a photo with a bright background and a dim foreground and either had the foreground too dark or the background blown-out?

Time for High Dynamic Range (HDR) imaging :D Its a feature that is far more at home on DSLRs, rather than lowly point-and-shoot cameras, let alone the Nokia N900. Of course, we can’t match the quality (or speed) of those DSLRs with their superior CCDs. On the other hand, it’s just downright amazing that you can actually snap respectable HDR photos with the N900 and automate the process to a high degree – right down to actually processing the HDR right from your own phone – as long as you have CPU cycles to spare..

New! 18th April 2010: More HDR photos taken with my N900

YMMV

Before continuing any further, some stuff I would like to say:

  • This article is very long :P
  • This article is mostly technical and intended for Nokia N900 users - although feel free to read on if you would like the vicarious experience of doing cool stuff on the N900 and learning a bit about HDR :)
  • If you prefer pictures instead of exposition, you can view all posts tagged with Exposure Bracketing.
  • Easy Debian must be installed for on-device processing of HDR images – the installation instructions can be found in the link provided. Alternatively, you can still snap the raw images with your N900, but do the actual HDR processing on your laptop or desktop using third-party tools. I won’t provide any help or support on any of these topics.
  • You need a tripod – I use a semi-DIY setup based on a Gorillapod and some other pieces of plastic. I might post pictures of it when I have the time.
  • Don’t expect too much from the N900 camera in low-light conditions – exposure control seems to be limited from around 33 to 33133 microseconds. Perhaps Nokia will remedy this in the fabled PR1.2 update.
  • I won’t be responsible if you somehow brick any of your devices (although its quite unlikely, given the general scope of this discussion).

Inspiration

The Nokia N900 is -the- dream phone for tinkerers and geeks – fully Linux, and almost completely open (there are some closed-source libraries in there). The community behind Maemo (the name of the operating system) is pretty active too – there is a lot of useful stuff that you can get from the forums, as long as you’re willing to sort through all the junk posts/threads. These two threads are of particularly interest for this post:

The first link describes the initial steps required to take HDR photos, accomplished via the expcomp library (source and binary provided by the original author) and an ASH script (BusyBox is the default shell on the N900). The expcomp library is a command-line tool to adjust the current exposure compensation value of the N900 camera. However, it has a few limitations:

  • You cannot set an absolute value for the exposure compensation. Adjustments are all relative to the current exposure compensation value of the camera – determined automatically by the in-built camera app when you point it around.
  • The soft limits of the gstreamer library which controls the camera are not taken into account. Apparently, the raw value must be somewhere in the range of 33 to 33133 – any larger or smaller value will simply cause gstreamer to cut-off at the minimum or maximum exposure level (even though expcomp returns a higher or lower value!)

These limitations can be easily overcome by rebuilding the script, since the author has kindly provided the source code. Nevertheless, I took a shortcut – bringing us neatly to the second link – my original source for the more powerful v4l2-ctl library.

The v4l2-ctl library provides a general-purpose command-line interface to gstreamer. Running ./v4l2-ctl -L will give you a run-down of the internal parameters which it can read/set. The one we need to use is called, strangely enough, exposure_time_us – I presume the us actually means μs i.e. microseconds.

HDR photography

As described in the original HDR thread linked above, taking HDR images is as simple as snapping 3 pictures with varying exposure values (EV). In short, one image will be overexposed (details in darker areas will be clearer), another one will be underexposed (brighter areas will not be blown-out), and the third one will be exposed – as Goldilocks put it – just right.

After the images are taken, we can use enfuse to process the image files and generate a single HDR image. To find out more about enfuse see this wiki page on PanoTools.org.

How-to: Installation

The following files are provided:

  • ./bracket-auto.sh
  • ./bracket-manual.sh
  • ./snap-images.sh (dependency for first two files)
  • ./v4l2-ctl (dependency for first two files)
  • ./hdr-enfuse.sh
  • ./completed.wav  (played by ./bracket-manual.sh on completion)

Download link: N900 Exposure Bracketing (md5sum: ef359a7a25bb8ce40ab6e9169970aed5)

  1. Download and extract the files.
    tar -xvzf bracketing.20100415.tar.gz
  2. Copy the extracted folder ./bracketing/ into a directory of your choice (I use /home/user/scripts/ for all my scripts)
    mv ./bracketing/ /home/user/scripts/
    Important!
    This folder should not be moved anywhere inside /home/user/MyDocs/
  3. Make all the files in the folder executable.
    chmod 755 /home/user/scripts/bracketing/*
  4. Create a directory called hdr in your phone’s MyDocs directory.
    mkdir /home/user/MyDocs/hdr/
  5. Optional: To do on-device processing of HDR images, install Easy Debianfor technical-minded users only!

How-to: Take pictures with ./bracket-auto.sh

This script will take three pictures of the same scene at the default EV (which is auto-set when the user initially activates the in-built camera application), and two other pictures of +2 and – 2 EV relative to the default EV.

Bonus: Installing the eSpeak CLI Speech synthesizer from the maemo.org Extras repository will give you spoken voice notifications while the script is working. It will also announce its own completion.

  1. Ensure that the camera slide is closed and that the camera application is not running.
  2. Open X Terminal. You can sudo gainroot if you like (the script can run as either user or root).
  3. Go to the directory where the scripts are copied and type (BUT DO NOT RUN!) ./bracket-auto.sh
  4. Mount the camera in your tripod (if it hasn’t been mounted yet), open the camera slide.
  5. The camera application should open. Half-press the shutter button to auto-focus the camera.
  6. When you are satisfied, close the camera application and bring up the X Terminal window in the Task Manager
  7. Hit the ENTER key to run the script (which you typed out previously but didn’t run)
  8. Wait for the script to complete – this may take awhile (can be within 1-3 minutes to my experience – it gets slower the longer I leave my phone on).
  9. You can find the image files in /home/user/MyDocs/hdr/<timestamp>/
    Example: /home/user/MyDocs/hdr/20100415135511/

How-to: Take pictures with ./bracket-manual.sh

This script allows the user to specify the minimum and maximum EV by pointing the camera at the brightest and dimmest areas of the scene. It will then calculate the average exposure, then take three pictures based on these recorded values.

Note: This script makes use of graphical notifications and must be run as user. This script will not run properly if you run as root (via sudo gainroot).

  1. Close the camera application if it is running.
  2. Start X Terminal. DO NOT SUDO GAINROOT.
  3. Go to the directory where you put the script and run ./bracket-manual.sh
  4. The script will now automatically open the camera application
  5. Point the camera at the brightest area in your scene, wait 1-2 seconds for the camera to automatically adjust its exposure, then half-press the shutter button. If you do it right, you will see an on-screen notification telling you the scene EV. You may release the shutter button at this time.
  6. Repeat the previous step, except this time you will point the camera at the dimmest area in your scene.
  7. After this, mount the camera in your tripod (there is no time limit on this, just be sure not to press the shutter button while mounting the camera).
  8. Half-press the shutter button again – you will a notification telling you to release the shutter button and close the camera application. You will have 5 seconds to close the camera application – just tap the close button in the top-right corner of the screen.
  9. The Task Manager should show up, you can tap the X Terminal window if you would like to see the process as it runs.
  10. Wait for the script to complete – it will beep (./completed.wav) 3 times (changed 2007-04-17: see updates at the bottom of this article) – this may take awhile (can be within 1-3 minutes to my experience – it gets slower the longer I leave my phone on).
  11. You can find the image files in /home/user/MyDocs/hdr/<timestamp>/
    Example: /home/user/MyDocs/hdr/20100415135511/

How-to: On-device HDR processing with ./hdr-enfuse.sh

Important! To use enfuse, you must install Easy Debian – depending on your level of technical expertise, this may be easy, or really really hard. You can still copy the raw image files out of your device and do the processing off-device (using GIMP, enfuse on Ubuntu, etc).

  1. Open Debian chroot.
  2. At the prompt, go to the directory where you copied the files.
  3. Run ./hdr-enfuse.sh <timestamp>
    <timestamp> must match the one of the sub-directory inside /home/user/MyDocs/hdr/ which contains the raw image files.
  4. Wait
  5. Wait
  6. Wait! (It can be freakishly slow some times :P )
  7. The file <timestamp>.jpg will be created in /home/user/MyDocs/hdr/ once processing is complete.

Updates

2010-04-17: Uploaded new bracketing script to fix a dependency issue with the play-sound command in bracket-manual.sh – the N900 does not come with play-sound – it comes with playsound :P This library is more limited, so the phone won’t beep 3 times, it will beep once.

2010-04-18: Added link to more N900 HDR photos. Script updated: Added fallback to dspjpegenc if the script can’t find pngenc in the list of gstreamer plugins.

2010-04-23: Fixed “dsp_thread: failed waiting for events” bug when the script falls back to dspjpegenc. If this problem still shows up, let me know in the comments.

<tt>

Post a Comment