rtsEnviLoadTraining.m 1.27 KB
function [F, C] = rtsEnviLoadTraining(filenames, classnames, binfile, ppc)

Nc = length(filenames);     %calculate the number of classes

if nargin < 3
    ppc = 0;
end

%---------Load the Masks-----------
%first, get the total number of pixels
Ns = 0;
for c = 1:Nc
   %load the mask
    image = imread(filenames{c});
    
    if c == 1
        mask = boolean(zeros(size(image, 2), size(image, 1), Nc));
    end
    
    if ppc ~= 0
        mask(:, :, c) = rtsRandomizeMask(image(:, :, 1)' > 0, ppc);
    else
        mask(:, :, c) = image(:, :, 1)' > 0;
    end
    
end

%calculate the number of samples
Ns = Ns + nnz(mask);    

%-----------Load the Training Data-------------
disp(['loading ' num2str(Ns) ' samples']);
ci = 1;
for c = 1:Nc
    
    disp(['loading class ' classnames(c) '...']);
    batch = nnz(mask(:, :, c));
    %create an ENVI file object
    envi = rtsEnviOpen(binfile, [binfile '.hdr'], mask(:, :, c));
    
    if c == 1
        F = zeros(Ns, envi.header.bands);
        C(Ns, 1) = char(0);
    end

    %read a bunch of spectra
    F(ci:ci+batch - 1, :) = rtsEnviRead(envi, batch)';
    C(ci:ci+batch - 1, 1) = repmat([classnames(c)], [batch 1]);
    
    %update the current index
    ci = ci+batch;

    %close the ENVI file
    rtsEnviClose(envi);
    
    disp('done');
end