matlab读取遥感hdr影像

matlab读取遥感hdr影像... matlab读取遥感hdr影像 展开
 我来答
缘Lai如茨
2016-04-09 · TA获得超过8631个赞
知道大有可为答主
回答量:7534
采纳率:92%
帮助的人:579万
展开全部
1、 读取ENVI标准图像代码非原创。文件名read_ENVIimagefile.m代码如下:
function data=read_ENVIimagefile(imgfilename)
%本函数读取img格式,前提是img图像显式带有'.img'后缀名
if length(imgfilename)>=4
switch strcmp(imgfilename(length(imgfilename)-3:end), '.img')
case 0
hdrfilename=strcat(imgfilename, '.hdr');
case 1
hdrfilename=strcat(imgfilename(1: (length(imgfilename)-4)), '.hdr');
end
else
hdrfilename=strcat(imgfilename, '.hdr');
end
%读取ENVI标准格式图像文件
%读取图像头文件
fid = fopen(hdrfilename, 'r');
info = fread(fid,'char=>char');
info=info';%默认读入列向量,须要转置为行向量才适于显示
fclose(fid);
%查找列数
a=strfind(info,'samples = ');
b=length('samples = ');
c=strfind(info,'lines');
samples=[];
for i=a+b:c-1
samples=[samples,info(i)];
end
samples=str2num(samples);
%查找行数
a=strfind(info,'lines = ');
b=length('lines = ');
c=strfind(info,'bands');
lines=[];
for i=a+b:c-1
lines=[lines,info(i)];
end
lines=str2num(lines);
%查找波段数
a=strfind(info,'bands = ');
b=length('bands = ');
c=strfind(info,'header offset');
bands=[];
for i=a+b:c-1
bands=[bands,info(i)];
end
bands=str2num(bands);
%查找数据类型
a=strfind(info,'data type = ');
b=length('data type = ');
c=strfind(info,'interleave');
datatype=[];
for i=a+b:c-1
datatype=[datatype,info(i)];
end
datatype=str2num(datatype);
precision=[];
switch datatype
case 1
precision='uint8=>uint8';%头文件中datatype=1对应ENVI中数据类型为Byte,对应MATLAB中数据类型为uint8
case 2
precision='int16=>int16';%头文件中datatype=2对应ENVI中数据类型为Integer,对应MATLAB中数据类型为int16
case 12
precision='uint16=>uint16';%头文件中datatype=12对应ENVI中数据类型为Unsighed Int,对应MATLAB中数据类型为uint16
case 3
precision='int32=>int32';%头文件中datatype=3对应ENVI中数据类型为Long Integer,对应MATLAB中数据类型为int32
case 13
precision='uint32=>uint32';%头文件中datatype=13对应ENVI中数据类型为Unsighed Long,对应MATLAB中数据类型为uint32
case 4
precision='float32=>float32';%头文件中datatype=4对应ENVI中数据类型为Floating Point,对应MATLAB中数据类型为float32
case 5
precision='double=>double';%头文件中datatype=5对应ENVI中数据类型为Double Precision,对应MATLAB中数据类型为double
otherwise
error('invalid datatype');%除以上几种常见数据类型之外的数据类型视为无效的数据类型
end
%查找数据格式
a=strfind(info,'interleave = ');
b=length('interleave = ');
c=strfind(info,'sensor type');
interleave=[];
for i=a+b:c-1
interleave=[interleave,info(i)];
end
interleave=strtrim(interleave);%删除字符串中的空格
%读取图像文件
fid = fopen(imgfilename, 'r');
data = multibandread(imgfilename ,[lines, samples, bands],precision,0,interleave,'ieee-le');
data= double(data);
end
将上面的代码保存为read_ENVIimagefile.m就可以直接拿来用了。但是还需要一点小的改进,稍后会提到。
2、 Matlab批处理img格式的实现。代码存放于parmcount.m中,代码如下:
clc;
clear;

filestring='E:\郭\实验数据2\*.img';%计算不同的路径中的图像,只需更改这里

%______以下,对于没有显式扩展名的情况,添加扩展名'.img'_________________%
subfile=filestring;
changefile=strrep(subfile,'*.img','');%找到图像所在文件夹
cfile=dir(changefile);%找到该文件夹下所有文件
for k=1:length(cfile)
if (size(strfind(cfile(k).name,'.'))==0)%判断文件名中有没有'.',如果没有意味着没有扩展名
copyfile(strcat(changefile,cfile(k).name),strcat(changefile,strcat(cfile(k).name,'.img')));
%上面这句,为没有'.img'的添加上
%delete(strcat(changefile,cfile(k).name));%删除多余数据,可选
end
end
%_________________________________________________________________%
filedir=strrep(filestring,'*.img','');
file=dir(filestring);
filenum=length(file);
%___以下代码计算每个图像在每个波段的均值,以及每个波段上所有图像均值的均值___%
fid=fopen(strcat(filedir,'均值.txt'),'wt');
fprintf(fid,'%s', '每个图像在每个波段的均值');
total=zeros(52,14);
for k=1:filenum
tempfile=strcat(filedir,file(k).name);
imgdata=read_ENVIimagefile(tempfile);
fprintf(fid,'\n%s',strcat('file',cfile(k).name,':'));
for i=1:14%事先已经知道共有14个波段,这点不好,需要对read_ENVIimagefile进行改造
meanvalue=[];
imgband=imgdata(:,:,i);
[x,y]=size(imgband);
reband=reshape(imgband,x*y,1);
countmean=mean(reband);
total(k,i)=countmean;
countmean=num2str(countmean);
meanvalue=[meanvalue countmean ' '];
meanvalue=num2str(meanvalue);
fprintf(fid,'%s',meanvalue);
end
end
fclose(fid);
fid1=fopen(strcat(filedir,'所有图像均值的均值.txt'),'wt');
fprintf(fid1,'所有图像均值的均值:');
fprintf(fid1,'%s',num2str(mean(total)));
fclose(fid1);
%___以下代码计算每个图像在每个波段的最大值,以及每个波段上所有图像的最大值的均值___%
fid=fopen(strcat(filedir,'最大值.txt'),'wt');
total=zeros(52,14);
for k=1:filenum
tempfile=strcat(filedir,file(k).name);
imgdata=read_ENVIimagefile(tempfile);
fprintf(fid,'\n%s',strcat('file',cfile(k).name,':'));
for i=1:14
maxvalue=[];
imgband=imgdata(:,:,i);
[x,y]=size(imgband);
reband=reshape(imgband,x*y,1);
countmax=max(reband);
total(k,i)=countmax;
countmax=num2str(countmax);
maxvalue=[maxvalue countmax ' '];
maxvalue=num2str(maxvalue);
fprintf(fid,'%s',maxvalue);
end
end
fclose(fid);
fid1=fopen(strcat(filedir,'所有图像最大值的均值.txt'),'wt');
fprintf(fid1,'所有图像最大值的均值:');
fprintf(fid1,'%s',num2str(mean(total)));
fclose(fid1);
%___以下代码计算每个图像在每个波段的最小值,以及每个波段上所有图像的最小值的均值___%
fid=fopen(strcat(filedir,'最小值.txt'),'wt');
total=zeros(52,14);
for k=1:filenum
tempfile=strcat(filedir,file(k).name);
imgdata=read_ENVIimagefile(tempfile);
fprintf(fid,'\n%s',strcat('file',cfile(k).name,':'));
for i=1:14
minvalue=[];
imgband=imgdata(:,:,i);
[x,y]=size(imgband);
reband=reshape(imgband,x*y,1);
countmin=min(reband);
total(k,i)=countmin;
countmin=num2str(countmin);
minvalue=[minvalue countmin ' '];
minvalue=num2str(minvalue);
fprintf(fid,'%s',minvalue);
end
end
fclose(fid);
fid1=fopen(strcat(filedir,'所有图像最小值的均值.txt'),'wt');
fprintf(fid1,'所有图像最小值的均值:');
fprintf(fid1,'%s',num2str(mean(total)));
fclose(fid1);
%___以下代码计算每个图像在每个波段的方差,以及每个波段上所有图像的方差的均值___%
fid=fopen(strcat(filedir,'方差.txt'),'wt');
total=zeros(52,14);
for k=1:filenum
tempfile=strcat(filedir,file(k).name);
imgdata=read_ENVIimagefile(tempfile);
fprintf(fid,'\n%s',strcat('file',cfile(k).name,':'));
for i=1:14
meanvalue=[];
imgband=imgdata(:,:,i);
[x,y]=size(imgband);
reband=reshape(imgband,x*y,1);
countvar=var(reband);
total(k,i)=countvar;
countvar=num2str(countvar);
varvalue=[meanvalue countvar ' '];
varvalue=num2str(varvalue);
fprintf(fid,'%s',varvalue);
end
end
fclose(fid);
fid1=fopen(strcat(filedir,'所有图像方差的均值.txt'),'wt');
fprintf(fid1,'所有图像方差的均值:');
fprintf(fid1,'%s',num2str(mean(total)));
fclose(fid1);
追问
这个是读的头文件吗
光点科技
2023-08-15 广告
通常情况下,我们会按照结构模型把系统产生的数据分为三种类型:结构化数据、半结构化数据和非结构化数据。结构化数据,即行数据,是存储在数据库里,可以用二维表结构来逻辑表达实现的数据。最常见的就是数字数据和文本数据,它们可以某种标准格式存在于文件... 点击进入详情页
本回答由光点科技提供
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式