Pengenalan Wajah Menggunakan Algoritma PCA


Salah satu algoritma yang dapat diimplementasikan dalam sistem pengenalan wajah (face recognition) adalah Principal Component Analysis (PCA). Berikut ini merupakan contoh pemrograman MATLAB mengenai pengenalan wajah menggunakan algoritma PCA. Source code yang digunakan merupakan modifikasi dari source code yang sebelumnya dikembangkan oleh Kalyan Sourav Dash. Modifikasi dilakukan untuk menghitung akurasi pelatihan dan pengujian serta memvisualisasikan citra wajah hasil pengenalan.

Pada pemrograman pengenalan wajah ini digunakan citra latih yang terdiri dari 10 individu (5 pria dan 5 wanita), pada masing-masing individu terdiri dari 15 citra wajah sehingga jumlah total data latih adalah sebanyak 150 citra wajah. Sedangkan pada citra uji, masing-masing individu terdiri dari 5 citra wajah sehingga jumlah total data uji adalah sebanyak 50 citra wajah. Berikut ini merupakan tampilan beberapa citra latih yang digunakan: (sumber dataset citra wajah: https://cswww.essex.ac.uk/mv/allfaces/faces94.html)

Langkah-langkah pemrogramannya adalah sebagai berikut:
1. Mengubah bentuk citra latih yang semula berupa matriks 2D menjadi vector 1D

clc; clear; close all;

data_train_path = 'data latih';

%%%%%%%%%  finding number of training images in the data path specified as argument  %%%%%%%%%%
filenames = dir(fullfile(data_train_path, '*.jpg'));
total_images = numel(filenames);

%%%%%%%%%%%%%%%%%%%%%%%%%%  creating the image matrix X  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
X = [];
for i = 1 : total_images
    str = fullfile(data_train_path, filenames(i).name);
    img = imread(str);
    img = rgb2gray(img);
    [r,c] = size(img);
    temp = reshape(img',r*c,1);  %% Reshaping 2D images into 1D image vectors
    %%% here img' is used because reshape(A,M,N) function reads the matrix A columnwise
    %%% where as an image matrix is constructed with first N pixels as first row,next N in second row so on
    X = [X temp];                %% X,the image matrix with columnsgetting added for each image
end

2.  Menghitung nilai m, A, dan eigenfaces

m adalah nilai mean dari citra latih,
A adalah matriks yang berisi vector hasil pengurangan antara vector citra latih terhadap m
eigenfaces adalah eigen value yang merepresentasikan fitur wajah

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   Now we calculate m, A and eigenfaces.The descriptions are below :
%
%          m           -    (MxN)x1  Mean of the training images
%          A           -    (MxN)xP  Matrix of image vectors after each vector getting subtracted from the mean vector m
%     eigenfaces       -    (MxN)xP' P' Eigenvectors of Covariance matrix (C) of training database X
%                                    where P' is the number of eigenvalues of C that best represent the feature set
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%% calculating mean image vector %%%%%

m = mean(X,2); % Computing the average face image m = (1/P)*sum(Xj's)    (j = 1 : P)
imgcount = size(X,2);

%%%%%%%%  calculating A matrix, i.e. after subtraction of all image vectors from the mean image vector %%%%%%

A = [];
for i=1 : imgcount
    temp = double(X(:,i)) - m;
    A = [A temp];
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% CALCULATION OF EIGENFACES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%  we know that for a MxN matrix, the maximum number of non-zero eigenvalues that its covariance matrix can have
%%%  is min[M-1,N-1]. As the number of dimensions (pixels) of each image vector is very high compared to number of
%%%  test images here, so number of non-zero eigenvalues of C will be maximum P-1 (P being the number of test images)
%%%  if we calculate eigenvalues & eigenvectors of C = A*A' , then it will be very time consuming as well as memory.
%%%  so we calculate eigenvalues & eigenvectors of L = A'*A , whose eigenvectors will be linearly related to eigenvectors of C.
%%%  these eigenvectors being calculated from non-zero eigenvalues of C, will represent the best feature sets.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

L = A' * A;
[V,D] = eig(L);  %% V : eigenvector matrix  D : eigenvalue matrix

%%%% again we use Kaiser's rule here to find how many Principal Components (eigenvectors) to be taken
%%%% if corresponding eigenvalue is greater than 1, then the eigenvector will be chosen for creating eigenface

L_eig_vec = [];
for i = 1 : size(V,2)
    if( D(i,i) > 1 )
        L_eig_vec = [L_eig_vec V(:,i)];
    end
end

%%% finally the eigenfaces %%%
eigenfaces = A * L_eig_vec;

3. Mencocokkan citra yang diujikan terhadap citra latih berdasarkan nilai eigenfaces. Pencocokkan citra didasarkan pada penghitungan jarak euclidean terdekat.

%In this part of recognition, we compare two faces by projecting the images into facespace and
% measuring the Euclidean distance between them.
%
%            recogimg           -   the recognized image name
%             testimg           -   the path of test image
%                m              -   mean image vector
%                A              -   mean subtracted image vector matrix
%           eigenfaces          -   eigenfaces that are calculated from eigenface function
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%% finding the projection of each image vector on the facespace (where the eigenfaces are the co-ordinates or dimensions) %%%%%

projectimg = [];  % projected image vector matrix
for i = 1 : size(eigenfaces,2)
    temp = eigenfaces' * A(:,i);
    projectimg = [projectimg temp];
end

4. Melakukan ekstraksi fitur terhadap citra latih dan mengujikannya

%%%%% extracting PCA features of the train image %%%%%
train_target = zeros(total_images,1);
num_img = 15;
num_face = 10;

for i = 1 : num_face
    train_target((i-1)*num_img+1:i*num_img) = i;
end

class_index = zeros(total_images,1);

for n = 1 : total_images
    str = fullfile(data_train_path, filenames(n).name);
    test_image = imread(str);
    test_image = rgb2gray(test_image);
    [r,c] = size(test_image);
    temp = reshape(test_image',r*c,1); % creating (MxN)x1 image vector from the 2D image
    temp = double(temp)-m; % mean subtracted vector
    projtestimg = eigenfaces'*temp; % projection of test image onto the facespace
    
    %%%%% calculating & comparing the euclidian distance of all projected trained images from the projected test image %%%%%
    
    euclide_dist = [ ];
    for i=1 : size(eigenfaces,2)
        temp = (norm(projtestimg-projectimg(:,i)))^2;
        euclide_dist = [euclide_dist temp];
    end
    
    [euclide_dist_min,recognized_index] = min(euclide_dist);
    class_index(n) = train_target(recognized_index);
end

[~,b] = find(class_index==train_target);
train_accuracy = sum(b)/total_images*100

akurasi yang diperoleh pada proses pelatihan adalah sebesar 100%

5. Melakukan ekstraksi fitur terhadap citra uji dan mengujikannya

%%%%% extracting PCA features of the test image %%%%%
data_test_path = 'data uji';

%%%%%%%%%  finding number of training images in the data path specified as argument  %%%%%%%%%%
filenames = dir(fullfile(data_test_path, '*.jpg'));
total_images = numel(filenames);
class_index = zeros(total_images,1);

for n = 1 : total_images
    str = fullfile(data_test_path, filenames(n).name);
    test_image = imread(str);
    test_image = rgb2gray(test_image);
    [r,c] = size(test_image);
    temp = reshape(test_image',r*c,1); % creating (MxN)x1 image vector from the 2D image
    temp = double(temp)-m; % mean subtracted vector
    projtestimg = eigenfaces'*temp; % projection of test image onto the facespace
    
    %%%%% calculating & comparing the euclidian distance of all projected trained images from the projected test image %%%%%
    
    euclide_dist = [ ];
    for i=1 : size(eigenfaces,2)
        temp = (norm(projtestimg-projectimg(:,i)))^2;
        euclide_dist = [euclide_dist temp];
    end
    
    [euclide_dist_min,recognized_index] = min(euclide_dist);
    class_index(n) = train_target(recognized_index);
end

test_target = zeros(total_images,1);
num_img = 5;
num_face = 10;

for i = 1 : num_face
    test_target((i-1)*num_img+1:i*num_img) = i;
end

[~,b] = find(class_index==test_target);
test_accuracy = sum(b)/total_images*100
save('pca','projectimg','eigenfaces','train_target','m')

akurasi yang diperoleh pada proses pengujian adalah juga sebesar 100%

6. Menampilkan contoh citra hasil pengenalan pada proses pengujian

clc; clear; close all;

load pca

datapath = 'data latih';
testimg = 'data uji\slbirc.17.jpg';

filenames = dir(fullfile(datapath, '*.jpg'));

%%%%% extracting PCA features of the test image %%%%%
test_image = imread(testimg);
test_image = rgb2gray(test_image);
[r,c] = size(test_image);
temp = reshape(test_image',r*c,1); % creating (MxN)x1 image vector from the 2D image
temp = double(temp)-m; % mean subtracted vector
projtestimg = eigenfaces'*temp; % projection of test image onto the facespace

%%%%% calculating & comparing the euclidian distance of all projected trained images from the projected test image %%%%%

euclide_dist = [ ];
for i=1 : size(eigenfaces,2)
    temp = (norm(projtestimg-projectimg(:,i)))^2;
    euclide_dist = [euclide_dist temp];
end

[euclide_dist_min,recognized_index] = min(euclide_dist);
recognized_img = fullfile(datapath, filenames(recognized_index).name);
test_image = imread(testimg);

figure,
subplot(1,2,1),imshow(test_image),title('Test Image');
subplot(1,2,2),imshow(recognized_img),title('Recognized Image');

Beberapa hasil pengenalan wajah yang dilakukan terhadap data uji adalah sebagai berikut

Source code dan citra pada pemrograman di atas dapat diperoleh pada halaman berikut ini: Source Code

Posted on August 14, 2018, in Pengenalan Pola, Pengolahan Citra and tagged , , , , , , , , , , , , , , , . Bookmark the permalink. 4 Comments.

  1. Assalamu’alaikum bg, terimakasih ya untuk Metode PCA dan LVQ nya..

  2. Assaamu’alaikum Warahmatullahi Wabarakatuh gan, mau tanya gan mengenai dataset nya, setelah di cek ukurannya 200x180x3 dan saya memakai dataset dengan ukuran 200×180, tanpa memakai x3. dan itu berpengarahuh pada saat program di run gan, ada solusi gan supaya image saya ukuranya bisa 200x180x3

  3. bg, menentukan nilai N itu gimana ? apa harus lebih kecil dari data latih ?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: