Steve on Image Processing

February 6th, 2009

Consider multibandread for reading your data

I've seen several situations recently in which a user couldn't figure out how to get their data into MATLAB, and it turned out that the data was in a form that could be handled by the MATLAB function multibandread. So I have a feeling that not enough people know about this useful function.

There are a lot of relatively simple gridded data file formats in use that follow a common pattern: the format has a fixed-length header, followed immediately by all the data values, in some defined order. Sometimes the data has multiple bands, in which case there is some variation from format to format in the ordering of band values in the file. This is the perfect scenario for using multibandread.

You can tell multibandread the header size, the data-value precision, the byte order, and how the bands are arranged, and then multibandread can read any band, or all of them, or even a subset of a band.

Here's an example. The Image Processing Toolbox ships with a few sample .lan files, which are Landsat Thematic Mapper images 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 values, which are band interleaved by line (BIL) in order of increasing band number. They are stored as unsigned 8-bit integers. The following command reads in bands 4, 3, and 2 from the file.

CIR = multibandread('paris.lan', [512, 512, 7], 'uint8=>uint8',...
                    128, 'bil', 'ieee-le', {'Band','Direct',[4 3 2]});
imshow(CIR)
xlabel('Image courtesy of Space Imaging, LLC')

And for no extra charge, I'll toss in a quick demo of decorrstretch (decorrelation stretch) from the Image Processing Toolbox.

decorrCIR = decorrstretch(CIR, 'Tol', 0.01);
imshow(decorrCIR)

The examples above are from the "Finding Vegetation in a Multispectral Image" demo in the Image Processing Toolbox.

Give multibandread a try. You might find it to be just what you need.


Get the MATLAB code

Published with MATLAB® 7.7

2 Responses to “Consider multibandread for reading your data”

  1. Henry replied on :

    Hi Steve! Thanks for this very useful hint. One question: is it possible to read all bands in a file using multibandread, if the total number of bands is not known. With fread, it is possible to simply read to the end of the file. Thanks for your help.Henry

  2. Steve replied on :

    Henry—No, you need to provide multibandread with the number of bands. In most situations where people are using multibandread, they are working with a particular data set, such as from a particular satellite program, in which all the files have the same form and the same number of bands.

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


Steve Eddins manages the Image & Geospatial development team at The MathWorks and coauthored Digital Image Processing Using MATLAB. He writes here about image processing concepts, algorithm implementations, and MATLAB.

  • Steve: Kezia—Try imrotate.
  • kezia: steve, how to perform rotation of structuring element by 15 degrees. kindly answer my question. thank u kezia...
  • Steve: Tasha—I only accept comments that are relevant to the particular blog post or are questions or comments...
  • Tasha: Steve,I send you a comment here but still didn’t get any reply yet.I did not see my comment posted here...
  • Steve: Carsten—Thanks for your input.
  • Carsten: Another vote for either imtranslate.m, or at least a blurb in the imtransform help why pure translation...
  • Loren Shure: If you look towards the end of the fftfilt program, you will see that there’s a check to see if...
  • Steve: Sonja—My imwritesize submission on the MATLAB Central File Exchange might be helpful. It was posted...
  • Steve: Grant—Sorry, but it won’t be for R2010a. That development deadline has already passed.
  • Sonja: My publisher is wanting images for a new book to be 300 dpi. Only 5 of the 19 images are 300, the rest are...

These postings are the author's and don't necessarily represent the opinions of The MathWorks.