% TRAINING DATASET
% The training images each contains
% a digit surrounded by other digits,
% which mimics how digits are normally
% seen together.
%============================
% Load training and test data
%===============================
load('digitDataSet.mat', 'trainingImages',...
    'trainingLabels','testImages');
%===============================
% Update file name relative to 
% matlabroot
%===============================
dataSetDir     = fullfile(matlabroot,'toolbox',...
    'vision','visiondemos');
trainingImages = fullfile(dataSetDir, trainingImages);
testImages     = fullfile(dataSetDir, testImages);
%=================================
% Show training and test samples
%================================
figure;
subplot(2,3,1); imshow(trainingImages{3,2});
subplot(2,3,2); imshow(trainingImages{23,4});
subplot(2,3,3); imshow(trainingImages{4,9});

subplot(2,3,4); imshow(testImages{2,2});
subplot(2,3,5); imshow(testImages{5,4});
subplot(2,3,6); imshow(testImages{8,9});
%
% Preprocessing
%
exTestImage  = imread(testImages{5,4});
exTrainImage = imread(trainingImages{23,4});
%
figure;
subplot(2,2,1); imshow(exTrainImage);
subplot(2,2,2); imshow(preProcess(exTrainImage));
subplot(2,2,3); imshow(exTestImage);
subplot(2,2,4); imshow(preProcess(exTestImage));
%=============================
% Extract HOG features
%=============================
img = imread(trainingImages{4,3});
%
% Extract HOG features and HOG visualization
%
[hog_2x2, vis2x2] = extractHOGFeatures(img,...
    'CellSize',[2 2]);
[hog_4x4, vis4x4] = extractHOGFeatures(img, ...
    'CellSize',[4 4]);
[hog_8x8, vis8x8] = extractHOGFeatures(img,'CellSize',[8 8]);
% Show the original image
figure;
subplot(2,3,1:3); imshow(img);
% Visualize the HOG features
subplot(2,3,4);
plot(vis2x2);
title({'CellSize = [2 2]'; ['Feature length = ' ...
    num2str(length(hog_2x2))]});
subplot(2,3,5);
plot(vis4x4);
title({'CellSize = [4 4]'; ['Feature length = ' ...
    num2str(length(hog_4x4))]});
subplot(2,3,6);
plot(vis8x8);
title({'CellSize = [8 8]'; ['Feature length = ' ...
    num2str(length(hog_8x8))]});
%===============================
% Compute the HOG feature size
%===============================
cellSize = [4 4];
hogFeatureSize = length(hog_4x4);
%=============================
% Train an SVM classifier for each 
% digit
%====================================
digits = char('0'):char('9');
%digits=[0:9];
for d = 1:numel(digits)
    % Pre-allocate trainingFeatures array
    numTrainingImages = size(trainingImages,1);
    trainingFeatures  = zeros(numTrainingImages,...
        hogFeatureSize,'single');
    % Extract HOG features from each training image. 
    % trainingImages contains both positive and 
    % negative image samples.
    for i = 1:numTrainingImages
        img = imread(trainingImages{i,d});
        img = preProcess(img);
        trainingFeatures(i,:) = extractHOGFeatures(...
            img,'CellSize',cellSize);
    end
    % Train a classifier for a digit. Each row of 
    % trainingFeatures contains the HOG features 
    % extracted for a single training image. The 
    % trainingLabels indicate if the features are 
    % extracted from positive(true) or negative 
    % (false) training images.
    svm(d) = svmtrain(trainingFeatures, ...
        trainingLabels(:,d));
end
%==================================
% Test the SVM classifier
%==================================
% Run each SVM classifier on the test images
for d = 1:numel(digits)
    % Pre-allocate testFeatures array
    numImages    = size(testImages,1);
    testFeatures = zeros(numImages, hogFeatureSize,...
        'single');
    % Extract features from each test image
    for i = 1:numImages
        img = imread(testImages{i,d});
        img = preProcess(img);
        testFeatures(i,:) = extractHOGFeatures(img,...
            'CellSize',cellSize);
    end
    % Run all the SVM classifiers
    for digit = 1:numel(svm)
        predictedLabels(:,digit,d) = svmclassify(...
            svm(digit),testFeatures);
    end
end
%====================================
% Tabulate the classification results 
% for each SVM classifier.
%=====================================
displayTable(predictedLabels,digits)