function makevideo_sampletracking(frames, wx, wy, filename, options)
options = argutil_setdefaults(options, 'marginx', [10, 10], 'marginy', [10, 10], ...
                              'mesh_bboxes', [], 'side', 10);

[m, n, nFrame] = size(frames);

% random sample some points as the vertices of the mesh
if isempty(options.mesh_bboxes)
    [X, Y] = meshgrid(options.marginx(1)+1:options.side:n-options.marginx(2), ...
                      options.marginy(1)+1:options.side:m-options.marginy(2));
    options.mesh = delaunay(X(:), Y(:), {'QJ'});
else
    X = [];
    Y = [];
    mesh = [];
    for i = 1:size(options.mesh_bboxes, 1)
        bb = options.mesh_bboxes(i, :);
        [Xi, Yi] = meshgrid(bb(1):options.side:bb(3), bb(2):options.side:bb(4));
        mesh = [mesh; delaunay(Xi(:), Yi(:), {'QJ'}) + length(X)];
        X = [X; Xi(:)];
        Y = [Y; Yi(:)];       
    end;
    options.mesh = mesh;
end;

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;

mDeform = size(wx, 1);
nDeform = size(wx, 2);

x = tracker_template(:, 1);
y = tracker_template(:, 2);

indices = sub2ind([mDeform, nDeform], round(y / m * mDeform), round(x / n * nDeform));

for i = 2:nFrame
    thisWx = wx(:, :, i); 
    trackers(:, 1, i) = x + thisWx(indices) / nDeform * n;
    thisWy = wy(:, :, i); 
    trackers(:, 2, i) = y + thisWy(indices) / mDeform * m;
end;

%figure;
makevideo_anno(frames, trackers, filename, options);
