# GenerateFigureCaption

## PURPOSE

GenerteFigureCaption - Generates html and latex documentation for the learning results.

## SYNOPSIS

function GenerateFigureCaption(folders)

## DESCRIPTION

     GenerteFigureCaption - Generates html and latex documentation for the learning results.

## CROSS-REFERENCE INFORMATION

This function calls:

This function is called by:

- StartFromHere StartFromHere - The entry point of this project.

## SOURCE CODE

    0001 function GenerateFigureCaption(folders)
    0002 % GenerteFigureCaption - Generates html and latex documentation for the learning results.
    0003 %
    0004
    0005 load template_params
    0006 nT = length(templateParameters); % number of categories, i.e. number of active basis templates
    0007
    0008 %% write the html code for reproducibility page
    0009
    0010 % delete the previous version
    0011 html_dir = 'document/html';
    0012 if ~exist(html_dir,'dir')
    0013     mkdir(html_dir);
    0014 else
    0015     rmdir(html_dir,'s');
    0016     mkdir(html_dir);
    0017 end
    0018 html_path = sprintf('%s/exp.html',html_dir);
    0019 html = fopen(html_path,'w');
    0020
    0021 html_img_dir = 'document/html/img/';
    0022 if ~exist(html_img_dir,'dir')
    0023     mkdir(html_img_dir);
    0024 end
    0025
    0026 tmp = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n';
    0027 tmp = [tmp '<html xmlns="http://www.w3.org/1999/xhtml">\n'];
    0028 tmp = [tmp '<head profile="http://gmpg.org/xfn/11">\n'];
    0029 tmp = [tmp '<title>Comparing object templates learned by active basis model and adaboost </title>\n'];
    0030 tmp = [tmp '</head>\n'];
    0031 fprintf(html,tmp);
    0032 fprintf(html, '<body>\n');
    0033 fprintf(html, '<h1> Comparing object templates learned by active basis model and adaboost </h1>');
    0034 fprintf(html, '<p> ');
    0035 fprintf(html, ['The figures below show learned image templates as active basis template or adaboost classifiers. ' ...
    0036     'In each figure, the top-left template illustrates the selected Gabor elements, where each selected Gabor ' ...
    0037     'element is represented by a bar of the same size. Following the template we' ...
    0038     ' show in pairs the original example image together the deformed template for that particular image. ' ...
    0039     'These example images and deformed templates are ranked by their likelihood scores in descending order. ' ...
    0040     'The weak classifiers of the adaboost template are created by thresholding Gabor responses. ' ...
    0041     '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.']);
    0042 fprintf(html, ' </p> <br/>');
    0043
    0044 for iFolder = 1:length(folders)
    0045     category = folders{iFolder};
    0046
    0047     % load the template parameters
    0048     for iT = 1:nT
    0049         if strcmp(templateParameters(iT).category,category) == 1
    0050             templateSize = templateParameters(iT).templateSize;
    0051             nsketch = templateParameters(iT).nElement_adaboost;
    0052             GaborScale = templateParameters(iT).GaborScale;
    0053             category_str = templateParameters(iT).category_str;
    0054             break;
    0055         end
    0056     end
    0057
    0058     % copy png figures to "<root>/document/html/img/"
    0059     new_img_name = sprintf('%s_activebasis.png',category);
    0060     src = sprintf('result/%s/activebasis_figure.png',category);
    0061     dst = sprintf('document/html/img/%s', new_img_name);
    0062     copyfile(src,dst);
    0063     new_img_name = sprintf('%s_adaboost.png',category);
    0064     src = sprintf('result/%s/adaboost_figure.png',category);
    0065     dst = sprintf('document/html/img/%s', new_img_name);
    0066     copyfile(src,dst);
    0067
    0068     % output the caption to html
    0069     fprintf(html, '<hr /> \n');
    0070     fprintf(html, sprintf('<h3>%s</h3>',category_str));
    0071     tmp = sprintf('<p>The following figure illustrates the <b>active basis template</b> learned from the %s category. ', category_str);
    0072     tmp = [tmp sprintf('The template is made up of %d Gabor elements of scale %.1f. ', nsketch, GaborScale)];
    0073     tmp = [tmp sprintf('The bounding window of the template is %d by %d pixels. ',templateSize(1),templateSize(2))];
    0074     tmp = [tmp '</p>\n'];
    0075     fprintf(html, tmp);
    0076
    0077     % output the figure to html: active basis
    0078     new_img_name = sprintf('%s_activebasis.png',category);
    0079     fprintf(html, sprintf('<br><img src="img/%s" alt="%s template for %s" width="1000" /> \n',new_img_name,'active basis',category_str));
    0080
    0081     % output the caption to html
    0082     tmp = sprintf('<p>The following figure illustrates the <b>adaboost strong classifier</b> learned from the %s category. ', category_str);
    0083     tmp = [tmp sprintf(['The strong classifier is made up of %d weak classifiers on locally maximized Gabor responses. '...
    0084         ' The scale of Gabor elements is also %.1f. '], nsketch, GaborScale)];
    0085     tmp = [tmp sprintf('The bounding window of the strong classifier is %d by %d pixels. ',templateSize(1),templateSize(2))];
    0086     tmp = [tmp '</p> \n'];
    0087     fprintf(html, tmp);
    0088
    0089     % output the figure to html: adaboost
    0090     new_img_name = sprintf('%s_adaboost.png',category);
    0091     fprintf(html, sprintf('<br><img src="img/%s" alt="%s template for %s" width="1000" /> \n',new_img_name,'adaboost',category_str));
    0092
    0093 end
    0094 fprintf(html, '</body> </html> \n');
    0095 fclose(html);
    0096
    0097
    0098 %% write the LaTex code for paper
    0099 tex_dir = 'document/latex';
    0100 if ~exist(tex_dir,'dir')
    0101     mkdir(tex_dir);
    0102 else
    0103     rmdir(tex_dir,'s');
    0104     mkdir(tex_dir);
    0105 end
    0106 tex_path = sprintf('%s/exp.tex',tex_dir);
    0107 tex = fopen(tex_path,'w');
    0108
    0109 tex_img_dir = 'document/latex/figure/';
    0110 if ~exist(tex_img_dir,'dir')
    0111     mkdir(tex_img_dir);
    0112 end
    0113
    0114 tmp = ['\\documentclass[11pt]{article}\n' ...
    0115     '\\usepackage{epsfig}\n'];
    0116 fprintf(tex,tmp);
    0117 fprintf(tex,'\\title{Report}\n');
    0118 fprintf(tex,'\\begin{document}\n');
    0119 fprintf(tex,'\\maketitle\n');
    0120
    0121 tmp = ['The figures below show learned image templates as active basis template or adaboost classifiers. ' ...
    0122     'In each figure, the top-left template illustrates the selected Gabor elements, where each selected Gabor ' ...
    0123     'element is represented by a bar of the same size. Following the template we' ...
    0124     ' show in pairs the original example image together the deformed template for that particular image. ' ...
    0125     'These example images and deformed templates are ranked by their likelihood scores in descending order. ' ...
    0126     'The weak classifiers of the adaboost template are created by thresholding Gabor responses. ' ...
    0127     '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'];
    0128 fprintf(tex,tmp);
    0129
    0130 % move eps figures to "<root>/document/latex/figure/"
    0131 for iFolder = 1:length(folders)
    0132     category = folders{iFolder};
    0133
    0134     % load the template parameters
    0135     for iT = 1:nT
    0136         if strcmp(templateParameters(iT).category,category) == 1
    0137             templateSize = templateParameters(iT).templateSize;
    0138             nsketch = templateParameters(iT).nElement_adaboost;
    0139             GaborScale = templateParameters(iT).GaborScale;
    0140             category_str = templateParameters(iT).category_str;
    0141             break;
    0142         end
    0143     end
    0144
    0145     % copy png figures to "<root>/document/tex/img/"
    0146     new_img_name = sprintf('%s_activebasis.eps',category);
    0147     src = sprintf('result/%s/activebasis_figure.eps',category);
    0148     dst = sprintf('document/latex/figure/%s', new_img_name);
    0149     copyfile(src,dst);
    0150     new_img_name = sprintf('%s_adaboost.eps',category);
    0151     src = sprintf('result/%s/adaboost_figure.eps',category);
    0152     dst = sprintf('document/latex/figure/%s', new_img_name);
    0153     copyfile(src,dst);
    0154
    0155     fprintf(tex, '\n \\begin{figure}[htp] \n');
    0156     % output the figure and caption to tex : active basis
    0157     new_img_name = sprintf('%s_activebasis.eps',category);
    0158     fprintf(tex, sprintf('\\\\psfig{figure=figure/%s, width=.8\\\\textwidth }\n', new_img_name));
    0159     tmp = sprintf('\\\\caption { This figure illustrates the active basis template learned from the %s category. ', category_str);
    0160     tmp = [tmp sprintf('The template is made up of %d Gabor elements of scale %.1f. ', nsketch, GaborScale)];
    0161     tmp = [tmp sprintf('The bounding window of the template is %d by %d pixels. }\n',templateSize(1),templateSize(2))];
    0162     fprintf(tex, tmp);
    0163     fprintf(tex, sprintf('\\\\label{fig:%s template for %s}\n', 'active basis', category_str));
    0164     fprintf(tex, '\\end{figure} \n');
    0165
    0166     % output the figure and caption to tex : adaboost
    0167     new_img_name = sprintf('%s_adaboost.eps',category);
    0168     fprintf(tex, '\n \\begin{figure}[htp] \n');
    0169     fprintf(tex, sprintf('\\\\psfig{figure=figure/%s, width=.8\\\\textwidth }\n', new_img_name));
    0170     tmp = sprintf('\\\\caption{ This figure illustrates the adaboost strong classifier learned from the %s category. ', category_str);
    0171     tmp = [tmp sprintf('The strong classifier is made up of %d Gabor elements of scale %.1f. ', nsketch, GaborScale)];
    0172     tmp = [tmp sprintf('The bounding window of the template is %d by %d pixels. }\n',templateSize(1),templateSize(2))];
    0173     fprintf(tex, tmp);
    0174     fprintf(tex, sprintf('\\\\label{fig:%s template for %s}\n', 'adaboost', category_str));
    0175     fprintf(tex, '\\end{figure} \n');
    0176     fprintf(tex, '\\clearpage\n');
    0177 end
    0178
    0179 fprintf(tex, '\\end{document} \n');
    0180 fclose(tex);
    0181
    0182 %% generate pdf (this is optional)
    0183 if ispc
    0184     cd document/latex
    0185     if dos('latex -interaction=nonstopmode -quiet exp.tex');
    0186         disp('no latex installed');
    0187     end
    0188     if dos('dvipdfm exp');
    0189         disp('no dvipdfm installed');
    0190     end
    0191     cd ../..
    0192 end