Steve on Image Processing

Concepts, algorithms & MATLAB

Consider multibandread for reading your data 49

Posted by Steve Eddins,

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]});
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);

The examples above are from the "Finding Vegetation in a Multispectral Image" example 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


Comments are closed.

49 CommentsOldest to Newest

Henry replied on : 1 of 49

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

Steve replied on : 2 of 49

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.

Muniru Akinkuolie replied on : 3 of 49

I am using multibandread for the AVIRIS Hyperspectral images. The problem I am facing is how to read in a file after applying the Burrows-Wheeler Compression scheme because it only reads half of the file content and not everything. Thank you.

Gülcan replied on : 5 of 49

Hello, I am trying to read in a multispectral -4 band- satellite image in tif format. Its type is unsigned 16 bit. I applied following code segments to visualize RGB of the image but the resulting colors of the image were not quite right (whole image was like masked with green).

I = multibandread('5m.tif', [927, 1151, 4], 'uint16=>uint8',0, 'bip', 'ieee-le', {'Band','Direct',[3 2 1]});

What can be the reason for that and how could I fix this? Any recommendation would be great help. Thanks.

Steve replied on : 6 of 49

Gülcan—Could be a few things. The most suspicious part of your code is that your converting the input uint16 values to uint8. That’s probably saturating most of the values in the file to 255. If that’s not the problem, then next I would look at the possibility that the bands in the file do not really correspond directly to visible red, green, and blue spectral bands, or that those bands are stored in the file in a different order than you think.

kotya replied on : 7 of 49

how can i read “IMAGERY.L-3” file. It relates to some remote sensing data. how can i run that? pls explain in detail…

Steve replied on : 8 of 49

Kotya—Never heard of it. You’ll need to do some research on your own to find out how the data is stored in the file and then to determine whether you can use a function like multibandread, find code that someone else has written, or write your own.

Ray Kim replied on : 9 of 49

Hi Steve,
I tried to load ENVI hyperspectral image in Matlab using ‘multibandread’ function. However, final reflectance value in matlab was slightly different from ENVI’s(ASCII file). Image was visible in Matlab. Do you have any idea?

Steve replied on : 10 of 49

Ray—Well, your question confuses me a bit because multibandread doesn’t read ASCII files. Can you clarify?

Smriti Sehgal replied on : 11 of 49
Hi Steve,
I am trying to load a BIL image but i dont know how many bands it have to give it as input to multibandread function.Is there a way to know how many bands a image have?

Steve replied on : 12 of 49

Smriti—You could try some Internet searches to see if you can find more about the file you have. Or you could make an educated guess based on the file size.

Steve replied on : 15 of 49

Rohit—The filename extension .img has been used for several different (and usually poorly documented) image file formats. You’ll need to try to find more information about your files.

SARHROUNI replied on : 16 of 49

Hi Sir, I’m traying to open th hyperspectral image 92AV3C.lan (AVIRIS 92AV3C hyperspectral database)
a = multibandread(’92AV3C.lan’, [145,145,224], …
‘uint8=>uint8′,0,’bil’,’ieee-le’, …
{‘Band’,’Direct’,[3 2 1]});
imshow(a), it gives an image flou… can u find where’s the error…think’s

kob replied on : 18 of 49

Hi Steve,

i tried reading hyperspectral data with the example you gave, but i keep getting error message. this is my file details
row=597,column=617,band=125,file format=bsq, file size =89,929KB
please can you explain why this is not working. thanks in advance.

yun replied on : 19 of 49

Hi Steve, i read hyperspectral image with the above example but the display image does not have any features,it is just full of blocky pixels. is there a way to make the features appear in the image as it is in the example above?


Steve replied on : 20 of 49

Kob—How do you know those file details are correct? What’s the exact code you used? And what’s the exact error message you received?

kob replied on : 22 of 49

Hi steve, in reponse to your question, i used the following commands.
CIR = multibandread(‘golinne’, [617, 548, 125],’uint8=> uint8’=>uint8′,…84529, ‘bsq’, ‘ieee-le’, {‘Band’,’Direct’
,[4, 3, 2]});

from envi header info: sample=548,lines=617, file format in bsq, 84529= file size.

this time the results that come out is only a graphic with colour mixture but no detail features to see like the one above.

Steve replied on : 25 of 49

Kob—Then it is very likely that the parameters you are passing to multibandread are incorrect. Because I don’t know anything about your files, I can only guess at the problem. Passing 84529 as the “offset” parameter to multibandread looks incorrect. You might want to look at the documentation again. If you still have trouble using multibandread, please contact technical support.

Mo replied on : 28 of 49

Hi, Steve my question about matlab.
I have a big file which (file name.img) and it is 18549KB. How can i read this file by using matlab?. Notes i have a header file in case we need it

Steve replied on : 29 of 49

Mo—I don’t know. You haven’t told anything about how the data is organized within the file. Do you know, or can you find out?

Mo replied on : 30 of 49

Steve nice to hear from you. the dimension of the 3795 is the column , 5005 is the raw the size is 74196.

Steve replied on : 31 of 49

Mo—I’m sorry, but that’s still too vague to work with. In general, there’s a lot one needs to know in order to read a data file if you don’t already have a standard reader for it. For example, how is the data stored in the file? ASCII, binary, etc.? What precision? Integers, floating-point, single or double, how many bits? Compressed or uncompressed? Pixels ordered by row or column? How many bands? How are the band components ordered? If multibyte binary integers, are they stored big endian or little endian?

Mo replied on : 32 of 49

Steve , when i use this comand
IM = multibandread(‘4.img’, [512, 512, 7], ‘uint8=>uint8’,…
128, ‘bil’, ‘ieee-le’, {‘Band’,’Direct’,[4 3 2]});
withe the same [512 , 512] the image become like milting and i cant recogize any thing on it. also in case i change both the [512, 512] to my regular dimension which [3795 5005] i got this error message [ ??? Error using ==> multibandread at 115
The file is too small to contain the specified data.Check the size, offset, and
precision arguments.] what do u think is the problem.

Steve replied on : 33 of 49

No idea. It could be just about anything. For example, maybe your file is corrupted, or truncated? Maybe your data doesn’t really have 7 bands, which is what you told multibandread?

Mo replied on : 34 of 49

Steve all what i know about my file is its img file the size is 3795 X 5005 the floating point is 4 bytes the size is 74196 KB . is these information enough to read it direct by using multibandread command? thank you steve.

Steve replied on : 35 of 49

Mo—Yes. Looks like you only have one band, though. In your earlier comment your code indicated you were telling multibandread you had seven bands. You may have to try reversing the order of the dimensions when you call multibandread. That is, if [3795 5005] doesn’t work, try [5005 3795].

Mo replied on : 36 of 49

Steve . you are the man. its work once i changed the band. but i have changed the band from 3 to 2 to 1 all of them work. what does that mean?

Aditi replied on : 37 of 49

hey steve i wanted to ask as to how to find how many bands are present an image so that i can work with multibandread function…

Steve replied on : 38 of 49

Aditi—You have to know. This information is often in the header or is available from the source that provided the image. Or, if you know the number of columns and rows, you can try to guess the number of bands based on the size of the file.

TJ replied on : 39 of 49

I am having a problem where I read an image cube using multibandread() but then imagesc() colors aren’t correct. My cube is really 10 bit but saved as 16 bit. Here is my code…

bands = [55 90 146]; %these are the RGB bands

a = multibandread(‘sample1.cub’,[1119 1272 292],’uint16′,0,’bil’,’ieee-be’,{‘band’,’direct’,bands});

a = a/(2^16-1); %have to normalize for imagesc()

Steve replied on : 40 of 49

TJ—I don’t think imagesc is appropriate for displaying an RGB truecolor image. Try using image instead.

Ajith Joseph replied on : 41 of 49

Hi Steve

I am trying to view a hyperspectral image in MATLAB using multibandread and then using imageshow command as shown below, but I am getting the following error:
??? Error using ==> imageDisplayValidateParams>validateCData at 114
Unsupported dimension

Error in ==> imageDisplayValidateParams at 31
common_args.CData = validateCData(common_args.CData,image_type);

Please could you help me figure it out.Thanks in advance

image_tile1 = multibandread('Image Processing Project\M3G20090107T130225_u2_rdn_img',[11739,304,85],'float',0,'bil','ieee-le',{'Row','Range',[25 500]});
sukuchha replied on : 42 of 49


you can only three bands one at a time. so have to use

imshow(image_tile1(:,:,1:3)) for viewing first three bands for example !

Fran replied on : 43 of 49

Hi Steve

I am trying to open a .dat image (I also have the .hdr file) in matlab apparently I succeed in reading it, but when I show it it is completely white (I can see some pixels in slightly different color, but most of it white). The header file says that is int16 data, could this be the problem?

Giulia replied on : 44 of 49

Hi!I’m using multibandread function for reading raw image.I followed mathworks examples: defined my dataset, instead of using fwrite, I used:

but i’ve some problems on fread: Invalid size. Thanks in advance

Giulia replied on : 45 of 49


im4 = multibandread(‘prova.raw’, [441 476 160], …
‘double’, 0, ‘bsq’, ‘ieee-le’, …
{‘Band’, ‘Direct’} );

foguy replied on : 46 of 49

Please, i need help
I tried to display de landsat OLI 8 in matlab, this is my code
X = multibandread(‘alldata’, [7621, 7791,7], ‘uint16’,…
0,’bsq’, ‘ieee-le’, {‘Band’, ‘Direct’, [3,2,1]});
figure, imshow(X)
the image who display is not good image.
thank you

foguy replied on : 47 of 49

when I try to display one band of the image, its just a bunch of lines that show no resemblance to the original image. I am not sure as to what the problem is.

Gheorghe gardu replied on : 48 of 49

Dear Sir,
Where did you get the file paris.lan, i.e. the source file to analyse ?
The landsat file ? I do not find it.
Gheorghe Gardu