{"id":364,"date":"2011-03-08T23:29:46","date_gmt":"2011-03-09T04:29:46","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/2011\/03\/08\/tips-for-reading-a-camera-raw-file-into-matlab\/"},"modified":"2019-10-29T16:31:36","modified_gmt":"2019-10-29T20:31:36","slug":"tips-for-reading-a-camera-raw-file-into-matlab","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2011\/03\/08\/tips-for-reading-a-camera-raw-file-into-matlab\/","title":{"rendered":"Tips for reading a camera raw file into MATLAB"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p>An academic colleague asked me recently how to read the sensor data (in <a href=\"http:\/\/en.wikipedia.org\/wiki\/Bayer_filter\">Bayer<\/a> or color filter array form) from a Nikon raw camera file (an NEF file) into MATLAB. We figured out a way to do it, and I\r\n      thought I'd pass it along.\r\n   <\/p>\r\n   <p>I should caution you, though, that there are multiple steps involved, some of which are \"advanced maneuvers.\"<\/p>\r\n   <p>First, I suggested that we try using the free <a href=\"http:\/\/www.adobe.com\/products\/dng\/\">Adobe DNG Converter<\/a> program to convert the NEF file to a DNG (Digital Negative) file. After some trial-and-error we found that it's necessary\r\n      to tell DNG Converter to do the conversion \"uncompressed.\" Here are some screenshots of the relevant dialogs.\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/change-preferences-button.png\"> <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/compatibility-option.png\"> <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/uncompressed-check-box.png\"> <\/p>\r\n   <p>Under the covers, a DNG file is a very specialized kind of TIFF file. However, we can't read the raw sensor data using <tt>imread<\/tt>. If you call <tt>imread<\/tt> on the DNG file, it will just give you back the thumbnail image. Instead, we have to make use of the MATLAB <a title=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/tiffclass.html (link no longer works)\"><tt>Tiff<\/tt> class<\/a>. This class offers low-level access to the \"guts\" of a TIFF file. To use it successfully, it helps to have some familiarity\r\n      with how TIFF files work.\r\n   <\/p>\r\n   <p>But first let's poke at a sample DNG file using <tt>imfinfo<\/tt>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">info = imfinfo(<span style=\"color: #A020F0\">'books.dng'<\/span>)<\/pre><pre style=\"font-style:oblique\">\r\ninfo = \r\n\r\n                     Filename: '\\\\mathworks\\home\\eddins\\files\\I\\ipblog_material\\2011\\books.dng'\r\n                  FileModDate: '08-Mar-2011 12:01:20'\r\n                     FileSize: 25079002\r\n                       Format: 'tif'\r\n                FormatVersion: []\r\n                        Width: 256\r\n                       Height: 170\r\n                     BitDepth: 24\r\n                    ColorType: 'truecolor'\r\n              FormatSignature: [73 73 42 0]\r\n                    ByteOrder: 'little-endian'\r\n               NewSubFileType: 1\r\n                BitsPerSample: [8 8 8]\r\n                  Compression: 'Uncompressed'\r\n    PhotometricInterpretation: 'RGB'\r\n                 StripOffsets: 133234\r\n              SamplesPerPixel: 3\r\n                 RowsPerStrip: 170\r\n              StripByteCounts: 130560\r\n                  XResolution: []\r\n                  YResolution: []\r\n               ResolutionUnit: 'None'\r\n                     Colormap: []\r\n          PlanarConfiguration: 'Chunky'\r\n                    TileWidth: []\r\n                   TileLength: []\r\n                  TileOffsets: []\r\n               TileByteCounts: []\r\n                  Orientation: 1\r\n                    FillOrder: 1\r\n             GrayResponseUnit: 0.0100\r\n               MaxSampleValue: [255 255 255]\r\n               MinSampleValue: 0\r\n                 Thresholding: 1\r\n                       Offset: 8\r\n                         Make: 'NIKON CORPORATION '\r\n                        Model: 'NIKON D90 '\r\n                     Software: 'Ver.1.00 '\r\n                     DateTime: '2011:03:06 14:45:58 '\r\n                      SubIFDs: {2x1 cell}\r\n                          XMP: [1x7309 char]\r\n                DigitalCamera: [1x1 struct]\r\n                   DNGVersion: [4x1 double]\r\n           DNGBackwardVersion: [4x1 double]\r\n            UniqueCameraModel: 'Nikon D90 '\r\n                 ColorMatrix1: [9x1 double]\r\n                 ColorMatrix2: [9x1 double]\r\n                AnalogBalance: [3x1 double]\r\n                AsShotNeutral: [3x1 double]\r\n             BaselineExposure: 0.2500\r\n                BaselineNoise: 1\r\n            BaselineSharpness: 1\r\n           LinearResponseUnit: 1\r\n           CameraSerialNumber: '3276464 '\r\n                     LensInfo: [4x1 double]\r\n                  ShadowScale: 1\r\n               DNGPrivateData: [121588x1 double]\r\n       CalibrationIlluminant1: 17\r\n       CalibrationIlluminant2: 21\r\n           AliasLayerMetadata: [16x1 double]\r\n          OriginalRawFileName: 'books.NEF '\r\n                  UnknownTags: [15x1 struct]\r\n\r\n<\/pre><p>You can see that <tt>imfinfo<\/tt> thinks that this file is a 256-by-170 truecolor image. But as I mentioned above, that's just a thumbnail image. The data\r\n      we want is hiding in something called a \"SubIFD.\"\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">info.SubIFDs{1}<\/pre><pre style=\"font-style:oblique\">\r\nans = \r\n\r\n                     Filename: '\\\\mathworks\\home\\eddins\\files\\I\\ipblog_material\\2011\\books.dng'\r\n                  FileModDate: '08-Mar-2011 12:01:20'\r\n                     FileSize: 25079002\r\n                       Format: 'tif'\r\n                FormatVersion: []\r\n                        Width: 4310\r\n                       Height: 2868\r\n                     BitDepth: 16\r\n                    ColorType: 'CFA'\r\n              FormatSignature: [73 73 42 0]\r\n                    ByteOrder: 'little-endian'\r\n               NewSubFileType: 0\r\n                BitsPerSample: 16\r\n                  Compression: 'Uncompressed'\r\n    PhotometricInterpretation: 'CFA'\r\n                 StripOffsets: 356842\r\n              SamplesPerPixel: 1\r\n                 RowsPerStrip: 2868\r\n              StripByteCounts: 24722160\r\n                  XResolution: []\r\n                  YResolution: []\r\n               ResolutionUnit: 'None'\r\n                     Colormap: []\r\n          PlanarConfiguration: 'Chunky'\r\n                    TileWidth: []\r\n                   TileLength: []\r\n                  TileOffsets: []\r\n               TileByteCounts: []\r\n                  Orientation: 1\r\n                    FillOrder: 1\r\n             GrayResponseUnit: 0.0100\r\n               MaxSampleValue: 65535\r\n               MinSampleValue: 0\r\n                 Thresholding: 1\r\n                       Offset: 130252\r\n                CFAPlaneColor: [3x1 double]\r\n                    CFALayout: 1\r\n           LinearizationTable: [772x1 double]\r\n          BlackLevelRepeatDim: [2x1 double]\r\n                   BlackLevel: 0\r\n                   WhiteLevel: 3767\r\n                 DefaultScale: [2x1 double]\r\n            DefaultCropOrigin: [2x1 double]\r\n              DefaultCropSize: [2x1 double]\r\n              BayerGreenSplit: 0\r\n            AntiAliasStrength: 1\r\n             BestQualityScale: 1\r\n                   ActiveArea: [4x1 double]\r\n                  UnknownTags: [2x1 struct]\r\n\r\n<\/pre><p>That's the good stuff! It's a 16-bit-per-sample 4310-by-2868 color filter array (CFA). Here's how to read it. I'm just going\r\n      to give the steps without explanation and refer you to the <a title=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/tiffclass.html (link no longer works)\">documentation for the <tt>Tiff<\/tt> class<\/a> for more information.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">warning <span style=\"color: #A020F0\">off<\/span> <span style=\"color: #A020F0\">MATLAB:tifflib:TIFFReadDirectory:libraryWarning<\/span>\r\nt = Tiff(<span style=\"color: #A020F0\">'books.dng'<\/span>,<span style=\"color: #A020F0\">'r'<\/span>);\r\noffsets = getTag(t,<span style=\"color: #A020F0\">'SubIFD'<\/span>);\r\nsetSubDirectory(t,offsets(1));\r\ncfa = read(t);\r\nclose(t);<\/pre><p>Here's a screen shot of <tt>imtool<\/tt> showing the resulting color filter array at 400% magnification. You can clearly see the Bayer array pattern.\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/imtool-screen-capture.jpg\"> <\/p>\r\n   <p>We wondered why the maximum sensor value was 768. After some investigation, we found that Nikon appears to be storing a nonlinear\r\n      quantization off the original 12-bit values. The linearization curve can be found in the DNG file as follows:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">curve = info.SubIFDs{1}.LinearizationTable;\r\nplot(curve)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/read_nef_raw_01.png\"> <p>So if you were going to do calculations based on this raw data, you'd probably want to linearize it back to the original 12-bit\r\n      range first using this curve.\r\n   <\/p>\r\n   <p>Have you used the MATLAB <tt>Tiff<\/tt> class to do advanced maneuvers with TIFF files? Let us know what you've done by posting a comment here.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_18995fd8d12f40dc8e272804796e987b() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='18995fd8d12f40dc8e272804796e987b ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 18995fd8d12f40dc8e272804796e987b';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Steve Eddins';\r\n        copyright = 'Copyright 2011 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_18995fd8d12f40dc8e272804796e987b()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.11<br><\/p>\r\n<\/div>\r\n<!--\r\n18995fd8d12f40dc8e272804796e987b ##### SOURCE BEGIN #####\r\n%%\r\n% An academic colleague asked me recently how to read the sensor data (in\r\n% <http:\/\/en.wikipedia.org\/wiki\/Bayer_filter Bayer> or color filter array\r\n% form) from a Nikon raw camera file (an NEF file) into MATLAB. We figured\r\n% out a way to do it, and I thought I'd pass it along.\r\n%\r\n% I should caution you, though, that there are multiple steps involved,\r\n% some of which are \"advanced maneuvers.\"\r\n%\r\n% First, I suggested that we try using the free\r\n% <http:\/\/www.adobe.com\/products\/dng\/ Adobe DNG Converter> program to\r\n% convert the NEF file to a DNG (Digital Negative) file. After some\r\n% trial-and-error we found that it's necessary to tell DNG Converter to do\r\n% the conversion \"uncompressed.\" Here are some screenshots of the relevant\r\n% dialogs.\r\n%\r\n% <<https:\/\/blogs.mathworks.com\/images\/steve\/2011\/change-preferences-button.png>>\r\n%\r\n% <<https:\/\/blogs.mathworks.com\/images\/steve\/2011\/compatibility-option.png>>\r\n%\r\n% <<https:\/\/blogs.mathworks.com\/images\/steve\/2011\/uncompressed-check-box.png>>\r\n%\r\n% Under the covers, a DNG file is a very specialized kind of TIFF file.\r\n% However, we can't read the raw sensor data using |imread|. If you call\r\n% |imread| on the DNG file, it will just give you back the thumbnail image.\r\n% Instead, we have to make use of the MATLAB\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/tiffclass.html |Tiff| class>.\r\n% This class offers low-level access to the \"guts\" of a TIFF file. To use\r\n% it successfully, it helps to have some familiarity with how TIFF files\r\n% work.\r\n% \r\n% But first let's poke at a sample DNG file using |imfinfo|.\r\n\r\ninfo = imfinfo('books.dng')\r\n\r\n%%\r\n% You can see that |imfinfo| thinks that this file is a 256-by-170\r\n% truecolor image. But as I mentioned above, that's just a thumbnail image.\r\n% The data we want is hiding in something called a \"SubIFD.\"\r\n\r\ninfo.SubIFDs{1}\r\n\r\n%%\r\n% That's the good stuff! It's a 16-bit-per-sample 4310-by-2868 color filter\r\n% array (CFA). Here's how to read it. I'm just going to give the steps\r\n% without explanation and refer you to the \r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/tiffclass.html documentation for the |Tiff|\r\n% class> for more information.\r\n\r\nwarning off MATLAB:tifflib:TIFFReadDirectory:libraryWarning\r\nt = Tiff('books.dng','r');\r\noffsets = getTag(t,'SubIFD');\r\nsetSubDirectory(t,offsets(1));\r\ncfa = read(t);\r\nclose(t);\r\n\r\n%%\r\n% Here's a screen shot of |imtool| showing the resulting color filter array\r\n% at 400% magnification. You can clearly see the Bayer array pattern.\r\n%\r\n% <<https:\/\/blogs.mathworks.com\/images\/steve\/2011\/imtool-screen-capture.jpg>>\r\n%\r\n% We wondered why the maximum sensor value was 768. After some\r\n% investigation, we found that Nikon appears to be storing a nonlinear\r\n% quantization off the original 12-bit values. The linearization curve can\r\n% be found in the DNG file as follows:\r\n\r\ncurve = info.SubIFDs{1}.LinearizationTable;\r\nplot(curve)\r\n\r\n%%\r\n% So if you were going to do calculations based on this raw data, you'd\r\n% probably want to linearize it back to the original 12-bit range first\r\n% using this curve.\r\n%\r\n% Have you used the MATLAB |Tiff| class to do advanced maneuvers with TIFF\r\n% files? Let us know what you've done by posting a comment here.\r\n\r\n##### SOURCE END ##### 18995fd8d12f40dc8e272804796e987b\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   An academic colleague asked me recently how to read the sensor data (in Bayer or color filter array form) from a Nikon raw camera file (an NEF file) into MATLAB. We figured out a way to do it,... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2011\/03\/08\/tips-for-reading-a-camera-raw-file-into-matlab\/\">read more >><\/a><\/p>","protected":false},"author":42,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[278,751,332,180,68,753,755,683,749],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/364"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/users\/42"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/comments?post=364"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/364\/revisions"}],"predecessor-version":[{"id":3723,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/364\/revisions\/3723"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=364"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=364"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=364"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}