matlab改变遥感图像的存储格式

实验要求

  1. 编写程序改变图像存储格式。
  2. 编写程序读取按不同格式存储的多波段数据。

实验材料

  • tiff格式图像;
  • jpg格式图像;
  • 标准ENVI格式(img+hdr)图像;
  • 编写好的遥感图像读写函数freadenvi.mfwriteenvi.m函数。

实验代码

程序1 图像按BIL和BIP格式保存

file='peppers.png';
Img=imread(file);

%% 波段分离
[m,n,p]=size(Img);
red=Img(:,:,1);
green=Img(:,:,2);
blue=Img(:,:,3);


%% 按行或列合并矩阵
BIL=[];
BIP=[];
for i=1:m
    BIL=[BIL;red(i,:);green(i,:);blue(i,:)];
end

for i=1:n
    BIP=[BIP,red(:,i),green(:,i),blue(:,i)];
end

%% 按图片格式保存
imwrite(BIL,'BIL.jpg');
imwrite(BIP,'BIP.jpg');


%% 按二进制保存
filename1 = 'peppers_bil.data';
fid = fopen(filename1,'wb');
fwrite(fid,BIL,class(BIL));
fclose(fid);

filename2 = 'peppers_bip.data';
fid = fopen(filename2,'wb');
fwrite(fid,BIP,class(BIP));
fclose(fid);

% 显示结果
figure(1) 
imshow(BIL);
title('BIL');
figure(2) 
imshow(BIP);
title('BIP');

程序二 读取BIL和BIP格式的图像


disp('start........')

m = 384;
n = 512;
p = 3;

%% 读取并显示BIL文件
% read binary data
filename = 'peppers_bil.data';
fid = fopen(filename,'rb');
if fid<0
    error('can not open file');
end

bil = fread(fid,[m*p,n],'uint8=>uint8');
fclose(fid);



%分割(分割步骤不影响后面的合并操作)
Img2=zeros(m,n,p,'uint8');

red=bil(1:p:p*m,:);
green=bil(2:p:p*m,:);
blue=bil(3:p:p*m,:);



%合并
for i=0:m-1
    for j=1:n
        Img2(i+1,j,:)=[bil(p*i+1,j),bil(p*i+2,j),bil(p*i+3,j)];
    end
end
    
%显示结果
figure(1)          % define figure
subplot(2,2,1);     % subplot(x,y,n)x表示显示的行数,y表示列数,n表示第几幅图片
imshow(red);
title('red');
subplot(2,2,2);
imshow(green);
title('green');
subplot(2,2,3);
imshow(blue);
title('blue');
subplot(2,2,4);
imshow(Img2);
title('bil to color');

%% 读取并显示BIP文件
filename = 'peppers_bip.data';
fid = fopen(filename,'rb');
if fid<0
    error('can not open file');
end

bip = fread(fid,[m,n*p],'uint8=>uint8');
fclose(fid);

%分割
red=bip(:,1:3:n*p);
green=bip(:,2:3:n*p);
blue=bip(:,3:3:n*p);

%合并
Img3=zeros(m,n,p,'uint8');
for i=0:n-1
    Img3(:,i+1,:)=[bip(:,i+1),bip(:,i+2),bip(:,i+3)];
end

figure(2)          % define figure
subplot(2,2,1);     % subplot(x,y,n)x表示显示的行数,y表示列数,n表示第几幅图片
imshow(red);
title('red');
subplot(2,2,2);
imshow(green);
title('green');
subplot(2,2,3);
imshow(blue);
title('blue');
subplot(2,2,4);
imshow(Img2);
title('bip to color');

程序3 将图像保存为ENVI格式和TIFF格式

%% 读取并显示图片
fname='myimg.jpg';
myimg=imread(fname);
imshow(myimg);
%显示图片信息
whos myimg

%% 保存ENVI格式
fwriteenvi('myimg.img',myimg);

%% 保存tiff文件
%获取图像大小
[m,n,p]=size(myimg);

tname='myimg.tiff';
t = Tiff(tname, 'w');
% 影像信息
tagstruct.ImageLength = m;
tagstruct.ImageWidth = n;  

% 颜色空间解释方式
tagstruct.Photometric = 1;

% 每个像素的数值位数,这里转换为unit8,所以为8位
tagstruct.BitsPerSample = 8;
% 每个像素的波段个数,一般图像为13,但是对于遥感影像存在多个波段所以常常大于3
tagstruct.SamplesPerPixel = p;
tagstruct.RowsPerStrip = 16;
tagstruct.PlanarConfiguration = Tiff.PlanarConfiguration.Chunky;
% 表示生成影像的软件
tagstruct.Software = 'MATLAB'; 
% 表示对数据类型的解释
tagstruct.SampleFormat = 1;
% 设置Tiff对象的tag
t.setTag(tagstruct);

% 以准备好头文件,开始写数据
t.write(myimg);
% 关闭影像
t.close;

程序4 读取多波段影像并保存为TIFF格式

%% 打开文档
filename = 'bhtmref.img';
fid = fopen(filename,'rb');
if fid<0
    error('can not open file');
end
%按8bit读取
file = fread(fid,[512,inf],'ubit8=>uint8');
fclose(fid);

%转换为三维张量
Img=reshape(file,512,512,6);

%% 按波段进行假彩色合成
colorImg=Img(:,:,2:4);

imshow(colorImg.*5);
filename1='bhtmref.tiff';
t = Tiff(filename1, 'w');
% 影像信息
tagstruct.ImageLength = 512;
tagstruct.ImageWidth = 512;  

% 颜色空间解释方式
tagstruct.Photometric = 1;

% 每个像素的数值位数,这里转换为unit8,所以为8位
tagstruct.BitsPerSample = 8;
% 每个像素的波段个数,一般图像为1或3,但是对于遥感影像存在多个波段所以常常大于3
tagstruct.SamplesPerPixel = 3;
tagstruct.RowsPerStrip = 16;
tagstruct.PlanarConfiguration = Tiff.PlanarConfiguration.Chunky;
% 表示生成影像的软件
tagstruct.Software = 'MATLAB'; 
% 表示对数据类型的解释
tagstruct.SampleFormat = 1;
% 设置Tiff对象的tag
t.setTag(tagstruct);

% 以准备好头文件,开始写数据
t.write(colorImg);
% 关闭影像
t.close;






%% 重采样并保存
newImg=Img(1:2:512,1:2:512,[1,3,4,5]);

filename2 = 'bhtmref_new.img';
fwriteenvi(filename2,newImg);

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

高堂明镜悲白发

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值