{"id":248,"date":"2009-02-06T07:00:12","date_gmt":"2009-02-06T12:00:12","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/2009\/02\/06\/consider-multibandread-for-reading-your-data\/"},"modified":"2019-10-28T15:25:53","modified_gmt":"2019-10-28T19:25:53","slug":"consider-multibandread-for-reading-your-data","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2009\/02\/06\/consider-multibandread-for-reading-your-data\/","title":{"rendered":"Consider multibandread for reading your data"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p>I've seen several situations recently in which a user couldn't figure out how to get their data into MATLAB, and it turned\r\n      out that the data was in a form that could be handled by the MATLAB function <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2008b\/techdoc\/ref\/multibandread.html\"><tt>multibandread<\/tt><\/a>. So I have a feeling that not enough people know about this useful function.\r\n   <\/p>\r\n   <p>There are a lot of relatively simple gridded data file formats in use that follow a common pattern: the format has a fixed-length\r\n      header, followed immediately by all the data values, in some defined order.  Sometimes the data has multiple bands, in which\r\n      case there is some variation from format to format in the ordering of band values in the file.  This is the perfect scenario\r\n      for using <tt>multibandread<\/tt>.\r\n   <\/p>\r\n   <p>You can tell <tt>multibandread<\/tt> the header size, the data-value precision, the byte order, and how the bands are arranged, and then <tt>multibandread<\/tt> can read any band, or all of them, or even a subset of a band.\r\n   <\/p>\r\n   <p>Here's an example.  The Image Processing Toolbox ships with a few sample .lan files, which are Landsat Thematic Mapper images\r\n      in Erdas LAN format. The file paris.lan contains a 7-band 512-by-512 Landsat image. A 128-byte header is followed by the pixel\r\n      values, which are band interleaved by line (BIL) in order of increasing band number. They are stored as unsigned 8-bit integers.\r\n      The following command reads in bands 4, 3, and 2 from the file.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">CIR = multibandread(<span style=\"color: #A020F0\">'paris.lan'<\/span>, [512, 512, 7], <span style=\"color: #A020F0\">'uint8=&gt;uint8'<\/span>,<span style=\"color: #0000FF\">...<\/span>\r\n                    128, <span style=\"color: #A020F0\">'bil'<\/span>, <span style=\"color: #A020F0\">'ieee-le'<\/span>, {<span style=\"color: #A020F0\">'Band'<\/span>,<span style=\"color: #A020F0\">'Direct'<\/span>,[4 3 2]});\r\nimshow(CIR)\r\nxlabel(<span style=\"color: #A020F0\">'Image courtesy of Space Imaging, LLC'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2009\/multibandread_example_01.jpg\"> <p>And for no extra charge, I'll toss in a quick demo of <tt>decorrstretch<\/tt> (decorrelation stretch) from the Image Processing Toolbox.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">decorrCIR = decorrstretch(CIR, <span style=\"color: #A020F0\">'Tol'<\/span>, 0.01);\r\nimshow(decorrCIR)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2009\/multibandread_example_02.jpg\"> <p>The examples above are from the <a href=\"https:\/\/www.mathworks.com\/help\/images\/examples\/finding-vegetation-in-a-multispectral-image.html\">\"Finding Vegetation in a Multispectral Image\" example<\/a> in the Image Processing Toolbox.\r\n   <\/p>\r\n   <p>Give <tt>multibandread<\/tt> a try. You might find it to be just what you need.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_fb7b56a9b84b4f67895def59721619b1() {\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='fb7b56a9b84b4f67895def59721619b1 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' fb7b56a9b84b4f67895def59721619b1';\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 2009 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_fb7b56a9b84b4f67895def59721619b1()\"><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.7<br><\/p>\r\n<\/div>\r\n<!--\r\nfb7b56a9b84b4f67895def59721619b1 ##### SOURCE BEGIN #####\r\n%%\r\n% I've seen several situations recently in which a user couldn't figure out how\r\n% to get their data into MATLAB, and it turned out that the data was in a form\r\n% that could be handled by the MATLAB function \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2008b\/techdoc\/ref\/multibandread.html \r\n% |multibandread|>. So I have\r\n% a feeling that not enough people know about this useful function.\r\n%\r\n%\r\n% There are a lot of relatively simple gridded data file formats in use that\r\n% follow a common pattern: the format has a fixed-length header, followed\r\n% immediately by all the data values, in some defined order.  Sometimes the data\r\n% has multiple bands, in which case there is some variation from format to\r\n% format in the ordering of band values in the file.  This is the perfect\r\n% scenario for using |multibandread|.\r\n%\r\n% You can tell |multibandread| the header size, the data-value precision,\r\n% the byte order, and how the bands are arranged, and then\r\n% |multibandread| can read any band, or all of them, or even a subset of\r\n% a band.\r\n%\r\n% Here's an example.  The Image Processing Toolbox ships with a few sample .lan\r\n% files, which are Landsat Thematic Mapper images in Erdas LAN format. The file\r\n% paris.lan contains a 7-band 512-by-512 Landsat image. A 128-byte header is\r\n% followed by the pixel values, which are band interleaved by line (BIL) in\r\n% order of increasing band number. They are stored as unsigned 8-bit integers.\r\n% The following command reads in bands 4, 3, and 2 from the file.\r\n\r\nCIR = multibandread('paris.lan', [512, 512, 7], 'uint8=>uint8',...\r\n                    128, 'bil', 'ieee-le', {'Band','Direct',[4 3 2]});\r\nimshow(CIR)\r\nxlabel('Image courtesy of Space Imaging, LLC')\r\n\r\n%%\r\n% And for no extra charge, I'll toss in a quick demo of |decorrstretch|\r\n% (decorrelation stretch) from the Image Processing Toolbox.\r\n\r\ndecorrCIR = decorrstretch(CIR, 'Tol', 0.01);\r\nimshow(decorrCIR)\r\n\r\n%%\r\n% The examples above are from the \r\n% <https:\/\/www.mathworks.com\/products\/image\/demos.html?file=\/products\/demos\/shipping\/images\/ipexndvi.html \r\n% \"Finding Vegetation in a Multispectral Image\" demo> in the Image Processing\r\n% Toolbox. \r\n%\r\n% Give |multibandread| a try. You might find it to be just what you need.\r\n##### SOURCE END ##### fb7b56a9b84b4f67895def59721619b1\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   I've seen several situations recently in which a user couldn't figure out how to get their data into MATLAB, and it turned\r\n      out that the data was in a form that could be handled by the... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2009\/02\/06\/consider-multibandread-for-reading-your-data\/\">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":[537,36,535,94],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/248"}],"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=248"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/248\/revisions"}],"predecessor-version":[{"id":2273,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/248\/revisions\/2273"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=248"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}