How to get four faces with the highest eigenvalue​s/eigenvec​tors?

9 views (last 30 days)
I'm trying to understand eigenfaces. I've a dataset with X faces as rows, and X datapoints as columns.
I've already turned this datafile into a covariance matrix, and now I want to make a graph with the two faces with the highest eigenvalues and the two faces with the lowest eigenvalues.
How to do this? I now have:
  • covmat = cov(faces)
  • [V,D] = eig(covmat)
  • high = eigs(D,2)
  • low = eigs(D,2,'smallestabs')
I now have the highest and lowest numbers in the D-matrix (highest and lowest eigenvalues). But how do I transpose this back to faces? How to know which eigenvalue corresponds to which face? And how to display them in a figure?
Thanks for thinking along!!

Answers (1)

Shubham
Shubham on 3 Dec 2024 at 11:21
Hi Eline,
To display the faces corresponding to the highest and lowest eigenvalues in your dataset, follow these steps:
1. Compute the covariance matrix:
covmat = cov(faces);
2. Use the eig function to calculate Eigenvectors and Eigenvalues:
[V, D] = eig(covmat);
3. Extract the Eignevalues from diagonal of D and sort them to find the indices of the highest and lowest values.
eigenvalues = diag(D);
[sortedEigenvalues, indices] = sort(eigenvalues, 'descend');
4. Use the indices to select the corresponding Eigenvectors for the highest and lowest Eigenvalues:
topEigenvectors = V(:, indices(1:2)); % Two largest eigenvectors
bottomEigenvectors = V(:, indices(end-1:end)); % Two smallest eigenvectors
5. Assuming each face is a square image, reshape the Eigenvectors to the original image dimensions and display them:
% Assuming each face is a square image of size sqrt(N) x sqrt(N)
faceSize = sqrt(size(faces, 2));
figure;
% Reshape and display the top eigenfaces
subplot(2, 2, 1);
imshow(reshape(topEigenvectors(:, 1), [faceSize, faceSize]), []);
title('Top Eigenface 1');
subplot(2, 2, 2);
imshow(reshape(topEigenvectors(:, 2), [faceSize, faceSize]), []);
title('Top Eigenface 2');
% Reshape and display the bottom eigenfaces
subplot(2, 2, 3);
imshow(reshape(bottomEigenvectors(:, 1), [faceSize, faceSize]), []);
title('Bottom Eigenface 1');
subplot(2, 2, 4);
imshow(reshape(bottomEigenvectors(:, 2), [faceSize, faceSize]), []);
title('Bottom Eigenface 2');
For more information, refer to the following documentation links:
Hope this helps.

Categories

Find more on Graph and Network Algorithms in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!