function [detectScore, numHit, numRepeat, numFalsePositive, runTime] = evaluate(imgDB_filename, imgD_filename, mask_filename)

%%% EE368 Project Spring 2004
%%% Evaluation Program
%%%
%%% INPUT:
%%%       1. imgDB_filename: filename of the template database image in BMP format
%%%       2. imgD_filename: filename of the image to be inspected in BMP format
%%%       3. mask_filename: filename of the mask image in BMP format

%%% 
%%% OUTPUT: 
%%%       1. detectScore = numHit - numRepeat - numFalsePositive (from ouput 3,4,5)
%%%       2. numHit = number of faces succesfully detected
%%%       3. numRepeat = number of faces repeatedly detected
%%%       4. numFalsePositive = number of cases where a non-face is reported
%%%       5. runTime = run time of your face detection routine
%%%
%%% EXMAPLE:
%%% 
%%%      [detectScore, numHit, numRepeat, numFalsePositive, runTime] = ...
%%%       evaluate('training_1_DB.bmp', 'training_1_D.bmp', 'training_1_mask.bmp')
%%%
%%%
%%% Your main routine has to be in this format: function defects = defect_detection(imgDB_filename, imgD_filename) 
%%%
%%% INPUT:  imgDB_filename and imgD_filename are filenames of the template database image and the image to be 
%%%         inspected in BMP format respectively.
%%% 
%%% OUTPUT: a N-by-2 matrix named 'defects' containg the coodinates of each detected defect
%%%         -- N is the number of defects you've detected
%%%         -- defects(:,1) contains the detected vertical coordinates (row index of the image matrix)
%%%         -- defects(:,2) contains the detected horizontal coordinates (column index of the image matrix)
%%%

startTime = clock;
defects = defect_detection(imgDB_filename, imgD_filename);
endTime = clock;
runTime = etime(endTime,startTime);

imgD = imread(imgD_filename);
imgD = double(imgD(:,:,1));
mask = imread(mask_filename);
mask = double(mask(:,:,1));

% Count how many defects are correctly detected
[numHit,numRepeat,numFalsePositive,imgLabeled] = countDefects(defects,imgD,mask);
detectScore = numHit - numRepeat - numFalsePositive;

outimg = 'result.jpg';
outtxt = 'result.txt';
outmat = 'result';

imwrite(uint8(imgLabeled),outimg);
fout = fopen(outtxt,'w');
fprintf(fout,' detectScore: %d\n numHit: %d\n numRepeat: %d\n numFalsePositive: %d\n runTime: %.2f\n',...
        detectScore,numHit,numRepeat,numFalsePositive,runTime);
fclose(fout);
save(outmat,'detectScore','numHit','numRepeat','numFalsePositive','runTime');




%%% Local Function
%%% Count how many defects are correctly detected
function [numHit,numRepeat,numFalsePositive,imgLabeled] = countDefects(defects,imgD,mask)

% define colors
red = [255 0 0];
yellow = [255 255 0];
cyan = [0 255 255];

% the output color image
imgD = uint8(imgD);
[imgh,imgw] = size(imgD);
imgLabeled = zeros(imgh,imgw,3);
imgLabeled(:,:,1) = imgD;
imgLabeled(:,:,2) = imgD;
imgLabeled(:,:,3) = imgD;

% number of detected defects
numFoundDefects = size(defects,1);
numHit = 0;
numRepeat = 0;
numFalsePositive = 0;

% label each defect in the mask with a number
% non-defect will be labeled as 0
maskLabeled = bwlabel(mask);
numDefects = max(maskLabeled(:));
hit = zeros(numDefects,1);

for i = 1:numFoundDefects

    found_y = defects(i,1);
    found_x = defects(i,2);
    currentLabel = maskLabeled(found_y,found_x);
    
    if (currentLabel~=0) % point on a defect
        
        if ~hit(currentLabel) % newly detected defect
            
            hit(currentLabel) = 1;
            color = cyan;
                        
            
        else % repeatedly detected defect
            
            numRepeat = numRepeat + 1;
            color = yellow;
            
        end        
        
    else % point not on a defect
        
        numFalsePositive = numFalsePositive + 1;
        color = red;
        
    end
        
    
    % label the point        
    x = found_x;
    y = found_y;
    xmin = max(1,x-1);
    xmax = min(imgw,x+1);
    ymin = max(1,y-12);
    ymax = min(imgh,y+12);
    xl = xmax - xmin + 1;
    yl = ymax - ymin + 1;
    imgLabeled(ymin:ymax,xmin:xmax,1) = color(1)*ones(yl,xl);
    imgLabeled(ymin:ymax,xmin:xmax,2) = color(2)*ones(yl,xl);
    imgLabeled(ymin:ymax,xmin:xmax,3) = color(3)*ones(yl,xl);
    xmin = max(1,x-12);
    xmax = min(imgw,x+12);    
    ymin = max(1,y-1);
    ymax = min(imgh,y+1);
    xl = xmax - xmin + 1;
    yl = ymax - ymin + 1;
    imgLabeled(ymin:ymax,xmin:xmax,1) = color(1)*ones(yl,xl);
    imgLabeled(ymin:ymax,xmin:xmax,2) = color(2)*ones(yl,xl);
    imgLabeled(ymin:ymax,xmin:xmax,3) = color(3)*ones(yl,xl);
        
end

numHit = sum(hit);

