NJU1healer的个人博客分享 http://blog.sciencenet.cn/u/NJU1healer

博文

Matlab之光谱预处理及相关操作、插值、坐标轴边框设置(全框)

已有 4576 次阅读 2020-9-14 10:54 |个人分类:matlab|系统分类:科研笔记

1.归一化处理,分为均值归一化(mapminmax)和标准化(mapstd)

    1.1mapminmax处理,按行逐行将数据归一化到-1-1,若6次采集的549波段的高光谱数据,如矩阵A为549*6,直接mapminmax(A),表示对于每一个波段,将不同批次采集的数据归一化,消除掉采集时外界因素对单波段的影响;

    1.2mapstd处理,同mapminmax,按行逐行将数据标准化到0-1;

PS:对于mapstd处理,如果后续要对数据再微分,因为包含0值,所以可以加减极小值eps,若已经存在eps,则加减eps*0.5;

2.微分diff

按照列对光谱微分,单列表示一个完整的光谱曲线,行表示波段个数;同样,以A为例,diff(A)表示对A按行一阶微分,diff(A,1,2)表示对A按列进行一阶微分,“1”表示几阶微分,“2”表示列,当该位置为“1”时表示行。

3.平滑smooth

smooth表示按列对光谱进行曲线平滑,每列为一个完整光谱,行表示波段个数;

直接smooth(A),表示默认为5*5窗口使用滑动平均法'moving'平滑曲线;

smooth平滑后会变成多行一维向量,根据需求重组成光谱形式;

点滴分享,福泽你我!Add oil!

4.MATLAB中常用的排列、组合、阶乘函数

(1)求n的阶乘,方法如下:
a、factorial(n)
b、gamma(n+1)
c、v='n!'; vpa(v)

(2)求组合(数),方法如下:
a、combntns(x,m)    列举出从n个元素中取出m个元素的组合。其中,x是含有n个元素的向量。
b、nchoosek(n,m)    从n各元素中取m个元素的所有组合数。
nchoosek(x,m)    从向量x中取m个元素的组合

(3)求排列(数),方法如下:
a、perms(x)         给出向量x的所有排列。
b、prod(n:m)        求排列数:m*(m-1)*(m-2)*…*(n+1)*n
prod(1:2:2n-1)    求(2n-1)!!
prod(2:2:2n)        求(2n)!!
prod(A)                对矩阵A的各列求积
prod(A,dim)         dim=1(默认);dim=2,对矩阵A的各行求积(等价于(prod(A'))')

 

(4)函数 cumprod()    ----累积求积函数:
cumprod(n:m)        输出一个向量[n n*(n+1) n(n+1)(n+2) … n(n+1)(n+2)…(m-1)m]
cumprod(A)            若A为矩阵:输出同维数的矩阵,按列累积求积
cumprod(A,dim)    A为矩阵,dim=1或2,dim=1,默认,与上面一样;dim=2,按行累积求积。

5.matlab读取高光谱影像

1.函数multibandread读取读取多波段二进制影像文件(ENVI主菜单file—save file as—envi standard得到的就是二进制影像文件,有时甚至会看到后缀名为bsq、bil、bip等影像)。

 im_hyper = multibandread(filename, size, precision, offset, interleave, byteorder)

                                                后附参考代码有实例

       filename: 文件名

       size:图像尺寸和波段数,size = [ 行数 列数 波段数 ]

       precision:读取的图像的数据格式,例如'uint8','uint16','double'等

       offset:偏移(一般为0)

       interleave:存储的图像的数据格式,有 bsq,bil,bip三种格式

       byteorder : 数据存储的字节排列方式,有'ieee-le'(小端),'ieee-be'(大端)

 

注:precision参数与matlab数据类型相应的关系(FORM https://malagis.com/use-matlab-read-envi-image.html,辛苦博主整理。)

    (

  precision='uint8=>uint8';%头文件中datatype=1对应ENVI中数据类型为Byte,对应MATLAB中数据类型为uint8

  precision='int16=>int16';%头文件中datatype=2对应ENVI中数据类型为Integer,对应MATLAB中数据类型为int16

  precision='uint16=>uint16';%头文件中datatype=12对应ENVI中数据类型为Unsighed Int,对应MATLAB中数据类型为uint16

  precision='int32=>int32';%头文件中datatype=3对应ENVI中数据类型为Long Integer,对应MATLAB中数据类型为int32

  precision='uint32=>uint32';%头文件中datatype=13对应ENVI中数据类型为Unsighed Long,对应MATLAB中数据类型为uint32

  precision='float32=>float32';%头文件中datatype=4对应ENVI中数据类型为Floating Point,对应MATLAB中数据类型为float32

  precision='double=>double';%头文件中datatype=5对应ENVI中数据类型为Double Precision,对应MATLAB中数据类型为double

 

2. 如果是.tif格式的图像,可先使用envi将其转化为envi standard 格式,然后使用enviread读取

ENVI软件标准格式的高光谱遥感图像含有两部分:

  一个是高光谱图像 '*.img ',

  另一个是遥感图像头文件 '*.hdr',该文件记录了遥感图像的信息,如图像尺寸、波段数、数据类型和大小端等。如果缺少头文件,将无法对遥感图像进行读取

读取代码:来源:https://malagis.com/use-matlab-read-envi-image.html, 亦可参考:http://blog.sina.com.cn/s/blog_16ecb33400102ybql.html

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

6.matlab插值函数及插值方法

matlab自带的插值函数interp1的四种插值方法

MAtlab最全插值,拿去不谢!

matlab的插值方法  (推荐,不错)

7.坐标轴全框显示

ax = gca;
ax.BoxStyle = 'full';

image.png

更多设置参考:https://ww2.mathworks.cn/help/matlab/ref/box.html

【参考】

https://www.cnblogs.com/shyzh/p/8675444.html



https://wap.sciencenet.cn/blog-3428464-1250494.html

上一篇:Anaconda 报错Multiple Errors Encountered和添加国内镜像以及换源和恢复默认源
下一篇:Python代码调试错误集合(1)
收藏 IP: 211.162.81.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...
扫一扫,分享此博文

全部作者的其他最新博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-18 16:22

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部