How to read multiband envi file into matlab?
7 views (last 30 days)
Show older comments
I am using the following matlab syntex to read envi generated image file;
img_path = 'C:\Data\'
d = uigetdir(pwd, 'Select a folder');
imgfiles = dir(fullfile(d, '*.img'));
for m = 1:length(imgfiles)
img_file = imgfiles(m);
disp(img_file.name);
img = imread(img_file.name);
red = img(:,:,4); % red band
rededge1 = img(:,:,5); % rededge1 band
rededge2 = img(:,:,6); % rededge2 band
rededge3 = img(:,:,7); % rededge3 band
nir = img(:,:,8); % near infrared band
This code is giving following errors
>> ds_indices_caf
img_path =
'C:\Data\'
20240309_SAMPLE_DATA.img
Error using imread>get_full_filename
File "20240309_SAMPLE_DATA.img" does not exist.
Error in imread (line 372)
fullname = get_full_filename(filename);
Error in ds_indices_caf (line 17)
img = imread(img_file.name);
>>
I am attaching the input file as zip file. Please suggest me how to fix this error.
Deva
0 Comments
Answers (1)
Manasa Singam
on 26 Mar 2024
The imread function can't read ENVI format files (.img and .hdr). Use the multibandread function and specify the required inputs.
For examplem, for the above data, the command will be:
>> ImgFilename = "20240309_SAMPLE_DATA.img";
>> size = [399 426 7];
>> datatype = "uint16"; % uint16 for 12
>> headerOffset = 0;
>> interleave = "bsq";
>> byteOrder = "ieee-le"; % 0 means little-endian
>> data = multibandread(ImgFilename,size,datatype,headerOffset,interleave,byteOrder);
But i suggest to use hypercube function, which supports to read envi format files. https://in.mathworks.com/matlabcentral/fileexchange/76796-image-processing-toolbox-hyperspectral-imaging-library
hypercube(ImgFilename);
7 Comments
Devendra
on 27 Mar 2024
Edited: Devendra
on 27 Mar 2024
Actulally I am computing NDVI as follows
NDVI = (nir - red) ./ (nir + red);
Here I am getting either 0 or 1 values not real numbers up to four decimal numbers. Since red and nir both contains integer numbers. I want NDVI values as real numbers. Please suggest me how to do it?
I am thankful to you for providing me support.
Deva
Manasa Singam
on 27 Mar 2024
You can convert to single data type.
data = single(img.DataCube);
Use data numeric array which is in single data type to caculate NDVI.
red = data(:,:,4);
nir = data(:,:,8);
But i suggest you to use ndvi function which is available in the downloaded hyperspectral package itself. It will directly take hypercube object and will give you the ndvi image.
If the HDR file doesn't contain wavelength values in it, make sure it give proper ranges fo red and nir. Red ranges from 610-700nm and NIR ranges from 700-1400nm.
See Also
Categories
Find more on Get Started with MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!