load deformation_field.mat;
load cloth_kyros.mat;

%%
[m, n, nFrame] = size(frames);
mxb = 50;
mxe = 3;
myb = 30;
mye = 10;
side = 10;
% random sample some points as the vertices of the mesh
[X, Y] = meshgrid(mxb+1:side:n-mxe, myb+1:side:m-mye);
tracker_template = [X(:), Y(:)];
nTracker = size(tracker_template, 1);

tracker_template = round(tracker_template + randn(nTracker, 2) * 1);

trackers = zeros(nTracker, 2, nFrame);
trackers(:, :, 1) = tracker_template;
for i = 2:nFrame
    indices = sub2ind([m, n], tracker_template(:, 2), tracker_template(:, 1));
    thisWx = wx(:, :, i); 
    trackers(:, 1, i) = tracker_template(:, 1) + thisWx(indices);
    thisWy = wy(:, :, i); 
    trackers(:, 2, i) = tracker_template(:, 2) + thisWy(indices);
end;

%% delancy triangularization
tris = delaunay(tracker_template(:, 1), tracker_template(:, 2), {'QJ'});
options.mesh = tris;
figure('Position', [0 0 1000 1000]);
makevideo_anno(frames, trackers, 'tracking.mpg', options);
