% training process
function train

  % basic values
  global numlines datalines currentimage

  % see if using live data
  % TEEDATA1/goodT - 3 errors on rotation into std orientation - 31 good
  imagestem = input('Test image file stem (filestem)\n?','s');
  
  % training phase
  datacount = 0;
  maximages = 31;
  for imagenum = 1 : maximages
  imagenum
     currentimagergb = imread([imagestem,int2str(imagenum),'.jpg'],'jpg');
     currentimage = rgb2gray(currentimagergb);


     % get corners using TASK1's method

     Image = getbinary(currentimage,0,0,0,0,0);
     [H,W] = size(Image);
     [r,c] = find( bwperim(Image,4) == 1 );

     [sr,sc] = removespurs(r,c,H,W,0);          %fig7
     [tr,tc] = boundarytrack(sr,sc,H,W,0);      %fig8

     datalines = zeros(100,4);         % space for results
     numlines = 0;
     findcorners(tr,tc,H,W,9,16);

     if numlines == 8
       % rotate datalines to standard position
       [newdatalines,flag] = standard_position(datalines(1:numlines,:),numlines,11);
        pause(0.5)
  
       % get vertices
       if flag
         % sort vertices into a standard order
         sortvertices = sortvert(newdatalines(1:numlines,1:2));
  
         % add to scatter matrix
         [Vnum,Vcoord] = size(sortvertices);
         if Vnum == 8
           tmp = reshape(sortvertices,1,Vnum*Vcoord);   % turn points into long array
           datacount = datacount + 1;               % accumulate
           allvertices(datacount,:) = tmp;
         end
       end
     end
  end

  % Create model
  meanvertex = mean(allvertices)
  vertexdev = allvertices - ones(datacount,1)*meanvertex;
  scatter = vertexdev'*vertexdev;
  [U,D,V]= svd(scatter);
  modeldev = V(:,1:5)'               % use only first 5 components

  figure(13)
  clf
  hold on
  for k = 1 : 16
   tmp(k) = D(k,k);
  end
  plot(tmp,'-b*')


  %figure(14)
  %clf
  %hold on
  %tmp1 = vertexdev(:,1);
  %tmp3 = vertexdev(:,3);
  %plot(tmp1,tmp3,'*')

  % get projections onto data
  vecs = vertexdev*modeldev';


  figure(15);
  clf;
  hold on;
  tmp1 = vecs(:,1);
  tmp3 = vecs(:,3) ; 
  plot(tmp1,tmp3,'*');

  % get class mean vector and covariance matrix
  [Mean,Invcor] = buildmodel(vecs,maximages);

  % save training data
  save modelfile modeldev meanvertex Mean Invcor
                
  % draw some examples illustrating eigenvectors
  scale = 30;
  figure(12);
  clf;
  axis ij;
  hold on;
  draweigenmodel(meanvertex,modeldev(1:4,:),[-2,0,0,0],scale,000,000);
  draweigenmodel(meanvertex,modeldev(1:4,:),[-1,0,0,0],scale,000,500);
  draweigenmodel(meanvertex,modeldev(1:4,:),[1,0,0,0],scale,000,1000);
  draweigenmodel(meanvertex,modeldev(1:4,:),[2,0,0,0],scale,000,1500);

  draweigenmodel(meanvertex,modeldev(1:4,:),[0,-2,0,0],scale,500,000);
  draweigenmodel(meanvertex,modeldev(1:4,:),[0,-1,0,0],scale,500,500);
  draweigenmodel(meanvertex,modeldev(1:4,:),[0,1,0,0],scale,500,1000);
  draweigenmodel(meanvertex,modeldev(1:4,:),[0,2,0,0],scale,500,1500);

  draweigenmodel(meanvertex,modeldev(1:4,:),[0,0,-2,0],scale,1000,000);
  draweigenmodel(meanvertex,modeldev(1:4,:),[0,0,-1,0],scale,1000,500);
  draweigenmodel(meanvertex,modeldev(1:4,:),[0,0,1,0],scale,1000,1000);
  draweigenmodel(meanvertex,modeldev(1:4,:),[0,0,2,0],scale,1000,1500);

  draweigenmodel(meanvertex,modeldev(1:4,:),[0,0,0,-2],scale,1500,000);
  draweigenmodel(meanvertex,modeldev(1:4,:),[0,0,0,-1],scale,1500,500);
  draweigenmodel(meanvertex,modeldev(1:4,:),[0,0,0,1],scale,1500,1000);
  draweigenmodel(meanvertex,modeldev(1:4,:),[0,0,0,2],scale,1500,1500);
   
