31 lines
No EOL
939 B
Matlab
31 lines
No EOL
939 B
Matlab
% implements the Back Projection algorithm, returns nothing but displays
|
|
% the first 5 eigenfaces of the training data after being 'back-projected'
|
|
% using the first 10, 50, and 100 eigenvectors of the data.
|
|
function [] = Back_Project(training_data, test_data, n_components)
|
|
|
|
% stack data
|
|
data = vertcat(training_data, test_data);
|
|
|
|
% perform PCA
|
|
[coeff, score] = pca(data);
|
|
|
|
% for each number of principal components
|
|
for n_idx = 1:length(n_components)
|
|
n = n_components(n_idx);
|
|
|
|
% perform the back projection algorithm using the first n_components(n) principal components
|
|
W = coeff(:,1:n);
|
|
z = score(:,1:n);
|
|
sample_mean = mean(data);
|
|
reconstruction = W * z' + sample_mean';
|
|
|
|
% plot first 5 images back projected using the first
|
|
% n_components(n) principal components
|
|
for i = 1:5
|
|
subplot(3,2,i)
|
|
imagesc(reshape(reconstruction(:,i),32,30)');
|
|
end
|
|
|
|
end
|
|
|
|
end % Function end |