• 大小: 3KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-03-25
  • 语言: Matlab
  • 标签: 高光谱  读取  

资源简介

包含高光谱图像读取及格式转换,端元提取算法N-FINDR

资源截图

代码片段和文件信息

function N_FINDR()

% read a hyperspectral image from sample data of ENVI.
nlines  = 254;
npixels = 3454;
nbands  = 37;
nlength = nlines*npixels;
%hs_img  = multibandread(‘cup95eff.int‘ [nlines npixels nbands] ...
                       % ‘int16‘ 0 ‘bil‘ ‘ieee-le‘); %从二进制文件中读取BSQBILBIP数据
%hs_img= multibandread(‘F:\285-destripe_new_flaash_wv_sg_37‘[nlines npixels nbands]...
 %                     ‘double‘0‘bsq‘‘ieee-be‘);%从二进制文件中读取BSQ文件
test_hdr=‘F:\test_img.hdr‘;%头文件的地址
maindata = ReadImg(test_hdr);%读取头文件相应的.img高光谱数据
hs_img=maindata;

% reduct the dimensionality of image to be one less the number of end-
% mumbers by using PCA.
% standardize the data by dividing each column by its standard deviation.
hs_img = reshape(hs_img [] nbands);%改变矩阵的形状,但是元素个数不变
meanh  = mean(hs_img);%求均值
stdh   = std(hs_img);%求标准差
sd_img = (hs_img-repmat(meanhnlength1))./repmat(stdhnlength1);%repmat:将meanh作为元素复制nlength*1块%标准化图像
[pcoef score latent] = princomp(sd_img);
                                       %做PCA。~是对主分的打分也就是原矩阵在主成分空间的表示,pcoef是元矩阵对应的协方差矩阵对的所有特征向量,latent协方差矩阵的特征值
                                       %latent是向量还是矩阵?

% determine the number of endmembers by calculating the contribution of
% %计算主成分的贡献来确定端元的数量
% principal components.
perc = cumsum(latent)/sum(latent)*100;%cumsum计算一个数据各行的累加值返回值与latent行列相同,sum是将latent的所有元素相加
Nend = sum(perc<99.5)+1;%确定端元的数量
% get the principal components.确定主成分的贡献量
pca_img = sd_img*pcoef(:1:Nend-1);

% repeat 50 times to find the endmembers with largest
% volume.%迭代50次找到最大体积的端元
Ntimes = 50;
locs   = zeros(NtimesNend);
V_max  = zeros(Ntimes1);
for i = 1:Ntimes%tic和toc计算程序的时间
   
    [locs(i:) V_max(i1)] = finder(pca_img nlength Nend);%N-finder的函数
   
end
[score ind] = max(V_max);
loc = locs(ind :);

% calculate the abundance of each endmember for each
% pixel.计算对于每个像素的每个端元的丰度(最小二乘法)
M   = ones(Nend);
M(2:end:) = pca_img(loc:)‘;
C = zeros(Nend nlength);
for i = 1:nlength
    p = [1; pca_img(i:)‘];
    C(:i) = lsqnonneg(M p);%返回C>=0约束下norm(M*C-p)的最小值向量
%     C(:i) = lsqlin(E p [][][][]01);
end

save(‘F:\NFINDR_res.txt‘‘C‘‘-ASCII‘);

%stop;

% end function N_FINDR.
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [loc V_max] = finder(pca_img nlength Nend)

% randomly select pixels as initial endmembers.随机选择像素作为初始端元,求单形体的体积
ind = unidrnd(nlength 1 Nend);%产生从1到nlength所指定的最大数数之间的离散均匀随机数,生成1*nend矩阵
E   = ones(Nend);%产生nend*nend全是1的数组
E(2:end:) = pca_img(ind:)‘;%随机选择nend个像素组成最初的端元矩阵
dentor = factorial(Nend-1);%求(端元-1)的阶乘
V_max  = abs(det(E))/dentor;%求E的行列式在取绝对值最后除以dentor。求体积

% find the largest volume and set the corresponding pixels as
% endmembers.找到最大的体积,并设置相应的像素作为端元
for i = 1:Nend  % loop over each endmember.循环每个端元
    i_max = ind(i);
    for j = 1:nlength  % loop over each pixel.循环每个像素
        E(2:endi) = pca_img(j:)‘;
        V = abs(det(E))/dentor;
      

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件        7865  2014-08-28 10:01  N_FINDR.m

评论

共有 条评论