Lesson 3/11 and moved eigenfaces folder from 29/9 lesson folder
This commit is contained in:
38
11-3/eigenfaces/eigenfaces_classify.m
Executable file
38
11-3/eigenfaces/eigenfaces_classify.m
Executable file
@ -0,0 +1,38 @@
|
||||
function [matched_individual,bestmatchdistance]=eigenfaces_classify(test,training,n);
|
||||
%classifies using n principal components, closest match
|
||||
[w, h, nExpressions, nIndividuals]=size(training);
|
||||
X=reshape(training,[w*h,nIndividuals*nExpressions]);
|
||||
avg=mean(X,2);
|
||||
Xd=bsxfun(@minus,X,avg);
|
||||
[U,S,V]=svd(Xd,0);
|
||||
Xt=reshape(test,w*h,numel(test)/(w*h));
|
||||
Xtd=bsxfun(@minus,Xt,avg);
|
||||
scores=U(:,1:n)'*Xtd;
|
||||
trainingscores=U(:,1:n)'*Xd;
|
||||
%normalize scores and samples
|
||||
%scores=bsxfun(@rdivide,scores,sqrt(sum(abs(scores).^2)));
|
||||
%trainingscores=bsxfun(@rdivide,trainingscores,sqrt(sum(abs(trainingscores).^2)));
|
||||
%cosine similarity
|
||||
%C=scores'*trainingscores;
|
||||
%[bestmatchdistance bestmatchindex]=max(C,[],2);
|
||||
%matched_individual=ceil(bestmatchindex/nExpressions);
|
||||
|
||||
%Euclidean distance
|
||||
distanceMatrix=nan(size(scores,2),size(trainingscores,2));
|
||||
for i=1:size(scores,2)
|
||||
for j=1:size(trainingscores,2)
|
||||
distanceMatrix(i,j)=norm(scores(:,i)-trainingscores(:,j));
|
||||
end
|
||||
end
|
||||
[bestmatchdistance bestmatchindex]=min(distanceMatrix,[],2);
|
||||
matched_individual=ceil(bestmatchindex/nExpressions);
|
||||
if numel(test)==w*h
|
||||
subplot(1,2,1);
|
||||
imagesc(test);
|
||||
colormap(gray);
|
||||
subplot(1,2,2);
|
||||
imagesc(reshape(X(:,bestmatchindex),[w,h]));
|
||||
colormap(gray);
|
||||
disp('best match distance=');
|
||||
disp(bestmatchdistance);
|
||||
end
|
||||
18
11-3/eigenfaces/eigenfaces_scatter.m
Executable file
18
11-3/eigenfaces/eigenfaces_scatter.m
Executable file
@ -0,0 +1,18 @@
|
||||
function eigenfaces_scatter(images, indices);
|
||||
|
||||
[w, h, nExpressions, nIndividuals]=size(images);
|
||||
X=reshape(images,[w*h,nIndividuals*nExpressions]);
|
||||
avg=mean(X,2);
|
||||
Xd=bsxfun(@minus,X,avg);
|
||||
[U,S,V]=svd(Xd,0);
|
||||
scores=U(:,indices)'*Xd;
|
||||
%normalize scores and samples
|
||||
%scores=bsxfun(@rdivide,scores,sqrt(sum(abs(scores).^2)));
|
||||
|
||||
if length(indices) == 3
|
||||
scatter3(scores(1,:),scores(2,:),scores(3,:),50*ones(size(scores(1,:))),kron(1:nIndividuals,ones(1,nExpressions)));
|
||||
elseif length(indices) == 2
|
||||
scatter(scores(1,:),scores(2,:),50*ones(size(scores(1,:))),kron(1:nIndividuals,ones(1,nExpressions)));
|
||||
else
|
||||
error('wrong indices size');
|
||||
end
|
||||
29
11-3/eigenfaces/interactiverec.m
Executable file
29
11-3/eigenfaces/interactiverec.m
Executable file
@ -0,0 +1,29 @@
|
||||
function interactiverec(F)
|
||||
% given a 243x320 image F, displays it as sum of components
|
||||
stdsize=[243,320];
|
||||
|
||||
F = F(:);
|
||||
|
||||
if not(numel(F) == prod(stdsize))
|
||||
error('The first argument must be the picture to reconstruct');
|
||||
end
|
||||
|
||||
X=readyalefaces_to_tensor;
|
||||
X=reshape(X,[prod(stdsize),numel(X)/prod(stdsize)]);
|
||||
avg=mean(X,2);
|
||||
Xs=bsxfun(@minus,X,avg);
|
||||
[U,S,V]=svd(X,0);
|
||||
colormap(gray);
|
||||
ncolors=size(gray,1);
|
||||
h = image(reshape(avg,stdsize)*ncolors);
|
||||
|
||||
% Add a slider
|
||||
uicontrol('Style', 'slider', 'Min', 0, 'Max', size(U,2), ...
|
||||
'Callback', @callback,'Position',[10 0 300 20]);
|
||||
|
||||
function callback(src,evt)
|
||||
d=round(get(src, 'Value'))
|
||||
set(h, 'CData', reshape(ncolors*(avg+U(:,1:d)*(U(:,1:d)'*F)),stdsize));
|
||||
end
|
||||
|
||||
end
|
||||
34
11-3/eigenfaces/readyalefaces_to_tensor.m
Executable file
34
11-3/eigenfaces/readyalefaces_to_tensor.m
Executable file
@ -0,0 +1,34 @@
|
||||
function [F,descr] = readyalefaces_to_tensor(str)
|
||||
|
||||
if not(exist('str','var'))
|
||||
str='all';
|
||||
end
|
||||
switch str
|
||||
case 'easy'
|
||||
extensions = {'happy', 'normal', 'sad', 'sleepy', 'surprised', 'wink' };
|
||||
case 'easy-nowink'
|
||||
extensions = {'happy', 'normal', 'sad', 'sleepy', 'surprised' };
|
||||
case 'nowink'
|
||||
extensions = {'centerlight', 'glasses', 'happy', 'leftlight', 'noglasses', 'normal', 'rightlight', 'sad', 'sleepy', 'surprised' };
|
||||
case {'hard','all'}
|
||||
extensions = {'centerlight', 'glasses', 'happy', 'leftlight', 'noglasses', 'normal', 'rightlight', 'sad', 'sleepy', 'surprised', 'wink' };
|
||||
otherwise
|
||||
error 'unknown selector';
|
||||
end
|
||||
|
||||
for i = 1 : 15,
|
||||
basename = 'yalefaces/subject';
|
||||
if( i < 10 )
|
||||
basename = [basename, '0', num2str(i)];
|
||||
else
|
||||
basename = [basename, num2str(i)];
|
||||
end;
|
||||
|
||||
for j = 1:length(extensions),
|
||||
fullname = [basename, '.', extensions{j}, '.gif'];
|
||||
X = imread(fullname);
|
||||
F(:,:,j,i) = double(X)/255;
|
||||
end;
|
||||
|
||||
end;
|
||||
descr=extensions;
|
||||
13
11-3/eigenfaces/showyalefaces.m
Executable file
13
11-3/eigenfaces/showyalefaces.m
Executable file
@ -0,0 +1,13 @@
|
||||
X=readyalefaces_to_tensor;
|
||||
%X = X(:,:,1:4,[12,9,6,1]); %filters out only some faces
|
||||
[w h expr ind]=size(X);
|
||||
clf;
|
||||
for i=1:expr
|
||||
for j=1:ind
|
||||
subplot('position', [(i-1)/expr, (j-1)/ind,1/expr,1/ind]);
|
||||
% subplot(expr,ind,j+(i-1)*ind);
|
||||
imagesc(X(:,:,i,j));
|
||||
colormap(gray);
|
||||
end
|
||||
end
|
||||
set(findobj(gcf, 'type','axes'), 'Visible','off')
|
||||
7
11-3/eigenfaces/tightsubplot.m
Executable file
7
11-3/eigenfaces/tightsubplot.m
Executable file
@ -0,0 +1,7 @@
|
||||
function tightsubplot(dim, i, data)
|
||||
|
||||
row = mod(i-1, dim);
|
||||
col = floor((i-1) / dim);
|
||||
subplot('position', [row*(1/dim), (dim-col-1)*(1/dim), 1/dim-.001, 1/dim-0.001 ]);
|
||||
imagesc(data);
|
||||
axis off;
|
||||
Reference in New Issue
Block a user