在Matlab上使用PCA进行人脸识别

时间:2015-11-01 14:51:49

标签: matlab computer-vision pca

我正在尝试在Matlab上使用PCA对一组图像进行分类。训练集包含约1800个380人的图像,每个图像具有该人的唯一标签(ID)。测试集包含相同380人(不同图像)的约750个图像,每个图像具有人的相应标签(ID)。所有图像均为160 x 128像素。

我的代码如下。

img_train{i}包含训练集中的第i个原始图像,img_test{i}包含测试集中的第i个原始图像。

euclide_dist = zeros(total_TrainImageFiles,1);
num_correct_labels = 0;

% Reshape 2D training images into 1D image vectors
train_img = zeros(irow*icol,total_TrainImageFiles);
for i = 1 : total_TrainImageFiles
    temp = reshape(img_train{i}',irow*icol,1);
    train_img(:,i) = temp;
end

% Calculate mean image vector
mean_face = mean(train_img,2);

% Subtract mean face from all training images
centred_data = train_img - repmat(mean_face, 1, total_TrainImageFiles);

% Determine eigenvectors and eigenvalues using SVD
[U, D, V] = svd(centred_data,0);

d = 10;
% Generate feature vectors from training set for subsequent classification
% Keep the top 'd' eigenvectors
eigenvectors = U(:,1:d);
% Project the training images into the facespace to generate the training feature vectors
train_features = eigenvectors' * centred_data;

% Classify testing images
for i = 1 : total_TestImageFiles
    % Reshape 2D test image into 1D image vectors
    test_img = reshape(img_test{i}',irow*icol,1);
    % Subtract mean face from test image
    centred_test_img = double(test_img) - mean_face;
    % Project test image onto the facespace
    test_features = eigenvectors' * centred_test_img;

    % Calculate the euclidian distance of all projected trained images from the projected test image
    for j = 1 : total_TrainImageFiles
        edist = (norm(train_features(:,j) - test_features))^2;
        euclide_dist(j) = edist;
    end

    % Find the minimum distance and compare class labels
    [euclide_dist_min,train_index] = min(euclide_dist);
    predicted_labels(i) = training_label(train_index);
    if training_label(train_index) == testing_label(i)
        num_correct_labels = num_correct_labels + 1;
    end 
end

% Calculate accuracy
accuracy = double(num_correct_labels) / double(total_TrainImageFiles);

我尝试过d = 10和d = 50,但我的准确度非常低,约为1.5%到2.5%。我的代码出了什么问题?

0 个答案:

没有答案