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.

  • Sana: hi steve, could you explain to me how i would be able to use the dir function, to do a loop through a directory...
  • Nishtha: Sir, I have preprocessed the image in following steps: [1] adaptive histogram equalization [2] thresholding...
  • Kristof: I also strongly support the idea. I have just recently bumped into the problem that im2single was not...
  • Steve: David—I’ m glad you found it useful!
  • David Lalejini: I found your example very useful for finding connected nodes in a large set of input pairs. I start...
  • tommy: Dear Steve, I have a question,please if you are kind to help me regarding the accumulator array dimensions of...
  • Steve: Abc—I don’t know how to distinguish the faces. You might try posting your question in the MATLAB...
  • Manju: well if we have a few ovals within each other like in a cell how do we measure the distance from the center...
  • Steve: Manju—What do you mean? How is each region defined?
  • Manju: if we have 2-3 regions within each other how do we measure the regions of each one?

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