I've been working on a simple machine learning on Matlab
My dataset looks like:
There are 10 pictures in a folder, 5 for training, and 5 for testing.
Totally 40 folders, or 400 pictures in my dataset.
First, I read 200 images(32*32pixel) into a matrix called FFace which size is 200x1024.
Then, perform PCA on FFace and get pcaTotalFace(200x50).
Next, perform LDA and get prototypeFace(200x50).
I've successfully reduce FFace(200x1024) to prototypeFace(200x50).
My question is : How to do template matching between remaining 200 images and prototypeFace?
Below are my PCA and LDA code for reference. PCA:
function [FFace, TotalMeanFace, pcaTotalFace, projectPCA, eigvector,
prototypeFace]=PCALDA_Train
people=40;
withinsamp=5;
principlenum=50; %reduction to dimension of 50
FFace=[]; %store every traning picture(200*1024)
for k=1:1:people
for m=1:2:10
matchstring=['dataset' '\' num2str(k) '\' num2str(m) '.bmp'];
matchX=imread(matchstring);
matchX=double(matchX);
if(k==1 && m==1)
[row, col]=size(matchX);
end
matchtmpF=[];
% arrange the image into a vector
for n=1:row
matchtmpF=[matchtmpF, matchX(n,:)]; %1*32 row concat 32 times
end
FFace=[FFace;matchtmpF]; % col concat
end
end
TotalMeanFace=mean(FFace);
FFaceNor=FFace-TotalMeanFace;
covPCA=FFaceNor'*FFaceNor;
[Vec, Val]=eig(covPCA);
eigval=diag(Val);
[junk, index]=sort(eigval, 'descend');
PCA=Vec(:,index);
eigval=eigval(index);
projectPCA=PCA(:,1:principlenum); %extract the principle component
pcaTotalFace=[];
for i=1:1:withinsamp*people
tmpFace=FFaceNor(i,:);
tmpFace=tmpFace*projectPCA;
pcaTotalFace=[pcaTotalFace; tmpFace];
end
LDA:
classMean=[];
SW=[];
for i=1:withinsamp:withinsamp*people
withinFace=pcaTotalFace(i:i+withinsamp-1,:);
if(i==1)
meanwithinFace=mean(withinFace);
withinFace=withinFace-meanwithinFace;
SW=withinFace'*withinFace %cov(withinFace)
classMean=mean(withinFace);
end
if(i>1)
meanwithinFace=mean(withinFace);
withinFace=withinFace-meanwithinFace;
SW=SW+withinFace'*withinFace;
classMean=[classMean;mean(withinFace)];
end
end
pcaTotalMean=mean(pcaTotalFace);
classMean=classMean-pcaTotalMean;
SB=classMean'*classMean;
[eigvector, eigvalue]=eig(inv(SW)*SB);
eigvalue=diag(eigvalue);
[junk, index]=sort(eigvalue, 'descend');
eigvalue=eigvalue(index);
eigvector=eigvector(:,index);
prototypeFace=pcaTotalFace*eigvector;
end