how to calculate eigen vector, value using in pca??

조회 수: 6 (최근 30일)
pooja dixit
pooja dixit 2013년 6월 25일
답변: Aashray 2025년 2월 14일
hello everyone.... my topic is face recognition... but problem is that how to calculate pca and how to find eigen value & matrix.. if anyone have an idea and have a matlab code for related this tropic please help me...

답변 (1개)

Aashray
Aashray 2025년 2월 14일
Hello Pooja,
for performing PCA it is recommended to convert the images into greyscale to reduce the dimensionality.
Next, the covariance matrix can be found by directly using the MATLAB in-built function cov. This can then be used to find the eigen values and hence the principal components from the images.
You may refer to the following MATLAB code for more understanding:
% Load face images into a matrix (here X) where each column is a flattened image vector
imageDir = './cats';
imageFiles = dir(fullfile(imageDir, '*.jpg'));
numImages = length(imageFiles);
sampleImage = imread(fullfile(imageDir, imageFiles(1).name));
[imageHeight, imageWidth] = size(rgb2gray(sampleImage)); % Convert to grayscale
% I have used 100 images of size 64x64 for illustration, so X is 4096x100.
X = zeros(imageHeight * imageWidth, numImages);
for i = 1:numImages
img = imread(fullfile(imageDir, imageFiles(i).name));
if size(img, 3) == 3
img = rgb2gray(img);
end
X(:, i) = img(:);
end
% Centering the data by subtracting the mean face
mean_face = mean(X, 2);
X_centered = X - mean_face;
% Calculating the covariance matrix, and the eigenvectors and eigenvalues matrix
cov_matrix = cov(X_centered');
[eigenvectors, eigenvalues_matrix] = eig(cov_matrix);
% Extracting eigenvalues from the diagonal of the eigenvalues matrix
eigenvalues = diag(eigenvalues_matrix);
% Sorting eigenvalues in descending order, and the corresponding eigen vectors
[sorted_eigenvalues, index] = sort(eigenvalues, 'descend');
sorted_eigenvectors = eigenvectors(:, index);
k = 10; % Selecting the top k eigenvectors (as they represent the principal components)
principal_components = sorted_eigenvectors(:, 1:k);
The following links might be helpful for better understanding of “cov”,eig”, and “diag” functions:
You can also refer to the attached output for better understanding, the principal components variable is highlighted:

카테고리

Help CenterFile Exchange에서 Dimensionality Reduction and Feature Extraction에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by