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

2 ビュー (過去 30 日間)
Eline Meuleman
Eline Meuleman 2022 年 2 月 22 日
回答済み: Shubham 2024 年 12 月 3 日
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!!

回答 (1 件)

Shubham
Shubham 2024 年 12 月 3 日
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.

カテゴリ

Help Center および File ExchangeGraph and Network Algorithms についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by