How to read multiband envi file into matlab?

4 次查看(过去 30 天)
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

回答(1 个)

Manasa Singam
Manasa Singam 2024-3-26
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 个评论
Devendra
Devendra 2024-3-27
编辑:Devendra 2024-3-27
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
Manasa Singam 2024-3-27
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Agriculture 的更多信息

产品


版本

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by