Not long ago I was working on an HDR algorithm modeled on linear camera data and needed to work with digital sensor data directly. Though this is possible with RAW image files, I found that getting the sensor data from a RAW file into MATLAB was not obvious, my displayed results had terrible color, and the images were intrinsically too dark for standard display. Googling for "How to process a RAW image" frustratingly yielded only Photoshop tutorials so, after much research, I wrote the following guide. - RS

Reading RAW files into MATLAB and Displaying Them

This guide is composed of two complementary aspects:
The process described is a first-order approximation of the steps that all cameras do to take an image and produce a viewable output. It starts with the raw sensor data and implements:
  1. Linearization
  2. White Balancing
  3. Demosaicing
  4. Color Space Correction
  5. Brightness and Constrast Adjustment for Display
The algorithms proposed in these steps are not particularly advanced, but rather are intended to be an introduction to what is required in this process (and why). They are largely based on the approach taken by dcraw, the best open resource I could find on RAW image processing. Though this information may be common knowledge in industry, I have found such references in academia and on the web in general to be sparse. Hopefully this fully self-contained reference will help.

Processing RAW Images in MATLAB, 2013 - PDF


The following files implement the code found in the paper. It is intended to be instructional and easily followed, therefor it is neither optimized for speed or memory usage, nor is it implemented as a function to simply read RAW files for your other work (though it could be easily converted into one). - All files listed below plus the PDF - 31MB.

raw_process.m - Main script implementing code from the paper.

wbmask.m      (supplemental functions)

camera_matrices.txt - Direct copy of the table of XYZ-to-Cam conversion matrices for many camera models from the dcraw source code. Each entry is formatted as {"Camera Model", <black>, <saturation>, {<9 ints>} }, where the last entry is an array of row-wise entries of the matrix, times 10000.

(Test images - Note these must be converted to DNG or TIFF as per the guide. I could supply such versions here, but then you'd miss out on that important step!)

banana_slug.cr2 - Canon test image, 15MB, taken by me.
nikon_testim.nef - Nikon test image, 14MB, from, where others can be found for many various cameras.

(The following dcraw resources (or some equivalent) are only necessary for those on Linux or with MATLAB older than r2011a).

dcraw_win.c - dcraw source code unedited except for the addition of the few defines necessary for error-free compilation with Visual Studio 2008 or newer, as described in the paper, if you want to compile yourself.
dcraw.exe - A compiled 32-bit Windows executable of the above dcraw source.

Change Log


Related Links

Steve on Image Processing - Origins of the MATLAB DNG read-in method used.
Inside the CR2 Format - Great detailed analysis of the CR2 file structure and the how dcraw reads and processes it.
Manpage of dcraw
Adobe DNG Converter Downloads
DNG Specifications - Information about all the Exif tags in a DNG, plus instructions for how to linearize a DNG and more advanced ways to color correct a RAW file than explored here.