Reputation: 57
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
Upvotes: 0
Views: 172
Reputation: 57
I figured it out on my own.
function [correct]=PCALDA_Test(projectPCA, LDA, prototypeFace,
TotalMeanFace)
principleNum=50;
ldaNum=30;
people=40;
withinsamp=5;
totalcount=0;
correct=0;
projectLDA=LDA(:, 1:ldaNum); %LDA
FFaceTest=[];
for i=1:1:people
for j=2:2:10
s=['ORL3232' '\' num2str(i) '\' num2str(j) '.bmp'];
totalcount=totalcount+1;
tempF_Test=[];
matchX=imread(s);
matchX=double(matchX);
if(i==1 && j==2)
[row, col]=size(matchX);
end
for n=1:row
tempF_Test=[tempF_Test, matchX(n,:)];
end
FFaceTest=[FFaceTest; tempF_Test];
end
end
meanTotalTest=mean(FFaceTest);
TestNor=FFaceTest-meanTotalTest;
pcaTotalFace_Test=[];
for i=1:1:withinsamp*people
tmpFace=TestNor(i,:);
tmpFace=tmpFace*projectPCA;
pcaTotalFace_Test=[pcaTotalFace_Test;tmpFace];
end
prototypeFace_Test=pcaTotalFace_Test*projectLDA; %map PCA testSet to LDA
[n,m]=size(prototypeFace);
C=floor(prototypeFace);
for id=1:1:withinsamp*people
P=prototypeFace_Test(id,:);
Distance=zeros([1,n]);
Distance=sqrt(P.^2*ones(size(C'))+ones(size(P))*(C').^2-2*P*C');
[minValue,minrow]=min(Distance);
if abs(id-minrow) <=4
correct=correct+1;
end
end
Recognition_rate=double(correct*100/totalcount);
fprintf('Number of correction : %d\nNumber of incorrection : %d\nRecognition Rate : %.4f', correct, 200-correct, Recognition_rate);
disp(' %');
Upvotes: 0