加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

Matlab读取envi图像img格式(批处理)

(2012-03-20 16:42:13)
标签:

杂谈

最近在忙一件事情,计算上百个遥感多波段图像中每个波段的均值、方差、最大值、最小值。如果用ENVI软件来统计,有点麻烦。所以就用Matlab编了一段小程序。

试验:Matlab读取ENVI标准图像(IMG+Hdr),并计算相关系数。

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);

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有