Saturday, 31 December 2011

Opening multispectral or hyperspectral ENVI files in MATLAB

ENVI/IDL is one of the most used remote sensing software package which has a nice programming interface known as IDL. Though I have been using IDL for quite some time now, I still prefer MATLAB over IDL because MATLAB has extensive help documentation with lots of useful demonstration which I think is lacking in IDL.

There is a function in MATLAB which not many people are aware of, which is called Multibandread. Multibandread can be use for any type of binary files such as bsq, bil and bip. As long as you know how the data is organized in the binary file, its data type, rows columns and no. of bands in the file you can read the file in MATLAB with Multibandread.  Multibandread requires following informations.

X = multibandread(filename, size, precision, offset, interleave, byteorder)

With ENVI files, you can find all these information in the header file of that ENVI file. Using that information, you can pass this information by manually typing or you can write a custom function which will open the header file, fetch those information from the header file and pass it to Multibandread. I usally do it with latter approach.
Opening whole remote sensing images in MATLAB is a bit tricky. Consider you have a hyperspectral data with 116 bands and you try to read whole image at one go. MATLAB will run out of memory. The wise thing is to read the image in a block by block (MxN), performing required operation in the block and later combining all blocks. 

One cautionary note, I have seen people doing some silly mistakes such as reading an ENVI file with 10 bands in a variable (image) and using imshow (image) to visualize it. They spend countless hours wondering why the Multibandread is not being able read their ENVI files. But they forget that imshow function can display only three band one time. So if you want to view first three band then you have to use

  Imshow( image(:,:,1:3),[])

So, try Multibandread function if you are MATLAB savvy person and prefer MATLAB over IDL.
If you run into any kind of problem, I am ready for help.