% testing process
function test

  % basic values
  global numlines datalines currentimage

  % parameters
  curvesegmentthresh=16;
  
  % see if using live data
  % TEEDATA1/goodT - 3 errors on rotation into std orientation - 31 good
  % Samples: 31 Mean: 2.1634 Max: 2.7836 Min: 1.0012
  % TEEDATA1/badT - 12 trials: 4 not find 8 vert,  2 bad orient - 6 worked
  % 1 dist = 2.7 < 5.9, 5.9, 8.5, 9.3, 5.5
  % Samples: 6 Mean: 6.2829 Max: 9.3174 Min: 2.6774

  imagestem = input('Test image file stem (filestem)\n?','s');
  load modelfile modeldev meanvertex Mean Invcor
  
  % testing phase
  datacount = 0;
  resultcount = 0;
  maximages = 31; % ************* change to 12 to use TEEDATA1/badT data
  dists=zeros(maximages,1);
  for imagenum = 1 : maximages
imagenum
     currentimagergb = imread([imagestem,int2str(imagenum),'.jpg'],'jpg');
     currentimage = rgb2gray(currentimagergb);



     % threshold using TASK1's method
     
     Image = getbinary(currentimage,0,0,0,0,0);
     [H,W] = size(Image);
     
     % extract properties
     [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,curvesegmentthresh);

     if numlines == 8
       % rotate datalines to standard position
       [newdatalines,flag] = standard_position(datalines(1:numlines,:),numlines,11);

       % get vertices
       if flag
         vertices = newdatalines(1:numlines,1:2);

         % sort vertices into a standard order
         sortvertices = sortvert(vertices);
         [Vnum,Vcoord] = size(sortvertices);
         if Vnum == 8
           tmp = reshape(sortvertices,1,Vnum*Vcoord);   % turn points into long array
           % project onto eigenvectors
           vec = (tmp - meanvertex)*modeldev';

           % compute Mahalanobis distance for this class
           resultcount = resultcount + 1;
           dists(resultcount) = mahalanobis(vec',Mean,Invcor);
           dists(resultcount)
           bad1(resultcount) = vec(1);
           bad2(resultcount) = vec(2);
         end
       end
     end

  end
dists(1:resultcount)

  ['Samples: ',int2str(maximages), ...
        ' Mean: ',num2str(mean(dists(1:resultcount)')), ...
        ' Max: ',num2str(max(dists(1:resultcount)')), ...
        ' Min: ',num2str(min(dists(1:resultcount)'))]

%% see tmp3 and tmp1 def. in train.m

tmp3 = [ ...
    4.8353 ...
  -55.5626 ...
   36.8645 ...
  -24.4392 ...
  -26.1701 ...
  -13.6939 ...
    0.1787 ...
    8.8398 ...
  -41.4225 ...
  -15.8559 ...
  -22.3312 ...
    7.2909 ...
   28.9201 ...
   18.5763 ...
   34.4772 ...
    4.2131 ...
  -36.0414 ...
   41.5251 ...
  -11.9777 ...
   29.6781 ...
    9.7486 ...
   25.5346 ...
  -29.8954 ...
  -38.8672 ...
   58.1417 ...
   19.1553 ...
  -14.8373 ...
  -43.9302 ...
   39.4884 ...
    1.6557 ...
    5.9013];

tmp1 = [ ...
  -40.6893 ...
  -47.9650 ...
  -15.2516 ...
   28.9860 ...
  -17.9227 ...
  -17.7397 ...
    6.3251 ...
  -56.2792 ...
   16.9555 ...
  -23.4351 ...
   45.0676 ...
  -12.6293 ...
  -19.9519 ...
   11.9728 ...
  -33.2835 ...
   56.8962 ...
  -41.9648 ...
   19.3866 ...
  -21.7725 ...
  -45.9377 ...
   -8.6102 ...
   13.6238 ...
   46.4691 ...
   29.0409 ...
   28.0127 ...
    5.0884 ...
  -17.0777 ...
   19.3831 ...
   27.2182 ...
   64.8675 ...
    1.2168];
    
  figure(15)
  clf
  hold on
  plot(tmp1,tmp3,'k*')
  plot(bad1,bad2,'rd')

  figure(14)
  clf
  hold on
  plot(sort(dists),'k*')
  axis([0 31 0 3])
dists
