Class Distance:
Compute matrix of pairwise distances between photographs.
Contents
Given a set of jpg-images in the directory PictureDir, load them, vectorize them and compute their pairwise distance.
% Close all previous figures close all % Edit PictureDir so that it contains the full location of the pictures PictureDir = '~/Google Drive/Teaching/'; %PictureDir = '~/Teaching/MA211/TestPics/'; % Search for all (JPEG) files ending with a .jpg Pics = dir([PictureDir,'*.jpg']); % Resize all pictures to a common size % Remove the division by 10 to make larger r = 600/10; % Number of rows in new resized picture c = 450/10; % Number of cols in new resized picture
Loop through each picture, load it into memory and view it
for n=1:length(Pics) % Load and resize the image one by one Im = imresize(imread([PictureDir,Pics(n).name]),[r,c]); % Vectorize the image PicVex(:,n) = double(reshape(Im,r*c*3,1)); % Display the vector as an image imshow(reshape(uint8(PicVex(:,n)),r,c,3)) % Label the image xlabel(['Person ',num2str(n),],'FontSize',15); pause end
![](ClassDistance_01.png)
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)); % Uncomment the two lines below (remove %) to plot %subplot(s,s,k) %imshow(reshape(uint8(PicVex(:,p)+PicVex(:,q)),r,c,3)) k = k+1; end end