-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpca_code.m
More file actions
52 lines (46 loc) · 1.44 KB
/
pca_code.m
File metadata and controls
52 lines (46 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
%add the path name where images are stored
path='--';
training_set_length=4;
images=[];
mean_image=[];
for a = 1:training_set_length
curr_path=strcat(path,int2str(a),'.jpg');
image=imread(curr_path);
image=rgb2gray(image);
image=reshape(image,[],1);
images = [images,image];
if a==1
mean_image=image/training_set_length;
else
try_img=image/training_set_length;
mean_image=mean_image+try_img;
end
end
%mean_image=mean_image/training_set_length;
for a=1:training_set_length
images(:,a)=images(:,a)-mean_image;
end
images_transpose=transpose(images);
covariance=double(images_transpose)*double(images);
[v,d]=eig(covariance);
eigenfaces=[];
for a=1:training_set_length
eigenfaces=[eigenfaces,double(images)*double(v(:,a))];
end
sol=[];
for a=1:training_set_length
sol(:,a)=linsolve(double(eigenfaces),double(images(:,a)));
end
[filename,pathname]=uigetfile('*.jpg','Select the test image');
chosen_image= imread(fullfile(pathname,filename), 'jpg');
chosen_image=rgb2gray(chosen_image);
chosen_image=reshape(chosen_image,[],1);
chosen_image=chosen_image-mean_image;
chosen_image=double(chosen_image);
chosen_image_sol=linsolve(double(eigenfaces),chosen_image);
eucledian_distance=[];
for a=1:4
eucledian_distance(:,a)=sum(sqrt((chosen_image_sol - sol(:,a)) .^ 2));
end
[M,I] = min(eucledian_distance);
fprintf('Chosen image matches to image %d ',I);