function [ Nrepeated, Nuseful ] = repeatibility(corners_dt0,corners_dt1,warp0_1,n ) 

% ECE 602: Project 
% Saad Waraich
% 06/04/2018
% This function evaluates the no. of useful and repeated corners for computing repeatibility

% corners_dt0 is the decision tree classified corner map of frame 0
% corners_dt1 is the decision tree classified corner map of frame 1
% warp0_1 is the warp map between the pair of frames


% Finding the location indices (row,col) of corners (non-zero) from frame 0
% corner map

[row_dt0,col_dt0] = find(corners_dt0);

% Finding the location indices (row,col) of corners (non-zero) from frame 1
% corner map

[row_dt1,col_dt1] = find(corners_dt1);


% Computing no. of useful corners by checking if the corners from frame 0,
% warp to an actual pixel in the warped frame_0_1

Nuseful = 0;
numtoincrease = 1;
new_row = [];
new_col = [];
for ii = 1:size(row_dt0,1)
    
    newpos = warpPosition([row_dt0(ii),col_dt0(ii)],warp0_1);  % Gives new position of corner from frame 0 after warping
    
    
    if newpos(1)~= -1 || newpos(2)~= -1  % newpos(1) is the row index while newpos(2) is the column index for warped pixel. When both are zero, pixel doesn't exist
        Nuseful=Nuseful+1;               % A warped corner is useful, if its row and column index are non-zero (means it warps to an actual pixel)
        new_row(numtoincrease,1) = newpos(1);
        new_col(numtoincrease,1) = newpos(2);
        numtoincrease = numtoincrease + 1;
    end
    
end

% Finding no. of 'Repeated' corners 

frame1 = [row_dt1,col_dt1];      % All corner positions in frame 1

warpednew = [new_row,new_col];   % All new warped corner positions

Nrepeated = 0;

for ii= 1:size(warpednew,1)
    
    warped_new = repmat(warpednew(ii,[1 2]),size(frame1,1),1);
    alpha = frame1 - double(warped_new);
    
    distances = sqrt(alpha(:,1).^2 + alpha(:,2).^2);  % Finding norm
    
    ncorners = sum(distances <= n);
    
    if ncorners >= 1
        Nrepeated = Nrepeated + 1;
    end
    
end




