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

(转)Matlab读取遥感影像

(2013-05-28 15:19:47)
标签:

matlab

遥感

分类: 学习笔记

fname    = 'spotbsq.img';
% 输入图像(可为任意后缀,或无后缀),必须包含头文件
elements = {'samples ' 'lines   ' 'bands   ' 'data type '};

%请注意此处的cell写法,打开*.hdr比对下,否则空格错误,将读不到需要的数据

datatype = {'uint8' 'int16' 'int32' 'float32' 'float64' 'uint16' 'uint32' 'int64' 'uint64'};

% 注意ENVI和MATLAB数据类型的对应关系,参笔者其它博文(有关两种数据类型的对比)

http://hi.baidu.com/yangfanxing__/blog/item/dd3d2403f96da082d43f7c97.html

% Check user input
if ~ischar(fname)
    error('fname should be a char string');
end

% Open ENVI header file to retreive s, l, b & datatype variables
corename = strtok(fname,'.');
% 去掉后缀
rfid = fopen(strcat(corename,'.hdr'),'r');
% 添加头文件后缀,读取

% Check if the header file is correctely open
if rfid == -1
    error('Input header file does not exist');
end;
% Read ENVI image header file and get
% sample : nb samples
% line : nb lines
% bands : nb bands and
% t : data type
while 1
    tline = fgetl(rfid);
    if ~ischar(tline), break, end
    [first,second]=strtok(tline,'=');
    
    switch first
        case elements(1)
            [f,s]=strtok(second);
            sample=str2num(s);
        case elements(2)
            [f,s]=strtok(second);
            line=str2num(s);
        case elements(3)
            [f,s]=strtok(second);
            bands=str2num(s);
        case elements(4)
            [f,s]=strtok(second);
            t=str2num(s);
            switch t
                case 1
                    t=datatype(1);
                case 2
                    t=datatype(2);
                case 3
                    t=datatype(3);
                case 4
                    t=datatype(4);
                case 5
                    t=datatype(5);
                case 12
                    t=datatype(6);
                case 13
                    t=datatype(7);
                case 14
                    t=datatype(8);
                case 15
                    t=datatype(9);
                otherwise
                    error('Unknown image data type');
            end
    end
end
fclose(rfid);

t=t{1,1};
% Open the ENVI image and store it in the 'image' MATLAB array
disp([('Opening: '),...
    (num2str(sample)),(' cols × '),(num2str(line)),(' lines × '),(num2str(bands)),(' bands ')...
    ('of type '),('"'),(t),('"'),(' image...')]);

fid=fopen(fname);
image=fread(fid,t);
fclose(fid);
% 关闭文件
% 显示图像
image_m=reshape(image,[sample,line,bands]); %这种读文件的方法使图像倒置了,image为一维列矩阵
imshow(uint8(image_m'));%转置并显示图像

【一个典型的遥感图像头文件】spotbsq.hdr

ENVI
description = {
Display Output to Image [Tue Oct 20 10:16:14 2009]}
samples = 2000
lines   = 1000
bands   = 1
header offset = 0
file type = ENVI Standard
data type = 1
interleave = bsq
sensor type = Unknown
byte order = 0
map info = {UTM, 1.000, 1.000, 468034.000, 4441479.000, 1.0000000000e+001, 1.0000000000e+001, 13, North, units=Meters}
wavelength units = Unknown
default stretch = 0.000000 255.000000 linear
band names = {
Gray Scale (Georeferenced SPOT):bldr_sp.img)}

0

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

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

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

新浪公司 版权所有