Blame view

matlab/rtsEnviLoadTraining.m 1.27 KB
1ff9af85   David Mayerich   added MATLAB/OCTA...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  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