% restoreQuery.m
% Restores all query CD covers using different models.

clc;
printTitle('Restoring query images ...');

% Loop over all input images
if ~exist('queryFilenames', 'var')
    load(SURFQueryMAT);
end
if ~exist('databaseFilenames', 'var')
    load(SURFDatabaseMAT);
end
if exist('queryImagesGray', 'var')
    clear queryImagesGray;
end
numImages = length(queryFilenames);
allImages = 1:numImages;
lowFeatureImages = [4 5 6 10 11 19 21 24 31 32 33 39 40];
closeFeatureImages = [4 5 10 11 29 34 36 37 39 43 44];
lowIntensityImages = [9 16 17 19 22 34 46 48];
if ~exist('databaseDescriptorsCompact', 'var')
    databaseDescriptorsCompact = cell(1, numImages);
    for nImage = allImages
        databaseDescriptorsCompact{nImage} = databaseDescriptors{queryToDatabaseNumbers(nImage)};
    end % nImage
    clear databaseDescriptors;
end
for nImage = 19:50
    
    disp(sprintf('\nRestoring image %d ... \n', nImage));
    
    plotRows = 3;
    plotCols = 2;
    
    % Show original query image
    imgColor = imread(queryFilenames{nImage});
    figure(1); clf;
    subplot(plotRows,plotCols,1);
    warning off;
    imshow(imgColor); title(sprintf('Original Query %d', nImage));
    warning on;
    pause(2);
    
    % Show database image
    nImageDB = queryToDatabaseNumbers(nImage);
    imgColorDB = imread(databaseFilenames{nImageDB});
    subplot(plotRows,plotCols,2);
    warning off;
    imshow(imgColorDB); title('Database');
    warning on;
    pause(2);
    
    % Generate restored query image
    frame = queryFrames{nImage};
    desc = queryDescriptors{nImage};
    frameDB = databaseFrames{nImageDB};
    descDB = databaseDescriptorsCompact{nImage};
    [imgColorRestore1stRGB, imgColorRestore2ndRGB, imgColorRestore1stLAB, imgColorRestore1stRGBMult] = ...
        restoreCD(imgColor, frame, desc, imgColorDB, frameDB, descDB);
    
    % Show restored query images
    subplot(plotRows,plotCols,3);
    warning off;
    imshow(imgColorRestore1stRGB); title('Restored Query - 1st Order RGB');
    warning on;
    pause(2);
    subplot(plotRows,plotCols,4);
    warning off;
    imshow(imgColorRestore1stLAB); title('Restored Query - 1st Order LAB');
    warning on;
    pause(2);
    subplot(plotRows,plotCols,5);
    warning off;
    imshow(imgColorRestore2ndRGB); title('Restored Query - 2nd Order RGB');
    warning on;
    pause(2);
    subplot(plotRows,plotCols,6);
    warning off;
    imshow(imgColorRestore1stRGBMult); title('Restored Query - 1st Order RGB Multiplicative');
    warning on;
    pause(2);
    
    % Save restored images to file
    if SURFEnhanced
        enhancedTag = 'enhanced';
    else
        enhancedTag = 'basic';
    end
    restoreName1stRGB = sprintf('%s%.2d-%s-1stRGB.jpg', restoreDir, nImage, enhancedTag);
    restoreName1stLAB = sprintf('%s%.2d-%s-1stLAB.jpg', restoreDir, nImage, enhancedTag);
    if ~isempty(imgColorRestore1stRGB)
        imwrite(imgColorRestore1stRGB, restoreName1stRGB);
        imwrite(imgColorRestore1stLAB, restoreName1stLAB);
    end
    
    % Clean up    
    pause(10);
    clear imgColor*;
    clear frame*;
    clear desc*;
    
end % nImage