Sei sulla pagina 1di 3

% [ INITIALIZATION PROCESS...

]
clear all;
start_time = cputime;
no_of_objects = 40;
no_of_views = 10; % Select alternate views for training...36 views can also be
considered
step_size = 2;
obj_views = no_of_views/step_size;
total_views = no_of_objects * obj_views ;
row = 112;
col = 92;
avg_image = zeros(row, col);
sum_image = zeros(row, col);
directory = 'C:\Documents and Settings\mithun\Desktop\KPCA_PACE\ORL_DATASET\s';
no_of_eigen = 20;

fprintf(1, 'Computation of sum and average of all images...\n');

% [COMPUTATION OF SUM OF ALL IMAGE MATRICES...OBJECT EIGENSPACE CONSTRUCTION]


k=0;
for i=1:no_of_objects
for j=1:step_size:no_of_views
% Read each view and compute the sum and store in sum matrix...
obj_name='';
obj_index = i;
view_index = j;
obj_name = strcat(directory ,int2str(obj_index), '\', int2str(view_index),
'.pgm');
im_data = imread(obj_name);
k = k + 1;
x(:,k) = im_data(:);
image_data = double(im_data);

sum_image = sum_image + image_data;

end
end
avg_image = sum_image / total_views;

x = double(x)/255; %convert to double and normalize


avrgx=mean(x')';
imsize= size(im_data);
N = row*col;
M = total_views;
X = zeros(N, M);
col_index = 0;

%[COMPUTATION OF COVARIANCE MATRIX]


for i=1:no_of_objects
for j=1:step_size:no_of_views
row_index = 1;
col_index = col_index + 1;
% Read each view and compute X
obj_name='';
obj_index = i;
view_index = j;
obj_name = strcat(directory, int2str(obj_index), '/', int2str(view_index),
'.pgm');
im_data = imread(obj_name);
image_data = double(im_data);
diff_img = image_data - avg_image;
%Transorm diff_imag to vector form and store in X
X(:,col_index) = reshape(diff_img , row*col, 1);
end
fprintf('storing image %d\n',i);
obj_name = strcat(directory, int2str(i), '\', int2str(1), '.pgm');

end

fprintf(1, 'Computing the covariance matrix...\n');


Q = X'*X;

close all;
fprintf(1, 'Started eigenvalue computation...\n');
[v, d] = eig(Q);

fprintf(1, 'Computing the eigenvectors of original matrix...\n');


%[COMPUTING THE EIGENVECTORS OF ORIGINAL NxN MATRIX]
Qd = d;
col_index = total_views;
for i=1:no_of_eigen
fprintf(1, ' Computing the %d th eigenvector...\n', i);
dom_vector = v(:, col_index);
Qv(:, i) = ((1.0/sqrt(d(col_index, col_index))) * X) * dom_vector;
col_index = col_index - 1;
end

fprintf(1, 'Computation of feature matrix\n');


%[COMPUTATION OF FEATURE MATRIX USING DOMINENT EIGEN VECTOR]
F_M = Qv' * X;
end_time = cputime;
total_time = (end_time-start_time);
fprintf(1, 'Total computing time (in min.) %12.6f\n', total_time );
clear test_data;

%[ALTERNATE RECOGNITION VERIFICATION PROCESS... ONLY FOR 10 DIMENSION...]


%GET THE INPUT IMAGE AND PROJECT TO EIGEN SPACE...
prompt = {'Enter test person number (a number between 1 to 40):'};
dlg_title = 'Input of PCA-Based Face Recognition System';
num_lines= 1;
def = {'1'};
TestImage1 = inputdlg(prompt,dlg_title,num_lines,def);

prompt = {'Enter test image name (a number between 1 to 10):'};


dlg_title = 'Input of PCA-Based Face Recognition System';
num_lines= 1;
def = {'1'};
TestImage2 = inputdlg(prompt,dlg_title,num_lines,def);

TestImage = strcat(directory,char(TestImage1),'\',char(TestImage2),'.pgm');

image_data = double(imread(TestImage));
PR_data = image_data-avg_image;
test_data= reshape(PR_data, row*col,1);
t_data = Qv' * test_data;
clear trained_vector;
for i=1:total_views
trained_vector(:, 1) = F_M(:, i);
sim_value = 0;
for j=1:no_of_eigen
sim_value = sqrt(sim_value + ( (trained_vector(j, 1) - t_data(j, 1)) *
(trained_vector(j, 1) - t_data(j, 1)) ));
end
DIST(i) = sim_value;

end

[s, index] =sort(DIST);


image_index = TestImage;
recog=floor((index(1,1)/5)+1);
view= mod(index(1,1),5)+1;
SelectedImage = TestImage1;
obj_name = strcat(directory, char(SelectedImage), '\', int2str(view), '.pgm');
figure;
subplot(2,2,1); imshow(TestImage); title('Test Image');
subplot(2,2,2); imshow(obj_name); titletitle ('RECOGNIZED IMAGE');
subplot(2,2,3:4); plot(DIST,'.-'); title('Euclidean Distance between Test image and
Equvilant image');

Potrebbero piacerti anche