Display Fisherface after reducing dimensions using PCA
3 views (last 30 days)
Show older comments
Hi,
I'm learning to implement the Fisherface algorithm using Matlab. I understand that to deal with the singularity issue of Sw, I can project Sb and Sw onto the PCA space of r dimensions, with r ≤ rank(Sw) and compute the Fisherfaces in this PCA space. If I set r = 1000, what are my Fisherfaces' dimensions, and how do I display them as images? Do I need to reconstruct them back to their original dimensions? If so, how do I do this?
Thank you.
0 Comments
Answers (1)
Aditya
on 31 Jan 2025 at 5:47
Hi Rayne,
The Fisherface algorithm involves two main steps: applying Principal Component Analysis (PCA) to reduce dimensionality and then performing Linear Discriminant Analysis (LDA) on the reduced data to find the Fisherfaces. Here's a step-by-step guide to implementing this in MATLAB, along with how to handle the dimensions and display the Fisherfaces.
% Assume X is your data matrix with each column as a flattened image
% labels is a vector of class labels
% Step 1: Compute PCA
[coeff, score, ~, ~, explained] = pca(X');
% Choose r dimensions based on cumulative variance explained or a fixed number
r = 1000; % or choose based on explained variance
pcaData = score(:, 1:r)'; % Transpose to have each column as a data point
% Step 2: Compute LDA
% Compute the within-class and between-class scatter matrices in the PCA space
uniqueClasses = unique(labels);
numClasses = numel(uniqueClasses);
meanTotal = mean(pcaData, 2);
Sw = zeros(r, r);
Sb = zeros(r, r);
for i = 1:numClasses
classData = pcaData(:, labels == uniqueClasses(i));
meanClass = mean(classData, 2);
Sw = Sw + cov(classData');
Sb = Sb + size(classData, 2) * (meanClass - meanTotal) * (meanClass - meanTotal)';
end
% Solve the generalized eigenvalue problem for LDA
[eigVecs, eigVals] = eig(Sb, Sw);
% Sort eigenvectors by eigenvalues in descending order
[~, idx] = sort(diag(eigVals), 'descend');
fisherfaces = eigVecs(:, idx);
% Step 3: Display Fisherfaces
% Fisherfaces are in the PCA space, reconstruct them to original dimensions
fisherfacesOriginal = coeff(:, 1:r) * fisherfaces;
% Choose how many Fisherfaces to display
numFisherfacesToDisplay = 5;
figure;
for i = 1:numFisherfacesToDisplay
% Reshape each Fisherface back to image dimensions
fisherfaceImage = reshape(fisherfacesOriginal(:, i), [imageHeight, imageWidth]);
subplot(1, numFisherfacesToDisplay, i);
imshow(mat2gray(fisherfaceImage)); % Normalize and display
title(['Fisherface ', num2str(i)]);
end
This implementation assumes your images are vectorized and that you know their original dimensions (imageHeight and imageWidth). Adjust the number of Fisherfaces to display based on your needs.
0 Comments
See Also
Categories
Find more on Dimensionality Reduction and Feature Extraction 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!