Importing a Canon Raw file (.CR2)

This is a very top level question with no code examples attached. I have an interest in using Mathematica to read raw files coming from a Canon camera, before they are converted into jpg's, where data is lost. Are there any existing non proprietary mathematica scripts, which can read a CR2 file from a Canon camera and display its image as it would if a jpeg were imported. At the very least a script that can display data about the image stored in the CR2 file to include intensity, size, pixel value, LRGB histograms, etc...

Yes! I have done this before with RAW files.

Use the dcraw command line utility and convert the RAW file to a TIFF. You'll need to use appropriate command line options to prevent any processing of the data.

dcraw -o 0 -D -T -6 infile.cr2


It'll output a TIFF file with un-demosaiced raw sensor data. You can read that with Mathematica.

EXIF data can be extracted using exiftool.

What I don't have a solution for yet is how to put any (Mathematica-processed) data back into RAW files (say, DNG) so it can be read into a RAW converter.

• Unfortunately I already know how to do that part, but I do appreaceate the answer. I probably need to dig deeper into the RAW file format and go from there in trying to get mathematca ro read in the information. It will probably not be trivial, but there has got to be a way to do this in Mathematica. – Jon Feb 11 '14 at 22:31
• @Jon I don't really understand what you are trying to do then. There's a simple way to get all the data into Mathematica, by using an external utility. Why are you looking for a complicated way to do the same without this utility? You'd just end up rewriting parts of dcraw. If you need decent performance, you'd probably end up rewriting it in C and using it through LibraryLink. There's no built-in way to read any of the myriads of RAW file types in Mathematica. – Szabolcs Feb 11 '14 at 22:53
• Also, while I haven't worked with CR2, the situation is probably similar to Nikon NEFs: Nikon doesn't document their RAW file format. Though it's TIFF-based, there's enough difference that you can't get usable data with a TIFF reader in a simple way. If you're a company like Adobe who wants to include NEF support in their software, you need to use the NEF SDK from Nikon, which you will gain access to only after approval. Otherwise you'll have to reverse engineer the format on your own like the dcraw people did. – Szabolcs Feb 11 '14 at 22:57
• Presumably to interpret the raw data properly you also need things like a bad pixel map, nonuniformity correction data (per-pixel gain and offset), color mapping curves, and perhaps various other things as well. How you extract that information from the camera firmware I have no idea, so while I suppose it's correct to say that information is lost in the conversion to JPEG, it sounds almost impossible to recover it from the raw sensor data without the correct manufacturer-supported software. – Oleksandr R. Feb 11 '14 at 23:06
• @OleksandrR. The bad pixel map and non-uniformity is corrected for already, but to produce a properly converted image afterwards you're right that you need the curves. Actually there are good reasons for reading the raw data. I used it for two things: looking at cosmic ray tracks (I needed non-demosaiced data) and for banding noise reduction. My camera has a pretty annoying banding at high ISO settings ... and I did manage to improve on it by tailoring the method for this particular camera. But I couldn't put back the de-banded data into a RAW file so I can use a proper RAW converter ... – Szabolcs Feb 11 '14 at 23:33