
clear
clc
close all

load('Directories_Mobo');


%% Initialize Face Detector:
% Initialize two detectors, one for front face, one for profile:
VJFrontFaceDetector = vision.CascadeObjectDetector('UseROI',true);
VJProfileFaceDetector = vision.CascadeObjectDetector('ProfileFace','UseROI',true);
% Regions of interest in the 4 camera angles (to reduce computation time):
ROI_Matrix=[73 40 320 200; ...
    100 68 245 209; ...
    126, 62, 256, 220; ...
    93, 11 272 223];


%% Loop Over All Individuals
for i=1:size(IndividualName,1)
    fprintf('Individual %i\n', i)
    
    
    %% Loop Over All Actions For Certain Individual:
    for j= 1:size(ActionName,1)
        fprintf('     Action %i\n', j)
        % Feature Length: 1600 x 1 elements
        % Number of Features: 340(Images)*4(Camera Angles)*2(Face Detectors)= 2720 possible features
        gray_features=zeros(1600,2720);
        LBP_features=zeros(256,2720);
        
        % Features are taken for a whole action subset over all 4 (viable)
        % camera angles:
        feature_idx=1;
        
        %% Loop Over 4 Camera Angles for Certain Action:
        for jj= 1:size(CameraAngle,1)
            fprintf('           Camera Angle %i\n', jj)
            
            Image_Names=dir(strcat('./moboJpg/',IndividualName{i},'/',ActionName{j},'/',CameraAngle{jj},'/*.jpg'));
            
            %% Loop Over All Images For Certain Camera Angle:
            
            for k= 1:size(Image_Names,1)
                
                I=imread(strcat('./moboJpg/',IndividualName{i},'/',ActionName{j},'/',CameraAngle{jj},'/',Image_Names(k).name));
                
                %% 1 - Viola-Jones Face Detection:
                bboxesFront = step(VJFrontFaceDetector, I, ROI_Matrix(jj,:));
                bboxesProfile = step(VJProfileFaceDetector, I, ROI_Matrix(jj,:));
                
                %DetectedFrontFaces = insertObjectAnnotation(I, 'rectangle', bboxesFront, ['Front Face: ', num2str(feature_idx)] );
                %DetectedProfileFaces = insertObjectAnnotation(I, 'rectangle', bboxesProfile, ['Profile Face: ', num2str(feature_idx)] );
                %imshow([DetectedFrontFaces DetectedProfileFaces],'InitialMagnification',100)
                
                
                if(size(bboxesFront,1)>0)
                    
                    %% 2 - Convert Front Faces to Standard Data:
                    % # Converting to grayscale
                    % # Resizing image to 40x40
                    % # Histogram Equalization
                    for l= 1:size(bboxesFront,1);
                        face=histeq(imresize(rgb2gray(I(bboxesFront(l,2):bboxesFront(l,2)+bboxesFront(l,4),bboxesFront(l,1):bboxesFront(l,1)+bboxesFront(l,3),:)), [40 40]));
                        
                        %% 3 - Populate Feature Matrix:
                        % The feature point is simply the resulting gray values. Alternatively LBP
                        % features can be used.
                        gray_image_feature=face(:);
                        LBP_image_feature=lbp(face)';
                        gray_features(:,feature_idx)=gray_image_feature;
                        LBP_features(:,feature_idx)=LBP_image_feature;
                        feature_idx=feature_idx+1;
                    end
                    
                end
                
                if(size(bboxesProfile,1)>0)
                    
                    %% 2 - Convert Profile Faces to Standard Data:
                    % # Converting to grayscale
                    % # Resizing image to 40x40
                    % # Histogram Equalization
                    for l= 1:size(bboxesProfile,1);
                        face=histeq(imresize(rgb2gray(I(bboxesProfile(l,2):bboxesProfile(l,2)+bboxesProfile(l,4),bboxesProfile(l,1):bboxesProfile(l,1)+bboxesProfile(l,3),:)), [40 40]));
                        
                        %% 3 - Populate Feature Matrix:
                        % The feature point is simply the resulting gray values. Alternatively LBP
                        % features can be used.
                        gray_image_feature=face(:);
                        LBP_image_feature=lbp(face)';
                        gray_features(:,feature_idx)=gray_image_feature;
                        LBP_features(:,feature_idx)=LBP_image_feature;
                        feature_idx=feature_idx+1;
                    end
                    
                end
                
                
            end
            
        end
        gray_features(:,feature_idx:end)=[];
        LBP_features(:,feature_idx:end)=[];
        save(strcat('./moboJpg/',IndividualName{i},'/GREYfeatures/',IndividualName{i},'_',int2str(j)),'gray_features')
        save(strcat('./moboJpg/',IndividualName{i},'/LBPfeatures/',IndividualName{i},'_',int2str(j)),'LBP_features')
    end
    fprintf('\n')
end