function [finalTrack] = correctTracks7(actFile, calFile)

warning off 'all';
modelSize = 49;
cst = 0.166;

dirBase = dir([actFile '.1.*.pgm']);
numberFrames = size(dirBase, 1);

trackName = [actFile '.finalTrack.mat'];
eval(['load ' trackName ' finalTrack;']);    

cleanName = [actFile '.cleanTrack.mat'];
eval(['load ' cleanName ' cleanTrack;']);    

% create model rigidity matrix
modelFile = ['model.dat'];
model = load(modelFile);
numEdge = size(model, 1); 
rigid = zeros(modelSize, modelSize);
for mk=1:numEdge
  rigid(model(mk, 1), model(mk, 2)) = 1;
end;

pointsName = [actFile '.points.mat'];
eval(['load '  pointsName ' humanBody ptNumber mkCode;']);

% create model distance matrix
links = zeros(modelSize, modelSize);
for mk=1:numEdge
  dst = 0;
  tot = 0;
  for frm=1:numberFrames
    if (finalTrack(model(mk, 1), frm)>0 && finalTrack(model(mk, 2), frm)>0)
      X(1) = humanBody(1, finalTrack(model(mk, 1), frm), frm);
      X(2) = humanBody(1, finalTrack(model(mk, 2), frm), frm);
      Y(1) = humanBody(2, finalTrack(model(mk, 1), frm), frm);
      Y(2) = humanBody(2, finalTrack(model(mk, 2), frm), frm);
      Z(1) = humanBody(3, finalTrack(model(mk, 1), frm), frm);
      Z(2) = humanBody(3, finalTrack(model(mk, 2), frm), frm);
      dx = X(1) - X(2);
      dy = Y(1) - Y(2);
      dz = Z(1) - Z(2);
      dst = dst + sqrt(dx*dx + dy*dy + dz*dz);
      tot = tot+1;
    end;
  end;
  links(model(mk, 1), model(mk, 2)) = dst/tot;
end;

errors = zeros(modelSize, numberFrames);
bigErr = zeros(modelSize, numberFrames);
for mk=1:numEdge
  if (model(mk, 1) < model(mk, 2))
    for frm=1:numberFrames
      if (finalTrack(model(mk, 1), frm)>0 && finalTrack(model(mk, 2), frm)>0)
        X(1) = humanBody(1, finalTrack(model(mk, 1), frm), frm);
        X(2) = humanBody(1, finalTrack(model(mk, 2), frm), frm);
        Y(1) = humanBody(2, finalTrack(model(mk, 1), frm), frm);
        Y(2) = humanBody(2, finalTrack(model(mk, 2), frm), frm);
        Z(1) = humanBody(3, finalTrack(model(mk, 1), frm), frm);
        Z(2) = humanBody(3, finalTrack(model(mk, 2), frm), frm);
        dx = X(1) - X(2);
        dy = Y(1) - Y(2);
        dz = Z(1) - Z(2);
        rate = sqrt(dx*dx + dy*dy + dz*dz)/links(model(mk, 1), model(mk, 2));
        if (rate < 1-cst || rate > 1+cst)
          errors(model(mk, 1), frm) = errors(model(mk, 1), frm)+1;
          errors(model(mk, 2), frm) = errors(model(mk, 2), frm)+1;
        end;
        if (rate > 3)
          bigErr(model(mk, 1), frm) = 1;
          bigErr(model(mk, 2), frm) = 1;
        end;
      end;
    end;
  end;
end;

for cam=1:8
  camFile = [calFile sprintf('%d', cam) '.Pmat.cal'];
  Pe(:, :, cam) = load(camFile);
end;  

errors = (errors>1);

for trk=1:modelSize
    
  fprintf('------------------------\n');
  fprintf('Correcting Marker %2d: ', trk);
  for f=1:numberFrames
    fprintf('%d', errors(trk, f));
  end;
  fprintf('\n------------------------\n');    
    
  for frm=1:numberFrames
    if ((errors(trk, frm) && ~cleanTrack(trk, frm)) || bigErr(trk, frm))
      fprintf('Frame %2d.\n', frm);
      if (bigErr(trk, frm)) fprintf('Big error.\n', frm); end;
      trackName = [actFile '.finalTrack.mat'];
      eval(['save '  trackName ' finalTrack;']);    

      cleanName = [actFile '.cleanTrack.mat'];
      eval(['save '  cleanName ' cleanTrack;']);          
      
      pointsName = [actFile '.points.mat'];
      eval(['save '  pointsName ' humanBody ptNumber mkCode;']);      
        
      %%% HELP
      
      if (finalTrack(trk, frm)>0)
        x = humanBody(:, finalTrack(trk, frm), frm);
      else
        x(1, 1) = 0;
        x(2, 1) = 0;
        x(3, 1) = 0;            
        x(4, 1) = 1;                  
      end;

      xT(1, 1) = 0;
      xT(2, 1) = 0;
      xT(3, 1) = 0;            
      xT(4, 1) = 1;                
      if (frm-1>0 && ~errors(trk, frm-1) && frm+1<=numberFrames && ~errors(trk, frm+1))
        cur = frm-1;
        prv = frm+1;
        if (finalTrack(trk, cur)>0 && finalTrack(trk, prv)>0)
          xT(1, 1) = (humanBody(1, finalTrack(trk, cur), cur) + humanBody(1, finalTrack(trk, prv), prv))/2;
          xT(2, 1) = (humanBody(2, finalTrack(trk, cur), cur) + humanBody(2, finalTrack(trk, prv), prv))/2;
          xT(3, 1) = (humanBody(3, finalTrack(trk, cur), cur) + humanBody(3, finalTrack(trk, prv), prv))/2;            
          xT(4, 1) = 1;                    
        end;
      elseif (frm+2<=numberFrames && ~errors(trk, frm+1) && ~errors(trk, frm+2))
        cur = frm+1;
        prv = frm+2;
        if (finalTrack(trk, cur)>0 && finalTrack(trk, prv)>0)        
          xT(1, 1) = humanBody(1, finalTrack(trk, cur), cur) + (humanBody(1, finalTrack(trk, cur), cur) - humanBody(1, finalTrack(trk, prv), prv));
          xT(2, 1) = humanBody(2, finalTrack(trk, cur), cur) + (humanBody(2, finalTrack(trk, cur), cur) - humanBody(2, finalTrack(trk, prv), prv));
          xT(3, 1) = humanBody(3, finalTrack(trk, cur), cur) + (humanBody(3, finalTrack(trk, cur), cur) - humanBody(3, finalTrack(trk, prv), prv));            
          xT(4, 1) = 1;          
        end;
      elseif (frm-2>0)
        cur = frm-1;
        prv = frm-2;
        if (finalTrack(trk, cur)>0 && finalTrack(trk, prv)>0)        
          xT(1, 1) = humanBody(1, finalTrack(trk, cur), cur) + (humanBody(1, finalTrack(trk, cur), cur) - humanBody(1, finalTrack(trk, prv), prv));
          xT(2, 1) = humanBody(2, finalTrack(trk, cur), cur) + (humanBody(2, finalTrack(trk, cur), cur) - humanBody(2, finalTrack(trk, prv), prv));
          xT(3, 1) = humanBody(3, finalTrack(trk, cur), cur) + (humanBody(3, finalTrack(trk, cur), cur) - humanBody(3, finalTrack(trk, prv), prv));            
          xT(4, 1) = 1;
        end;
      end;
      
      for cam=1:8
        frmFile = [actFile '.' sprintf('%d', cam)  '.' sprintf('%05d', frm-1) '.pgm'];
        Io = imread(frmFile, 'pgm');
        dim = size(Io);
        subplot(2, 4, cam), imshow(rot90(Io));
        hold on;
        A1 = Pe(:, :, cam);
          
        p1 = A1*x;
        p1 = p1./(p1(3, 1));
        plot(p1(2,1), dim(2)-p1(1,1), 'ro');          
              
        p1 = A1*xT;
        p1 = p1./(p1(3, 1));
        plot(p1(2,1), dim(2)-p1(1,1), 'bo');                        
      end;            
      in = input('(0-Red | 1-Blue | 2-None): ');
      clf;                    

      if (in == 0)
      elseif (in == 1)
        ptNumber(frm) = ptNumber(frm)+1;
        humanBody(:, ptNumber(frm), frm) = xT;
        finalTrack(trk, frm) = ptNumber(frm);                                            
      else
        clPt = 0;
        pass = 0;
        while (clPt < 2 && pass < 2)
          fprintf('Please, select at least two points.\n');                                                                                                          
          pC = zeros(2, 8);
          pass = pass+1;
          click = 0;
          for cam=1:8
            frmFile = [actFile '.' sprintf('%d', cam)  '.' sprintf('%05d', frm-1) '.pgm'];
            Io = double(imread(frmFile, 'pgm'));
            imshow(Io/255);
            pause;
            [xC, yC, button] = ginput(1);
            if (button == 3)
              pC(1, cam) = xC;
              pC(2, cam) = yC;
              click = click+1;
            elseif (button == 2)
              pass = 2;
              break;              
            end;
            if (click == 3) break; end;
          end;
          mkBin = (pC(1,:) > 0);
          clPt = sum(mkBin);
        end;
        %%% RESET    
                
        mkBin = (pC(1,:) > 0);
        if (sum(mkBin) >= 2)
          camInd = find(mkBin);
          A1 = Pe(:, :, camInd(1)); 
          A = A1;
          b1(1, 1) = pC(1, camInd(1));
          b1(2, 1) = pC(2, camInd(1));
          b1(3, 1) = 1;
          b = b1;
          for ind=2:sum(mkBin)
            A1 = Pe(:, :, camInd(ind)); 
            A = [A A1];
            b1(1, 1) = pC(1, camInd(ind));
            b1(2, 1) = pC(2, camInd(ind));
            b1(3, 1) = 1;
            b = [b ; b1];
          end;
          
          x = uP2X(b, A);                    
          ptNumber(frm) = ptNumber(frm)+1;
          humanBody(:, ptNumber(frm), frm) = x;
          finalTrack(trk, frm) = ptNumber(frm);           

          %fprintf('Reset with %d markers.\n', sum(mkBin));
          for cam=1:8            
            frmFile = [actFile '.' sprintf('%d', cam)  '.' sprintf('%05d', frm-1) '.pgm'];
            Io = imread(frmFile, 'pgm');
            dim = size(Io);
            subplot(2, 4, cam), imshow(rot90(Io));
            hold on;
            A1 = Pe(:, :, cam);
          
            p1 = A1*x;
            p1 = p1./(p1(3, 1));
            plot(p1(2,1), dim(2)-p1(1,1), 'ro');          
          end;
          pause;
          clf;
        else
          finalTrack(trk, frm) = -1;
          
        end;              
        %%% RESET              
        
        cleanTrack(trk, frm) = 1;
        
        trackName = [actFile '.finalTrack.mat'];
        eval(['save '  trackName ' finalTrack;']);    

        cleanName = [actFile '.cleanTrack.mat'];
        eval(['save '  cleanName ' cleanTrack;']);    

        pointsName = [actFile '.points.mat'];
        eval(['save '  pointsName ' humanBody ptNumber mkCode;']);            
        
      end;
      %%% HELP
      errors(trk, frm) = 0;
      bigErr(trk, frm) = 0;
    end;
  end;
end;

trackName = [actFile '.finalTrack.mat'];
eval(['save '  trackName ' finalTrack;']);    

cleanName = [actFile '.cleanTrack.mat'];
eval(['save '  cleanName ' cleanTrack;']);    

pointsName = [actFile '.points.mat'];
eval(['save '  pointsName ' humanBody ptNumber mkCode;']);    

fprintf('Correcting done.\n');              