File Exchange

image thumbnail

Read RAW camera images

version 18.913 (654 KB) by Emmanuel Farhi
Read RAW camera images from within Matlab, using DCRAW

25 Downloads

Updated 11 Jan 2021

From GitHub

View Version History

View license on GitHub

Read RAW camera images from within Matlab, using DCRAW

The use of this class boils down to simply creating the object. Then, you
may simply use the imread and imfinfo call as usual, and RAW files
will magically be handled.

Example:
--------

In the following example, we just call readraw once, and then all is done
with imread and imfinfo as you would do with other image formats.

dc = readraw;
im = imread('file.RAW');
info = imfinfo('file.RAW'); % this creates a file.tiff
delete('file.tiff'); % to save disk space
...
delete(dc); clear dc

NOTES:
------

NOTE: Each RAW file will be converted to a 16-bits TIFF one at the same
location as the initial RAW file. This file is then read again by imread
to actually get the image RGB channels. If you have created these files
(which are each 146 Mb), you may either remove them, or further access
them without requiring conversion.

Supported RAW camera image formats include:

- RAW CRW CR2 KDC DCR MRW ARW NEF NRW DNG ORF PTX PEF RW2 SRW RAF KDC

If you which to import the RAW files with specific DCRAW options, use the
readraw class method 'imread' with options as 3rd argument e.g:

dc = readraw;
im = imread(dc, 'file.RAW', '-a -T -6 -n 100');

and if you wish to get also the output file name and some more information:

[im, info, output] = imread(dc, 'file.RAW', '-T');

Some useful DCRAW options are:

-T write a TIFF file, and copy metadata in
-w -T -6 -q 3 use camera white balance, and best interpolation AHD
-a -T -6 use auto white balance
-i -v print metadata
-z set the generated image date to that of the camera
-n 100 remove noise using wavelets
-w use white balance from camera or auto

Methods:
--------

- readraw class instantiation. No argument.
- compile check for DCRAW availability or compile it
- delete remove readraw references in imformats. Then use clear
- imread read a RAW image using DCRAW. Allow more options
- imfinfo read a RAW image metadata using DCRAW

Credits:
--------

- DCRAW is a great tool <https://www.cybercom.net/~dcoffin/dcraw/>
- Reading RAW files into MATLAB and Displaying Them <http://www.rcsumner.net/raw_guide/>
- RAW Camera File Reader by Bryan White 2016 <https://fr.mathworks.com/matlabcentral/fileexchange/7412-raw-camera-file-reader?focused=6779250&tab=function>

License: (c) E. Farhi, GPL2 (2018)

Cite As

Emmanuel Farhi (2021). Read RAW camera images (https://github.com/farhi/matlab-readraw), GitHub. Retrieved .

Comments and Ratings (16)

anneamrie nadort

Hi Emmanual! Thank you for sharing this file. Has there been a solution for the "argument must contain a character vector" issue?
>> im = imread('Img0540.nef');
Error using imread (line 403)
Argument must contain a character vector.
I get this also with the test image that you suggested below. Removing all spaces in the folder names did not solve the issue as suggested by Thomas King, in my case.
Any other ideas? Thanks!

arnold

but dcraw is discontinued is it not, the link doesn't work anymore either. I thought an alternative libraw implementation was available but I couldn't find any.

Emmanuel Farhi

Hi Arnold,
To better support recent RAW formats, the idea is to recompile DCRAW, or get pre-compiled binaries. It is true that the included DCRAW version (2016) is now getting old. Just update the dcraw.c file in 'private' directory and type: compile(readraw, 'force')

arnold

reading a DNG, created with Adobe DNG Converter.... readraw.m imports the small resolution thumbnail but not the RAW data.
Any ideas?

arnold

any chance you could add Canon's *.CR3?

Wes Baldwin

Worked perfectly for my Nikon NEF data. To read the RAW unaltered digitized values I used the following:
dc=readraw();
im = imread(dc,filename,'-D -4');

Thomas King

The fix for this error:

Error using imread (line whatever)
Name must be a string scalar or character vector.

is to have no spaces in any of the folder names that lead to your image.

Thomas Darwent

Hi Emmanuel,

I'm wondering if there is a reason for the size of the image it loads and if there is a way to change this as it is larger than it should be.

Ozgur

Hi Emmanuel,
I receive the same error message as Ahmet:

Error using imread (line 438)
Name must be a string scalar or character vector.

I want to import "ARW" files. I tried your suggested test image but it does not work either. My MATLAB version is 2019a running on Win 10.

Thank you.

Stan Thorez

Hi Emmanuel,

I am having some trouble with the software while trying to convert/open .nef images. As a test, I have downloaded the test image from your last reply to Ahmet and I get the same error:

"
>> readraw;
readraw: installed ReadRaw as loader for RAW camera images.
>> im = imread('E1DXINBI000800.CR2'); % this creates a file.tiff
Error using delete
Argument must contain a character vector.

Error in readraw/imread (line 182)
if self.clean, delete(output); end

Error in readraw>@(f)imread(self,f) (line 124)
rawFormat.read = @(f)imread(self, f);

Error in imread (line 403)
[X, map] = feval(fmt_s.read, filename, extraArgs{:});
"

Any idea what the problem here could be? My Matlab version is 2016b. On another system with 2017b, I get the same ("Argument must contain a character vector.") error. I am running it on Windows.

Emmanuel Farhi

Hello Ahmet, I have bee using the test image https://www.imaging-resource.com/PRODS/canon-1dx/FULLRES/E1DXINBI000800.CR2 and the commands:
>> readraw;
>> a=imread('E1DXINBI000800.CR2');
All works OK on my side. Can you try it out yourself ? What system do you have ? Matlab version ?

Neon Argentus

With

rr=imread('IMG_0034.CR2');

gives error

Error using imread (line 440)
Name must be a string scalar or character vector.

Emmanuel Farhi

Hi Imran, I do not have access to your image, and can not inquire what's wrong. Perhaps you're accessing the thumbnail image/size.

Imran Kanjoo

Hello, It always give width and height 120*16*3 although my images have size much bigger size.
dc = readraw;
im = imread(filenames{2});
info = imfinfo(filenames{2}); % this creates a file.tiff

Emmanuel Farhi

Hi Jack, I've updated the contribution to include Windows and MacOSX executables. Try it again, and it should run immediately.

Jack

Hi, I think this would be a great tool. Unfortunately, though, I cannot try it since am not able to compile dcraw.c 9.28 via dcraw_compile_binary.m. result shows 'collect2.exe: error: ld returned 1 exit status'

MATLAB Release Compatibility
Created with R2010a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Communities

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!