function database = build_database(rt_img_dir,rt_gt_dir)
% This function is to build a database for the image sets 
% Input:  rt_img_dir -- direction of image sets
%         rt_gt_dir   -- direction of image sets
% Output: database    -- database that contains all the information of
%                        images

% Written by Jimei Yang
% Oct. 28, 2009

fprintf('dir the database...');
subfolders = dir(rt_img_dir);

database = [];

database.imnum = 0; % total image number of the database
database.cname = {}; % name of each class
database.label = []; % label of each class
database.imgpath = {}; % contain the pathes for each image of each class
database.gtpath = {};
database.nclass = 0;

for ii = 1:length(subfolders),
    subname = subfolders(ii).name;
    
    if ~strcmp(subname, '.') & ~strcmp(subname, '..'),
        
        if ~strcmp(subname, 'none') % object category
            database.nclass = database.nclass + 1;
            database.cname{database.nclass} = subname;
        
            iframes = dir(fullfile(rt_img_dir, subname, '*.bmp'));
            c_num = length(iframes);

            database.imnum = database.imnum + c_num;
            database.label = [database.label; ones(c_num, 1)*database.nclass];
        
            for jj = 1:c_num,
                c_path = fullfile(rt_img_dir, subname, iframes(jj).name);
                database.imgpath = [database.imgpath; c_path];
            end;    

            gt_subname = sprintf('%s_gt',subname);
            gtframes = dir(fullfile(rt_gt_dir, gt_subname, '*.jpg'));
            for jj = 1:c_num,
                c_path = fullfile(rt_gt_dir, gt_subname, gtframes(jj).name);
                database.gtpath = [database.gtpath; c_path];
            end;
            
        else % background
            iframes = dir(fullfile(rt_img_dir, subname, '*.bmp'));
            c_num = length(iframes);

            database.imnum = database.imnum + c_num;
            database.label = [database.label; zeros(c_num, 1)];
            
            for jj = 1:c_num,
                c_path = fullfile(rt_img_dir, subname, iframes(jj).name);
                database.imgpath = [database.imgpath; c_path];
            end;
            for jj = 1:c_num,
                database.gtpath = [database.gtpath; ' '];
            end; 
        end
    end;
end;
disp('done!');