『公告』 预祝您龙年大吉,万事如意, 过节期间, 大家如需数据服务,请拨打400 或直接添加客服微信,再祝大家龙年,心想事成。
关注我们 新浪 腾讯

MatLab读取ENVI图像统计多波段图像信息

MatLab读取ENVI图像统计多波段图像信息
在ENVI统计遥感多波段图像中每个波段的均值、方差、最大值、最小值是比较容易办到的,但是如果要处理多批的数据就没有那么方便了,这里转载一个MatLab读取ENVI图像(img+hdr)的程序,并且计算了相关系数。

       在ENVI统计遥感多波段图像中每个波段的均值、方差、最大值、最小值是比较容易办到的,但是如果要处理多批的数据就没有那么方便了,这里转载一个MatLab读取ENVI图像(img+hdr)的程序,并且计算了相关系数。

 

       之前我在利用MatLab读取ENVI图像里分享了一个MatLab读取ENVI图像的函数,这里可以用上这个函数,具体的请查看上篇文章。

 

       在新的.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);

      京ICP备2025132830号-1 京公网安备 号