Sie sind auf Seite 1von 4

Face recognition by appearance

Appearance-based methods rely heavily on the concept of an image space. A twodimensional


image I (x,y ) or any type of visual data (depth maps, flow fields, etc.)
may be viewed as a vector (or point) in a very high dimensional space, often called
the image space, where each coordinate of the space corresponds to one pixel
value of the original image. In general, a grayscale image with r rows and c columns
describes a vector x in a m -dimensional image space, where m = r x c . With this
image representation, the image becomes a very high dimensional feature vector,
and perhaps the simplest classification approach is a nearest neighbor classifier in
the image space
Face recognition using pca and matlab

%data matrix
%each column is a face
X=[x1 x2 x3 x4 x5 x6 x7 x8];
% PCA
[V,D,Average] = pc_evectors(X,Nv);
%projecting the data
P=V'*Xm;
Pca_test.m
%pca_test1.m, example using data in [smith 2002] LI Smith,%matlab by khwong
%"A tutorial on Principal Components Analysis,
%www.cs.otago.ac.nz/cosc453/student_tutorials/principal_components.pdf
%---------Step1--get some data-----------------function test
x=[2.5 0.5 2.2 1.9 3.1 2.3 2 1 1.5 1.1]' %column vector
y=[2.4 0.7 2.9 2.2 3.0 2.7 1.6 1.1 1.6 0.9]' %column vector
N=length(x)
%---------Step2--subtract the mean-----------------mean_x=mean(x), mean_y=mean(y)
x_adj=x-mean_x,y_adj=y-mean_y %data adjust for x,y
%---------Step3---cal. covariance matrix---------------data_adj=[x_adj,y_adj]
cov_x=cov(data_adj)
%---------Step4---cal. eignvector and eignecalues of cov_x------------[eigvect,eigval]=eig(cov_x)
eigval_1=eigval(1,1), eigval_2=eigval(2,2)
eigvect_1=eigvect(:,1),eigvect_2=eigvect(:,2),
%eigvector1_length is 1, so the eigen vector is a unit vector
eigvector1_length=sqrt(eigvect_1(1)^2+eigvect_1(2)^2)
eigvector2_length=sqrt(eigvect_2(1)^2+eigvect_2(2)^2)
%sorted, big eigen_vect(big eignval first)
%P_full=[eigvect(1,2),eigvect(2,2);eigvect(1,1),eigvect(2,1)]
P_full=[eigvect_2';eigvect_1'] %1st eigen vector is small,2nd is large
P_approx=[eigvect_2';[0,0]]%keep (2nd) big eig vec only,small gone
figure(1)

clf
hold on
plot(-1,-1) %create the same diagram as in fig.3.1 of[smith 2002].
plot(4,4), plot([-1,4],[0,0],'-'),plot([0,0],[-1,4],'-')
hold on
title('PCA demo')
%step5: select feature
%eigen vectors,length of the eigen vector proportional to its eigen val
plot([0,eigvect(1,1)*eigval_1],[0,eigvect(2,1)*eigval_1],'b-')%1stVec
plot([0,eigvect(1,2)*eigval_2],[0,eigvect(2,2)*eigval_2],'r-')%2ndVec
title('eign vector 2(red) is much longer (bigger eigen value), so keep it')
plot(x,y,'bo') %original data
%%full
%%%%%%%%%%%%%%%%%%%%%%%%
%recovered_data_full=P_full*data_adj+repmat([mean_x;mean_y],1,N)
final_data_full=P_full*data_adj'
recovered_data_full=P_full'*final_data_full+repmat([mean_x;mean_y],1,N)
%recovered_data_full=P_full*data_adj'+repmat([mean_x;mean_y],1,N)
plot(recovered_data_full(1,:),recovered_data_full(2,:),'r+')
%%approx %%%%%%%%%%%%%%%%
%recovered_data_full=P_full*data_adj+repmat([mean_x;mean_y],1,N)
final_data_approx=P_full*data_adj'
recovered_data_approx=P_approx'*final_data_approx+repmat([mean_x;mean_y],1,N)
%recovered_data_full=P_full*data_adj'+repmat([mean_x;mean_y],1,N)
plot(recovered_data_approx(1,:),recovered_data_approx(2,:),'gs')

%pca_test1.m, example using data in [smith 2002] LI Smith,%matlab by khwong


%"A tutorial on Principal Components Analysis,
%www.cs.otago.ac.nz/cosc453/student_tutorials/principal_components.pdf
%---------Step1--get some data-----------------function test
x=[2.5 0.5 2.2 1.9 3.1 2.3 2 1 1.5 1.1]' %column vector
y=[2.4 0.7 2.9 2.2 3.0 2.7 1.6 1.1 1.6 0.9]' %column vector
N=length(x)
%---------Step2--subtract the mean-----------------mean_x=mean(x), mean_y=mean(y)
x_adj=x-mean_x,y_adj=y-mean_y %data adjust for x,y
%---------Step3---cal. covariance matrix---------------data_adj=[x_adj,y_adj]
cov_x=cov(data_adj)
%---------Step4---cal. eignvector and eignecalues of cov_x------------[eigvect,eigval]=eig(cov_x)
eigval_1=eigval(1,1), eigval_2=eigval(2,2)
eigvect_1=eigvect(:,1),eigvect_2=eigvect(:,2),
%eigvector1_length is 1, so the eigen vector is a unit vector

eigvector1_length=sqrt(eigvect_1(1)^2+eigvect_1(2)^2)
eigvector2_length=sqrt(eigvect_2(1)^2+eigvect_2(2)^2)
%sorted, big eigen_vect(big eignval first)
%P_full=[eigvect(1,2),eigvect(2,2);eigvect(1,1),eigvect(2,1)]
P_full=[eigvect_2';eigvect_1'] %1st eigen vector is small,2nd is large
P_approx=[eigvect_2';[0,0]]%keep (2nd) big eig vec only,small gone
figure(1), clf, hold on
plot(-1,-1) %create the same diagram as in fig.3.1 of[smith 2002].
plot(4,4), plot([-1,4],[0,0],'-'),plot([0,0],[-1,4],'-')
hold on
title('PCA demo')
%step5: select feature
%eigen vectors,length of the eigen vector proportional to its eigen val
plot([0,eigvect(1,1)*eigval_1],[0,eigvect(2,1)*eigval_1],'b-')%1stVec
plot([0,eigvect(1,2)*eigval_2],[0,eigvect(2,2)*eigval_2],'r-')%2ndVec
title('eign vector 2(red) is much longer (bigger eigen value), so keep it')
plot(x,y,'bo') %original data
%%full
%%%%%%%%%%%%%%%%%%%%%%%%
%recovered_data_full=P_full*data_adj+repmat([mean_x;mean_y],1,N)
final_data_full=P_full*data_adj'
recovered_data_full=P_full'*final_data_full+repmat([mean_x;mean_y],1,N)
%recovered_data_full=P_full*data_adj'+repmat([mean_x;mean_y],1,N)
plot(recovered_data_full(1,:),recovered_data_full(2,:),'r+')
%%approx %%%%%%%%%%%%%%%%
%recovered_data_full=P_full*data_adj+repmat([mean_x;mean_y],1,N)
final_data_approx=P_full*data_adj'
recovered_data_approx=P_approx'*final_data_approx+repmat([mean_x;mean_y],1,N)
%recovered_data_full=P_full*data_adj'+repmat([mean_x;mean_y],1,N)
plot(recovered_data_approx(1,:),recovered_data_approx(2,:),'gs')

Test_cov.m
%test_cov
clear
x=[2.5 0.5 2.2 1.9 3.1 2.3 2 1 1.5 1.1]'
y=[2.4 0.7 2.9 2.2 3.0 2.7 1.6 1.1 1.6 0.9]'
x_adj=x-mean(x),y_adj=y-mean(y)
cov_xy=0,cov_xx=0,cov_yy=0,N=length(x)
for (i=1:N)
cov_xx=x_adj(i)*x_adj(i)+cov_xx
cov_xy=x_adj(i)*y_adj(i)+cov_xy
cov_yy=y_adj(i)*y_adj(i)+cov_yy
end
'direct calculate'
c_N_minus_1_as_denon= [cov_xx/(N-1) cov_xy/(N-1) ; cov_xy/(N-1) cov_yy/(N-1)]
c_N_as_denon= [cov_xx/(N) cov_xy/(N) ; cov_xy/(N) cov_yy/(N)]

'using matlab cov function to calculate'


matlab_cov_xy=cov(x,y)
matlab_cov_xy1=cov(x,y,1)

Das könnte Ihnen auch gefallen