Category Archives: MATLAB

Film-like tone mapping

Every once and a while someone will ask me why I still use film. First of all, I have a film camera, and to buy a new one would be a significant chunk of cash. But that’s not the main reason. I like the look of film; I know how the various emulsions work and know what I need to do to get the best image from them; and (above all) I like the fact that I have an artifact when I get my film back from the lab.

Digital does have a lot of advantages, though. It provides instant feedback at the point of making the photograph. It’s also hard to beat the speed and flexibility of a digital workflow. And for 35mm-sized sensors, it’s hard to tell the difference from film.

This article mixes the look of film and the enormous possibilities of digital. It also continues the trend of notes about high dynamic range images that I’ve been posting recently.

Color film is an analog tone reproduction operator. It renders a vast range of illumination values to a particular color image. That is, it maps the high dynamic range world to a lower dynamic range representation. The major factors that influence this tone mapping “function” include the exposure settings (shutter time, aperture, and ISO speed), the film response curves, and development technique. A film response curve for Fuji Velvia, the color film I use, appears below. Notice how the curve is more-or-less flat in the highlights and shadows and that it has an S-shaped curve in between.

Velvia film response curve

If you take some liberties with that S-shaped part of response curve where the real detail in the image is, you might almost say that the curve has a flat part for the shadows, a flat part for the highlights, and a line that connects the two.

When I first started puttering around with high dynamic range images, I began by translating what I knew about how film renders a scene with a lot of tones:

  • Film is sensitive across a particular exposure range.
  • Parts of the image with less than a certain amount of exposure are rendered as black.
  • Overexposed parts of image are rendered as white (technically clear).
  • You can’t change the film sensitivity (except with some processing tricks that we’ll ignore).
  • You can change the amount of light that is recorded during the exposure.

It wasn’t hard for me to combine this information with the generalization about the film response “curve” to produce a very simple tone-renderer which simulates color slide film in MATLAB. You can download the M-file from my page on MATLAB Central, where you can find some other good stuff. (I’ve also reprinted it below.) I ran it on the HDR image of my office, varying the exposure from 0 EV to 12 EV in half-stop increments, and was very glad to see the same kind of results that I do with film.

My office rendered from HDR to LDR in 25 steps
(Click for larger image…)

But film is so twentieth-century. :-) So I went on to more interesting forms of tone mapping.


function rgbSimulated = simulateFilm(rgbRadiance, nStops) %simulateFilm   Perform film-like tone mapping. %   LDR = simulateFilm(HDR, middleEV) converts the floating-point high %   dynamic range image HDR to a UINT8 low dynamic range image LDR using a %   method that recreates the sensitivity of film.  The middleEV value sets %   the middle exposure value (EV) for the rendering.  Larger EV numbers %   will generate brighter images; smaller values of middleEV will result %   in darker images. % %   The film sensitivity is set to 6 EV, which is comparable to many %   late-model transparency (positive) films. % %   Example %   ------- % %   Render the same HDR image using different "exposure settings." %   Essentially, simulate exposing the same scene from 0 to 12 EV in 1/2 %   stop steps. % %      % Read the HDR image and create a buffer for the rendered images. %      hdr = hdrread('office.hdr'); %      s = size(hdr); %      ldr = ones(s(1), s(2), 3, 25, 'uint8'); % %      % Perform the tone mapping. %      for p = 0.5:0.5:12.5 %          ldr(:,:,:,p*2) = simulateFilm(hdr, p); %      end %      figure; montage(ldr) % Author: Jeff Mather % Copyright 2006-2007 The MathWorks, Inc. % Set the film sensitivity and the mid-tone log-radiance. sensitivity = 6; midPoint = 3; minLogExposure = midPoint - sensitivity/2; maxLogExposure = midPoint + sensitivity/2; % Film works in stops.  Convert radiance to base 2 to compute perception. % Values that are outside the film sensitivity are lost (min or max). rgbRadiance = rgbRadiance * 2^(nStops); rgbSimulated = rgbRadiance; rgbSimulated(rgbRadiance ~= 0) = log2(rgbRadiance(rgbRadiance ~= 0)); rgbSimulated(rgbSimulated < minLogExposure) = minLogExposure; rgbSimulated(rgbSimulated > maxLogExposure) = maxLogExposure; % Convert to RGB. rgbSimulated = uint8(255 * (rgbSimulated - minLogExposure) ./ ...                      sensitivity);
Posted in Color and Vision, Computing, Fodder for Techno-weenies, MATLAB, Photography | Leave a comment

Ask Dr. Color’s Assistant: Tone-mapping in MATLAB

How does the tonemap function in the Image Processing Toolbox product work?

Tone mapping, or tone reproduction, compresses the enormous amount of illumination data in a high dynamic range image to something more suitable for output on a medium that has a lower dynamic range. The recent discussion of my office image shows the results of tone mapping.

But what is dynamic range? And what makes a high dynamic range image?

In the simplest terms, dynamic range is the ratio of the brightest value in an image or scene and the darkest value. Consider the following table of illumination sources from High Dynamic Range Imaging by Erik Reinhard, Greg Ward, Sumanta Pattanaik, and Paul Debevic:

Condition Illumination (in cd/m2)
Starlight 10-3
Moonlight 10-1
Indoor Lighting 102
Sunlight 105

The human visual system is capable of discriminating about five orders of magnitude at most of these different illumination levels.[1] That is, if you have normal vision, you can see something with a brightness value of 1 and a value of 100,000 in the same scene. Under different conditions, you might be able to make out detail in something with a value of 0.001 at the same time that you look at an object with a brightness level of 100. What you can’t do is see detail in something with a value of 0.001 and another thing with brightness 100,000 simultaneously. That would require being able to distinguish between eight orders of magnitude at once.

An image with values that range from 1 to 100,000 has a dynamic range of 100,000:1. Most of the images made for display on contemporary monitors have a dynamic range of only 256:1 per color channel, because that’s all that most monitors are built to support. We’ll call the latter images low dynamic range (LDR) images and anything with the ability to store more illumination detail a high dynamic range (HDR) image. There’s nothing stopping us from creating HDR images, but we won’t be able to display them on a LDR device like a monitor or inkjet prints.

Tone mapping is the HDR to LDR conversion. There are many tone mapping operators that we might have implemented. (See “A review of tone reproduction techniques” (2002) by Kate Devlin for an overview of these technique.) We chose a technique that renders floating-point high dynamic range RGB radiance maps to low dynamic range RGB UINT8 images using a spatially uniform tone mapping operator similar to what Ward et al. described in their 1997 paper “A visibility matching tone reproduction operator for high dynamic range images” (IEEE Transactions on Visualization and Computer Graphics, 3(4):291-306). It differs in several ways, though.

In a nutshell, our tonemap function takes the base-2 logarithm of the RGB radiance map to mimic the human visual response to brightness, transforms the image into the L*a*b* colorspace, and performs contrast limited adaptive histogram equalization on the luminance channel without changing the overall color content of the image. After converting the modified L*a*b* image back to RGB, the result for most images is a fairly good LDR image suitable for output. We provide a couple of parameters to improve the “artistic” qualities of the rendering – AdjustSaturation and AdjustLuminance – as well as the ability to tune the histogram equalization.

While our technique doesn’t use much of what is known about how the human visual system performs tone mapping, we think we picked a method that balances good results, acceptable performance, and low complexity. My hope is that people doing work on HDR imagery will take a look at the two-dozen so important lines inside tonemap and see just how easy it is to get started implementing their own tone reproduction operator in MATLAB.

[1] – One “order of magnitude” is one power of ten. Engineering types are fond of talking about orders of magnitude.

Posted in Color and Vision, Computing, Fodder for Techno-weenies, MATLAB, Photography | 2 Comments

New MATLAB-related article

Curious how I spend my days at work? Well, my new article Two New Functions for Converting Datatypes and Changing Byte Order pretty much sums it all up. Create small applications in MATLAB and C to access file formats. Write tools to make working with those files easier. Write the occasional article about tool-building for others toolmakers.

Oh! And meetings. Can’t forget those. . . .

My other recently published article, Color Management and Color Transformations in MATLAB, shows the work that isn’t related to formats that I do at the office. Currently I’m somewhere between that stage where I think I know what I’m talking about and actually knowing it.

Posted in Color and Vision, Computing, MATLAB | Leave a comment

MATLAB + Doom = Future engineers

“The next-generation engineer will use the 3D control scheme he has grown up with.” That’s how Jörg Buchholz introduced his Doom 1.1 MATLAB code that lets data visualizers “fly through a 3D scene like in a first-person shooter in god mode.”

Are shooter games the future of data visualization? Maybe not, but the entertainment industry will play a role.

  • Radiologists are awash in so much data than they have trouble reading “films” in the traditional way. SCAR‘s Transforming the Radiological Interpretation Process (TRIP) initiative draws upon expertise from Hollywood for—among other things—novel visualization techniques and data navigation tools.
  • When I visited the Chicago Mercantile Exchange on a break during a business trip, I saw a trader using what looked like a PlayStation game controller. A Baseline Magazine article explains why game controllers are useful for trading: “[The Exchange is pursuing] new markets such as trading arcades, small venues in Dublin, London and Gibraltar that host a new generation of traders who can use joysticks instead of computers to swap financial derivatives. ‘They trade as if it were a video game,’ says Steve Goldman, director of network architecture at the exchange, holding up a PlayStation joystick. ‘And we want to make sure they have something to trade here.’ . . . These traders follow set systems and algorithms and chase seemingly minuscule market moves. Under these systems, rapid hand-eye coordination is critical.”
Posted in Computing, MATLAB | Leave a comment

Color Management in MATLAB

Earlier this week The MathWorks published my article Color Management and Color Transformations in MATLAB. Enjoy!

Posted in Color and Vision, Computing, MATLAB | Leave a comment