%% Collect face shots of class members into a zip file living in a
%common/public directory

%% Download & unzip the zip file to your computer, in a place MATLAB can


%% Load each photo and compress it.
% Close all previous figures
        close all

% Edit PictureDir so that it contains the full location of the pictures
        %PictureDir = '~/Google Drive/Fractals/';
        PictureDir = '/Users/macproyo/Google Drive/Teaching/MA211/pics';

% Search for all (JPEG) files ending with a .jpg
        Pics = dir([PictureDir,'*.jpg']);

% Resize all pictures to a common size
        r = 60; % Number of rows in new resized picture
        c = 45; % Number of cols in new resized picture


% Loop through each picture, load it into memory and view it
        for n=1:length(Pics)
                        Im = imresize(imread([PictureDir,Pics(n).name]),[r,c]);
                        PicVex(:,n) = double(reshape(Im,r*c*3,1));
            %imshow(reshape(uint8(PicVex(:,n)),r,c,3))
            xlabel(['Person ',num2str(n),],'FontSize',15);
                        %pause
        end

% Create a distance matrix of norms between each pair of pictures
% and plot the difference between two pictures
        figure; hold on;
        k = 1;
        s = length(Pics);
        for p=1:s
                        for q=1:s
                                        DistanceMatrix(p,q) = norm(PicVex(:,p)-PicVex(:,q));
                                        %subplot(s,s,k)
                                        %imshow(reshape(uint8(PicVex(:,p)-PicVex(:,q)),r,c,3))
                                        %k = k+1;
                        end
    end
    %% Finding Neighbors
for row = 1:p
                        % Make a vector out of each row vector of the distance matrix
                        V = DistanceMatrix(row,:);
                        % Find the minimum non-zero value
                        MinDist = min(V(V>0));
                        % Find where this value occurred
                        NearestNeighbor(2,row) = find(V==MinDist);

                        % Display who is closest to whom
                        disp(['Person ',num2str(row),'is closest to Person',num2str(NearestNeighbor(2,row))]);
                        % Plot all the pictures in a grid for evaluation of the algorithm
                        subplot(5,ceil(p/5),row)
                        hold on;
                        title(['Person ',num2str(row)]);
                        imshow(reshape(uint8(PicVex(:,row)),r,c,3))
        end
    %% The pairwise angle between each person in our class (MA211)
AngleMatrix(p,q) = acos((dot(PicVex(p,:),PicVex(:,q)))/(norm(PicVex(p,:)*PicVex(:,q))));

%% Who's "way out there"?
%A = PicVex;
%(maxnum,id) = max(diag(A));
%(minnum,minid) = min(diag(A));