% generateDatabaseFeatures.m
% Extracts SURF features from all database images.

printTitle('Generating database features ...');

% Loop over all input images
overwrite = 0;
databaseFilenames = listDir(databaseDir, '*.jpg');
numImages = length(databaseFilenames);
databaseFrames = cell(1, numImages);
databaseDescriptors = cell(1, numImages);
for nImage = 1:numImages
    
    disp(sprintf('\nProcessing image %d ... \n', nImage));
    
    % Parse root name
    jpgName = databaseFilenames{nImage};
    indExt = strfind(jpgName, '.jpg');
    rootName = jpgName(1:indExt-1);
    
    % Make sure downsampled grayscale version exists
    pgmName = [rootName '.pgm'];
    if ~exist(pgmName, 'file') || overwrite
        imgColor = imread(jpgName);
        imgGray = rgb2gray(imgColor);
        imgGray = imresize(imgGray, [SURFInputHeight SURFInputWidth], 'bicubic');
        imwrite(imgGray, pgmName);
    end % if
    
    % Make sure SURF file exists
    if SURFEnhanced
        SURFExtension = '.surfe';
    else
        SURFExtension = '.surf';
    end
    SURFName = [rootName SURFExtension];
    if ~exist(SURFName, 'file') || overwrite
        createSURFFile(SURFEnhanced, SURFProg, pgmName, SURFName, SURFExtension);
    end % if
    
    % Load SURF data
    [databaseFrames{nImage}, databaseDescriptors{nImage}] = loadSURFFile(SURFName);
    
end % nImage

% Save MAT file
save(SURFDatabaseMAT, 'database*');
