Matlab读取ENVI格式遥感图像的头文件

ENVI 格式遥感图像头文件的读取

%设置函数HDR_Read,返回值为parameter,参数为HDRfilename
function  parameter =HDR_Read(HDRfilename)
fileID = fopen(HDRfilename, 'r');  %以只读方式打开.hdr头文件
    
%一直读取头文件的内容直至文件末尾
while ~feof(fileID)
     fileline=fgetl(fileID);
     C = cellstr(strsplit(fileline,'= '));     %"="分割每一行   
     if(C(1)=="samples ")  
         samples=str2double(cell2mat(C(2)));   %存储列数samples
     end
     if(C(1)=="lines   ")       
         lines=str2double(cell2mat(C(2)));     %存储行数lines
     end
     if(C(1)=="bands   ")                      %存储波段数bands
         bands=str2double(cell2mat(C(2)));
     end
     if(C(1)=="interleave ")                   %存储envi中数据存储方式
         interleave=cell2mat(C(2));
     end  
      if(C(1)=="data type ")                   %存储数据类型
         datatype=cell2mat(C(2));
     end       
end
 fclose(fileID);     %关闭文件的读取

%定义ENVI中的数据类型格式
 data_type = {'uint8' 'int16' 'int32' 'float32' 'float64' 'uint16' 'uint32' 'uint64'};
 
 %将envi中不同的数据格式转换成matlab中的特定格式
 switch datatype
    case '1'
        datatype = cell2mat(data_type(1));    %灰度范围0-255
    case '2'
        datatype = cell2mat(data_type(2));    %16位整数
    case '3'
        datatype = cell2mat(data_type(3));    %32位整数
    case '4'
        datatype = cell2mat(data_type(4));    %32位浮点数
    case '5'
        datatype = cell2mat(data_type(5));    %64位浮点数
    case '6'
        datatype = cell2mat(data_type(6));    %灰度范围0-2^16
    case '7'
        datatype = cell2mat(data_type(7));    %灰度范围0-2^32
    case '8'
        datatype = cell2mat(data_type(8));    %灰度范围0-2^64
 end
 
 %将各数据存入返回值参数parameter中
 parameter = {samples,lines,bands,interleave,datatype};
end

你可能感兴趣的:(遥感数字图像处理)