fisherface

http://blog.163.com/pz124578@126/blog/static/235226942012421111923805/

 

function [NumberImg, NumberImgInClass, A, m, m_class, W_opt, ProjectedImages, Dist_Face_ItsReconstruct_max] = FisherFaces(T) % Use Principle Component Analysis (PCA) + Fisher Linear Discrimination (FLD) to determine image space. This approach is called Fisherfaces. % Please refer to the paper Eigenfaces vs.Fisherfaces: Recognition Using Class Specific Linear Projection (IEEE) % % Description: This function gets a 2D matrix, containing all training image vectors % and returns 3 outputs which are extracted from training database. % % Argument: T - A 3D matrix, containing many class, each class represent one person. % Each class is a 2D matrix, containing all 1D image vectors of same person % Suppose all images in the training database have the same size of MxN. % So the length of 1D column vectors is MN and 'T' willbe a MNxP 2D matrix. % % Returns: NumberImg - Number of images in training database % NumberImgInClass - Number of images in each class in training databse % A - ((M*N) x P) Matrix of centered image vectors % m - ((M*N) x 1) Mean of all images in the training database % W_opt - ((M*N) x m) Eigen vectors of the SW^(-1)*SB (refer paper to understand more) % Dist_Face_ItsReconstruct_max - We do not need to consider this. It is computed to check if the image is a face image. % But it does not work well. % See also: EIG, MEAN %%%%%%%%%%%%%%%%%%%%%%%% Calculating the mean image m = mean(mean(T,2),3); % Computing the average face image m = (1/P)*sum(Tj's) (j = 1 : P) NumberClass=size(T,3); NumberImgInClass=size(T,2); NumberImg = NumberClass*NumberImgInClass; m_class=[]; for i=1:NumberClass % number of class m_class(:,i)=mean(T(:,:,i),2); end %%%%%%%%%%%%%%%%%%%%%%%% Calculating the deviation of each image from mean image A = []; % A matrix with dimension of (numberpixel * NumberImg) for i = 1 : NumberClass for j=1:NumberImgInClass temp = double(T(:,j,i)) - m; % Computing the difference image for each image in the training set A(i) = T(i) - m A=[A temp]; end end A_class=[]; for i = 1 : NumberClass for j=1:NumberImgInClass A_class(:,j,i) = double(T(:,j,i)) - m_class(:,i); % Computing the difference image for each image in the training set Ai = Ti - mi end end %%%%%%%%%%%%%%%%%%%%%%%% Snapshot method of Eigenface methos % We know from linear algebra theory that for a PxQ matrix, the maximum % number of non-zero eigenvalues that the matrix can have is min(P-1,Q-1). % Since the number of training images (P) is usually less than the number % of pixels (M*N), the most non-zero eigenvalues that can be found are equal % to P-1. So we can calculate eigenvalues of A'*A (a PxP matrix) instead of % A*A' (a M*NxM*N matrix). It is clear that the dimensions of A*A' is much % larger that A'*A. So the dimensionality will decrease. L = A'*A; % L is the surrogate of covariance matrix C=A*A'. [V D] = eig(L); % Diagonal elements of D are the eigenvalues for both L=A'*A and C=A*A'. %%%%%%%%%%%%%%%%%%%%%%%% Sorting and eliminating eigenvalues % All eigenvalues of matrix L are sorted and those who are less than a % specified threshold, are eliminated. So the number of non-zero % eigenvectors may be less than (P-1). temp2=[]; % sap xep cac eigenvectors tuong ung voi cac eigenvalues tu lon toi nho for i = 1 : size(V,2) for j=1:i if D(j,j)<D(i,i) temp1=D(i,i); temp2=V(:,i); D(i,i)=D(j,j); V(:,i)=V(:,j); D(j,j)=temp1; V(:,j)=temp2; end end end L_eig_vec = []; % L_eig_vec is matrix of eigenvectors of matrix L %for i = 1 : min(size(V,2),(NumberImgInClass-1)*NumberClass) for i = 1 : min(size(V,2),120) % chi lay toi da 120 eigenvector lon nhat (get at most 120 eigenvectors corresponding with 120 greatest eigenvalues ), day chinh la PCA: lay ra cac eigenvectors co ban nhat, co anh huong lon nhat. if( D(i,i)>1 ) % loai bo nhung eigenvector co eigenvalue bang 0 (trong Matlab tinh ra gan bang 0, chu ko hoan toan, do do bo nhung eigen vector tu 1 tro len), cac eigenvalues con lai co gia tri rat lon, khoang 1000000 tro len L_eig_vec = [L_eig_vec V(:,i)/norm(V(:,i))]; % chuan hoa cac vector (standardize eigenvectors), tuc la do lon vector phai bang 1 , viec nay cung giong nhu viec nhan caceigenvector nay voi lamda m? 1/2. end end %%%%%%%%%%%%%%%%%%%%%%%% Calculating the eigenvectors of covariance matrix 'C' % Eigenvectors of covariance matrix C (or so-called "Eigenfaces") % can be recovered from L's eiegnvectors. Eigenvector_ST = A * L_eig_vec; % A: centered image vectors Eigenvector_ST are eigenvectors of Covariance Matrix (A*transpose(A)) % Dimension of Eigenvector_ST=numberpixel * (NumberImgInClass-1)*NumberClass %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Tinh matran Sw=tong cac Sw cua tung lop%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%% Moi Sw cua tung lop se tinh theo cong thuc Sw_FLD=W_PCA'*Sw_PCA*W_PCA SW=[0]; for i=1:NumberClass for j=1:NumberImgInClass temp1=Eigenvector_ST'*A_class(:,j,i); temp2=A_class(:,j,i)'*Eigenvector_ST; SW=SW+temp1*temp2; % Tong Sw cua cac lop end end %SW %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Hoan tat tinh matran Sw=tong cac Sw cua tung lop%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Tinh matran Sb %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% SB=[0]; for i=1:NumberClass temp1=Eigenvector_ST'*(m_class(:,i)-m); temp2=(m_class(:,i)-m)'*Eigenvector_ST; SB=SB+NumberImgInClass*temp1*temp2; end %SB %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Hoan tat tinh matran Sb %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% [W lamda]=eig(SW^(-1)*SB); temp2=[]; % sap xep cac eigenvectors tuong ung voi cac eigenvalues tu lon toi nho for i = 1 : size(W,2) for j=1:i if lamda(j,j)<lamda(i,i) temp1=lamda(i,i); temp2=W(:,i); lamda(i,i)=lamda(j,j); W(:,i)=W(:,j); lamda(j,j)=temp1; W(:,j)=temp2; end end end Original_FLD_eig_vec = []; % L_eig_vec is matrix of eigenvectors of matrix L for i = 1 : min(size(W,2),15) if( lamda(i,i)>1 ) % loai bo nhung eigenvector co eigenvalue bang 0 (trong Matlab tinh ra gan bang 0, chu ko hoan toan, do do bo nhung eigen vector tu 1 tro len), cac eigenvalues con lai co gia tri rat lon, khoang 1000000 tro len Original_FLD_eig_vec = [Original_FLD_eig_vec W(:,i)]; % chuan hoa cac vector (standardize eigenvectors), tuc la do lon vector phai bang 1 , viec nay cung giong nhu viec nhan caceigenvector nay voi lamda m? 1/2. end end %FLD_eig_vec %norm(FLD_eig_vec(:,5)) % kiem tra xem cac eigenvector (trong ma tran FLD_eig_vec) cua SW^(-1)*SB co duoc chuan hoa chua (gia tri co bang 1 hay ko) Adjust_FLD_eig_vec = []; % L_eig_vec is matrix of eigenvectors of matrix L NumberEliminateVectors=1; for i = 1 : (size(Original_FLD_eig_vec,2)-NumberEliminateVectors) Adjust_FLD_eig_vec(:,i) = Original_FLD_eig_vec(:,i+NumberEliminateVectors); % chuan hoa cac vector (standardize eigenvectors), tuc la do lon vector phai bang 1 , viec nay cung giong nhu viec nhan caceigenvector nay voi lamda m? 1/2. end size(Adjust_FLD_eig_vec) W_opt=Eigenvector_ST*Adjust_FLD_eig_vec; % size(Eigenvector_ST) % kiem tra kich thuoc cua ma tran chua cac eigenvector cua A*At % size(FLD_eig_vec) % size(W_opt) %%%%%%%%%%%%%%%%%%%%%%%% Projecting centered image vectors into facespace % All centered images are projected into facespace by multiplying in Fisherfaces basis's. % Projected vector of each face will be its corresponding feature vector. ProjectedImages = []; for i = 1 : NumberImg temp = W_opt'*A(:,i); % Projection of centered images into facespace ProjectedImages = [ProjectedImages temp]; % ProjectedImages = [omega1 omega2 ...] in power point slide end %%%%%%%%%%%%%%%%%%%%%%%% Compute the threshold distance between an image face and its reconstruction Using formula Omega(mu) = Sum(Wi*Ui) Dist_Face_ItsReconstruct_matrix=[]; temp1=[]; for i=1:NumberImg temp1=W_opt(:,1)'*A(:,i)*W_opt(:,1); % tinh gia tri ban dau cho temp1 de temp1 co kich thuoc (sizeheight(eigenfaces) x 1) for j=2:size(W_opt,2) temp1=temp1+W_opt(:,j)'*A(:,i)*W_opt(:,j); % compute Sum(ui*wi) end temp2=norm(A(:,i)-temp1); Dist_Face_ItsReconstruct_matrix=[Dist_Face_ItsReconstruct_matrix temp2]; end [Dist_Face_ItsReconstruct_max, Dist_Face_ItsReconstruct_max_number]=max(Dist_Face_ItsReconstruct_matrix);

你可能感兴趣的:(fisherface)