function [warpsX, warpsY] = getWarpFromStitch(img, blockx, blocky, b, deltaps)
% Created by Yuandong Tian, Oct. 5, 2009
% Email: yuandong@andrew.cmu.edu
% centerize the coefficients and reconstruct and stitch the warping

[nC, nFrame, nBlockx, nBlocky] = size(deltaps);

if nBlockx ~= length(blockx) - 1 || nBlocky ~= length(blocky) - 1
    fprintf(1, 'error!! dimension mismatch!\n');
    warpsX = [];
    warpsY = [];
    return;
end;

if size(img, 1) == 1 || size(img, 2) == 1
    m = img(1);
    n = img(2);
else
    [m, n] = size(img);
end;

warpsX = zeros(m, n, nFrame);
warpsY = zeros(m, n, nFrame);
for i = 1:nBlockx
    xsel_d = blockx(i):(blockx(i+1)-1);
    for j = 1:nBlocky
        ysel_d = blocky(j):(blocky(j+1)-1);

        fprintf(1, 'Deal with image block (%d/%d) * (%d/%d) \n', i, length(blockx), j, length(blocky));
        % centerize
        thisDeltaPs = deltaps(:, :, i, j);
        thisDeltaPs = thisDeltaPs - repmat(mean(thisDeltaPs, 2), [1, nFrame]);

        warpsX(ysel_d, xsel_d, :) = reshape(b.bx(:, 1:nC) * thisDeltaPs, [b.h, b.w, nFrame]);
        warpsY(ysel_d, xsel_d, :) = reshape(b.by(:, 1:nC) * thisDeltaPs, [b.h, b.w, nFrame]);
    end;
end;
