function smDatabase = learn_saliency(imageBase,s2Database)
% This function is to extract saliency maps of original images with the size of
% object level sparse feature maps. See Radhakrishna Achanta'CVPR09 paper.
% Input:  imageBase  -- the database of original images
%         s2Database -- the database of object level sparse feature maps
% Output: smDatabase -- the database of saliency maps
% Written by Jimei Yang
% Mar. 14, 2010

for i = 1:imageBase.imnum
    fprintf('extract the saliency map...');
    tic;
    fpath = imageBase.path{i};
    I = imread(fpath);
    if ndims(I) == 3
        smap = saliency(double(I));
    else
        smap = im2double(I);
    end
    % resize image
    s2path = s2Database.path{i};
    load(s2path);
    
    [im_h, im_w] = size(smap);
    if im_h > im_w
        smap = imresize(smap, [s2Fea.height,NaN], 'bicubic');
    else
        smap = imresize(smap, [NaN,s2Fea.width], 'bicubic');
    end
%         smap = imresize(smap, param.maxImgSize/max(im_h, im_w), 'bicubic');
    [im_h, im_w] = size(smap);
    m = min(min(smap));
    smap = smap - m;
    M = max(max(smap));
    smap = smap ./ M;
    
    if im_h < im_w
        app = round((im_w-im_h)/2);
        smap = [zeros(app,im_w);smap;zeros(im_w-im_h-app,im_w)];
    else
        app = round((im_h-im_w)/2);
        smap = [zeros(im_h,app),smap,zeros(im_h,im_h-im_w-app)];
    end
    [im_h, im_w] = size(smap);

    [pdir,fname] = fileparts(imageBase.path{i});
    seps = findstr(pdir,'\');
    fdir = ['data\sm_',pdir(seps(1)+1:end)];
    if ~isdir(fdir)
        mkdir(fdir);
    end
    fpath = fullfile(fdir,[fname,'.mat']);
    save(fpath,'smap');
%     figure; imshow(image,[]);
    smDatabase.path{i} = fpath;
    disp([num2str(i),' in ',num2str(toc),' seconds;']);
end