% Created by Yuandong Tian, Oct. 5, 2009
% Email: yuandong@andrew.cmu.edu
% generate Figure 11 and videos in the paper

load warp.mat;

framess = {};
reconImg1s = {};
reconImg2s = {};
reconAno1s = {};
reconAno2s = {};
z1s = {};
z2s = {};
%imgBoard = im2double(rgb2gray(imread('tracking/checkboard.png')));
%imgBoard = im2double(imread('tracking/vertical.png'));

names = {};
validRange1s = {};
validRange2s = {};

% Sorry but I don't have the video for Large letters and checkboard since I forgot to take groundtruth image in the experiment...
%% Middle letters
imgBoard = im2double(imread('firetextMid.png'));
name = 'middle';
load expdata_middle.mat;
load exp_computed_middle.mat;
putDistortion;

%% Small letters
imgBoard = im2double(imread('firetext.png'));
name = 'small';
load expdata_small.mat;
load exp_computed_small.mat;
putDistortion;

%% Tiny letters
name = 'tiny';
load expdata_tiny.mat;
load exp_computed_tiny.mat;
putDistortion;

%% Brick
imgBoard = im2double(imread('BrickWall.jpg'));
name = 'brick';
load expdata_brick.mat;
load exp_computed_brick.mat;
putDistortion;

%% then make videos
nSeries = length(framess);
nChannel = size(imgBoard, 3);

% for first partition
for i = 1:nSeries
    xsel = validRange1s{i}(1):validRange1s{i}(3);
    ysel = validRange1s{i}(2):validRange1s{i}(4);
    
    filename = [names{i} 'syn1.avi'];
    filename_surf = [names{i} 'syn1_z.avi'];
   
    if nChannel > 1
        clear colorframe colorRecon;

        colorframe = permute(repmat(framess{i}(ysel, xsel, 1:61), [1, 1, 1, nChannel]), [1, 2, 4, 3]);
        colorRecon = permute(repmat(reconImg1s{i}(ysel, xsel, :), [1, 1, 1, nChannel]), [1, 2, 4, 3]);
        
        makevideo3_notitle(colorframe, colorRecon, reconAno1s{i}(ysel, xsel, :, :), filename);
    else
        makevideo3_notitle(framess{i}(ysel, xsel, 1:61), reconImg1s{i}(ysel, xsel, :), reconAno1s{i}(ysel, xsel, :), filename);
    end;
    
    % then make the water surface
    makevideo_z(z1s{i}, filename_surf);
end;

% for second partition
for i = 1:nSeries
    xsel = validRange2s{i}(1):validRange2s{i}(3);
    ysel = validRange2s{i}(2):validRange2s{i}(4);
    
    filename = [names{i} 'syn2.avi'];
    filename_surf = [names{i} 'syn2_z.avi'];
    
    if nChannel > 1
        clear colorframe colorRecon;
        
        colorframe = permute(repmat(framess{i}(ysel, xsel, 1:61), [1, 1, 1, nChannel]), [1, 2, 4, 3]);
        colorRecon = permute(repmat(reconImg2s{i}(ysel, xsel, :), [1, 1, 1, nChannel]), [1, 2, 4, 3]);
        
        makevideo3_notitle(colorframe, colorRecon, reconAno2s{i}(ysel, xsel, :, :), filename);
    else
        makevideo3_notitle(framess{i}(ysel, xsel, 1:61), reconImg2s{i}(ysel, xsel, :), reconAno2s{i}(ysel, xsel, :), filename);
    end;
    % then make the water surface
    makevideo_z(z2s{i}, filename_surf);
end;

