%Sarah Meshreqy
%Training training set and concatination with all patterns and  tests the functionality of fast andd compares the pixel by pixel decision tree and the pixel by pixel segment criterion 

% Properties
threshold = 0.05;
n = 12;

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

allPatterns=GeneratePatterns( 1, 16);

allF0 = extractFeatureVector(frame_0,threshold);
allF0 = flatten(allF0);
allF1 = extractFeatureVector(frame_1,threshold);
allF1 = flatten(allF1);
allF2 = extractFeatureVector(frame_2,threshold);
allF2 = flatten(allF2);
allF_Train=horzcat(allF0,allF1,allF2);
% Training images concatenated with all paterns
allF=horzcat(allF_Train,allPatterns);



%Provide test images
root = 'testdata/'
I1=readpng('frame_12.png');
I2=double(readppm('img16.ppm'));
I3=double(readpgm('boat_img6.pgm'));
testIm=double(readppm('img17.ppm'));
%allF = extractFeatureVector(frame_0,threshold);
%allF = flatten(allF);

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


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

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


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

sprintf('Labeling Time: %0.1f s',label_time)
sprintf('training Time: %0.1f s',train_time)
sprintf('DecisionTree Classifier Time: %0.1f s',dt_time)
sprintf('SegmentTestCriterion Classifier Time: %0.1f s',fullsegmentTime)
imshowpair(corners_dt, corners_slow, 'montage')
