function GenerateFigureCaption(folders)
% GenerteFigureCaption - Generates html and latex documentation for the learning results.
%

load template_params
nT = length(templateParameters); % number of categories, i.e. number of active basis templates 

%% write the html code for reproducibility page

% delete the previous version
html_dir = 'document/html';
if ~exist(html_dir,'dir')
    mkdir(html_dir);
else
    rmdir(html_dir,'s');
    mkdir(html_dir);
end
html_path = sprintf('%s/exp.html',html_dir);
html = fopen(html_path,'w');

html_img_dir = 'document/html/img/';
if ~exist(html_img_dir,'dir')
    mkdir(html_img_dir);
end

tmp = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n';
tmp = [tmp '<html xmlns="http://www.w3.org/1999/xhtml">\n'];
tmp = [tmp '<head profile="http://gmpg.org/xfn/11">\n'];
tmp = [tmp '<title>Comparing object templates learned by active basis model and adaboost </title>\n'];
tmp = [tmp '</head>\n'];
fprintf(html,tmp);
fprintf(html, '<body>\n');
fprintf(html, '<h1> Comparing object templates learned by active basis model and adaboost </h1>');
fprintf(html, '<p> ');
fprintf(html, ['The figures below show learned image templates as active basis template or adaboost classifiers (with 400 negative examples). ' ...
    'In each figure, the top-left template illustrates the selected Gabor elements, where each selected Gabor ' ...
    'element is represented by a bar of the same size. Following the template we' ...
    ' show in pairs the original example image together the deformed template for that particular image. ' ...
    'These example images and deformed templates are ranked by their likelihood scores in descending order. ' ...
    'The weak classifiers of the adaboost template are created by thresholding Gabor responses. ' ...
    'A red bar means the weak classifier is true when the response is larger than a threshold. Otherwise it is denoted by a blue bar.']);
fprintf(html, ' </p> <br/>');

for iFolder = 1:length(folders)
    category = folders{iFolder};
    
    % load the template parameters
    for iT = 1:nT
        if strcmp(templateParameters(iT).category,category) == 1
            templateSize = templateParameters(iT).templateSize;
            nsketch = templateParameters(iT).nElement_adaboost;
            GaborScale = templateParameters(iT).GaborScale;
            category_str = templateParameters(iT).category_str;
            break;
        end
    end
    
    % copy png figures to "<root>/document/html/img/"
    new_img_name = sprintf('%s_activebasis.png',category);
    src = sprintf('result/%s/activebasis_figure.png',category);
    dst = sprintf('document/html/img/%s', new_img_name);
    copyfile(src,dst);
    new_img_name = sprintf('%s_adaboost.png',category);
    src = sprintf('result/%s/adaboost_figure.png',category);
    dst = sprintf('document/html/img/%s', new_img_name);
    copyfile(src,dst);
    
    new_img_name = sprintf('%s_activebasis_template.png',category);
    src = sprintf('result/%s/activebasis_template.png',category);
    dst = sprintf('document/html/img/%s', new_img_name);
    copyfile(src,dst);
    new_img_name = sprintf('%s_adaboost_template.png',category);
    src = sprintf('result/%s/adaboost_template.png',category);
    dst = sprintf('document/html/img/%s', new_img_name);
    copyfile(src,dst);
    
    % output the caption to html
    fprintf(html, '<hr /> \n');
    fprintf(html, sprintf('<h3>%s</h3>',category_str));
    tmp = sprintf('<p>The following figure illustrates the <b>active basis template</b> learned from the %s category. ', category_str);
    tmp = [tmp sprintf('The template is made up of %d Gabor elements of scale %.1f. ', nsketch, GaborScale)];
    tmp = [tmp sprintf('The bounding window of the template is %d by %d pixels. ',templateSize(1),templateSize(2))];
    tmp = [tmp '</p>\n'];
    fprintf(html, tmp);
    
    % output the figure to html: active basis
    new_img_name = sprintf('%s_activebasis.png',category);
    fprintf(html, sprintf('<br><img src="img/%s" alt="%s template for %s" width="1000" /> \n',new_img_name,'active basis',category_str));
    
    % output the caption to html
    tmp = sprintf('<p>The following figure illustrates the <b>adaboost strong classifier</b> learned from the %s category. ', category_str);
    tmp = [tmp sprintf(['The strong classifier is made up of %d weak classifiers on locally maximized Gabor responses. '...
        ' The scale of Gabor elements is also %.1f. '], nsketch, GaborScale)];
    tmp = [tmp sprintf('The bounding window of the strong classifier is %d by %d pixels. ',templateSize(1),templateSize(2))];
    tmp = [tmp '</p> \n'];
    fprintf(html, tmp);
    
    % output the figure to html: adaboost
    new_img_name = sprintf('%s_adaboost.png',category);
    fprintf(html, sprintf('<br><img src="img/%s" alt="%s template for %s" width="1000" /> \n',new_img_name,'adaboost',category_str));
    
    % output the figure to html: active basis
    new_img_name = sprintf('%s_activebasis_template.png',category);
    fprintf(html, sprintf('<br><br><img src="img/%s" alt="%s template for %s" width="100" /> \n',new_img_name,'active basis',category_str));
    new_img_name = sprintf('%s_adaboost_template.png',category);
    fprintf(html, sprintf('<img src="img/%s" alt="%s template for %s" width="100" /> \n',new_img_name,'adaboost',category_str));
    
end
fprintf(html, '</body> </html> \n');
fclose(html);


%% write the LaTex code for paper 
tex_dir = 'document/latex';
if ~exist(tex_dir,'dir')
    mkdir(tex_dir);
else
    rmdir(tex_dir,'s');
    mkdir(tex_dir);
end
tex_path = sprintf('%s/exp.tex',tex_dir);
tex = fopen(tex_path,'w');

tex_img_dir = 'document/latex/figure/';
if ~exist(tex_img_dir,'dir')
    mkdir(tex_img_dir);
end

tmp = ['\\documentclass[11pt]{article}\n' ...
    '\\usepackage{epsfig}\n'];
fprintf(tex,tmp);
fprintf(tex,'\\title{Report}\n');
fprintf(tex,'\\begin{document}\n');
fprintf(tex,'\\maketitle\n');

tmp = ['The figures below show learned image templates as active basis template or adaboost classifiers. ' ...
    'In each figure, the top-left template illustrates the selected Gabor elements, where each selected Gabor ' ...
    'element is represented by a bar of the same size. Following the template we' ...
    ' show in pairs the original example image together the deformed template for that particular image. ' ...
    'These example images and deformed templates are ranked by their likelihood scores in descending order. ' ...
    'The weak classifiers of the adaboost template are created by thresholding Gabor responses. ' ...
    'A red bar means the weak classifier is true when the response is larger than a threshold. Otherwise it is denoted by a blue bar. \n'];
fprintf(tex,tmp);

% move eps figures to "<root>/document/latex/figure/"
for iFolder = 1:length(folders)
    category = folders{iFolder};
    
    % load the template parameters
    for iT = 1:nT
        if strcmp(templateParameters(iT).category,category) == 1
            templateSize = templateParameters(iT).templateSize;
            nsketch = templateParameters(iT).nElement_adaboost;
            GaborScale = templateParameters(iT).GaborScale;
            category_str = templateParameters(iT).category_str;
            break;
        end
    end
    
    % copy png figures to "<root>/document/tex/img/"
    new_img_name = sprintf('%s_activebasis.eps',category);
    src = sprintf('result/%s/activebasis_figure.eps',category);
    dst = sprintf('document/latex/figure/%s', new_img_name);
    copyfile(src,dst);
    new_img_name = sprintf('%s_adaboost.eps',category);
    src = sprintf('result/%s/adaboost_figure.eps',category);
    dst = sprintf('document/latex/figure/%s', new_img_name);
    copyfile(src,dst);
    
    fprintf(tex, '\n \\begin{figure}[htp] \n');
    % output the figure and caption to tex : active basis
    new_img_name = sprintf('%s_activebasis.eps',category);
    fprintf(tex, sprintf('\\\\psfig{figure=figure/%s, width=.8\\\\textwidth }\n', new_img_name));
    tmp = sprintf('\\\\caption { This figure illustrates the active basis template learned from the %s category. ', category_str);
    tmp = [tmp sprintf('The template is made up of %d Gabor elements of scale %.1f. ', nsketch, GaborScale)];
    tmp = [tmp sprintf('The bounding window of the template is %d by %d pixels. }\n',templateSize(1),templateSize(2))];
    fprintf(tex, tmp);
    fprintf(tex, sprintf('\\\\label{fig:%s template for %s}\n', 'active basis', category_str));
    fprintf(tex, '\\end{figure} \n');
    
    % output the figure and caption to tex : adaboost
    new_img_name = sprintf('%s_adaboost.eps',category);
    fprintf(tex, '\n \\begin{figure}[htp] \n');
    fprintf(tex, sprintf('\\\\psfig{figure=figure/%s, width=.8\\\\textwidth }\n', new_img_name));
    tmp = sprintf('\\\\caption{ This figure illustrates the adaboost strong classifier learned from the %s category. ', category_str);
    tmp = [tmp sprintf('The strong classifier is made up of %d Gabor elements of scale %.1f. ', nsketch, GaborScale)];
    tmp = [tmp sprintf('The bounding window of the template is %d by %d pixels. }\n',templateSize(1),templateSize(2))];
    fprintf(tex, tmp);
    fprintf(tex, sprintf('\\\\label{fig:%s template for %s}\n', 'adaboost', category_str));
    fprintf(tex, '\\end{figure} \n');
    fprintf(tex, '\\clearpage\n');
end

fprintf(tex, '\\end{document} \n');
fclose(tex);

%% generate pdf (this is optional)
if ispc
    cd document/latex
    if dos('latex -interaction=nonstopmode -quiet exp.tex');
        disp('no latex installed');
    end
    if dos('dvipdfm exp');
        disp('no dvipdfm installed');
    end
    cd ../..
end
