%% ECE 602: Project 
% Hassan Nahas

% 23/03/2018

% Implementation Of FAST


%%

% Properties
threshold = 35;
n = 12;

% TODO: Provide Images
root = 'box/';
frame_0 = double(imread((strcat(root,'frames/frame_0.png'))));
frame_1 = double(imread((strcat(root,'frames/frame_1.png'))));
warp0_1 = imread(strcat(root,'pngwarps/warp_0_1.png'));

% figure(1)
% imshow(frame_0);
% 
% figure(2)
% imshow(frame_1);



allF = extractFeatureVector(frame_0,threshold);
allF = flatten(allF);

%Get labels from 'slow' segmentTest
labels = segmentCriterionFeatures(allF,12);

%Declare DecisionTree and Train it
Tree = cell(2,1);
[ bestx,resultingSubsets ] = findbestx( allF,labels );
Tree{1,1} = bestx;
Tree{2,1} = FindSubtree(resultingSubsets);

%Test Pixel-by-Pixel Decision Tree
tic;
corners_dt0 = classifyImageAccordingToTree( Tree,frame_0,threshold );
dt_time = toc;

tic;
corners_dt1 = classifyImageAccordingToTree( Tree,frame_1,threshold );
dt_time = toc;

% non maximum suppresion
% cornerStrengthMap_dt = cornerStrengthTree(frame_0,Tree);
% corner_nms_dt = nonmaximalsuppression(cornerStrengthMap_dt);
% corners_dt = corners_dt.*corner_nms_dt;


%Test Pixel-by-Pixel Segment Criterion Test
tic;
corners_slow = classifyImageAccordingToSegment( frame_0,threshold,n );
fullsegmentTime = toc;

%non maximum suppresion
% cornerStrengthMap_segment = cornerStrengthSegment(frame_0,n);
% corner_nms_segment = nonmaximalsuppression(cornerStrengthMap_segment);
% corners_slow = corners_slow.*corner_nms_dt;

sprintf('DecisionTree Classifier Time: %0.1f s',dt_time)
sprintf('SegmentTestCriterion Classifier Time: %0.1f s',fullsegmentTime)
imshowpair(corners_dt, corners_slow, 'montage')
% figure(1);
% imagesc(frame_0);
% 
% figure(2);
% imagesc(frame_1);
% 
% figure(3);
% [warped_f_0,coords] = warper(frame_0,warp0_1);
% imagesc(warped_f_0);
% 
% figure(4);
% imagesc(warp0_1);
% 
% newpos = warpPosition([191,400],warp0_1,size(frame_0(:,:,1)));