当前位置: 代码迷 >> 综合 >> 【图像识别】基于主成分分析PCA实现人脸识别matlab代码
  详细解决方案

【图像识别】基于主成分分析PCA实现人脸识别matlab代码

热度:22   发布时间:2023-12-03 22:34:29.0

?1 简介

人脸识别是生物特征识别和人工智能领域特别重要的课题之一.讨论了统计主成分分析法(Principal Component Analysis,PCA)在人脸识别中的应用.PCA是基于统计的方法,可以对人脸库数据起到降低维数,去除相关性等作用.通过Kauhunen-Loeve变换(K—L变换)将人脸库变换到新的坐标系,得到人脸特征子空间,然后将待测人脸图像投影到特征子空间,最后利用2-范数距离分类器进行分类,从而达到识剐的目的.最后利用人脸库对其进行了测试.

img

2 部分代码

%%Using Eigen vectors,Matrix,Normalization of Image & Matrix comparison.% Face recognition 
clear all
close all
clc
% To Take number of images on your training set.
M=20;%Chosen std and mean. 
%It can be any number that it is close to the std and mean of most of the images.
um=100;
ustd=80;%read and show images(here pgm format);
S=[]; %img matrix
figure(1);
for i=1:M
str=strcat(int2str(i),'.pgm'); %concatenates two strings that form the name of the image
eval('img=imread(str);');
subplot(ceil(sqrt(M)),ceil(sqrt(M)),i)
imshow(img)
if i==3title('Image Data set','fontsize',18)
end
% drawnow;
[irow icol]=size(img); % get the number of rows (N1) and columns (N2)
temp=reshape(img',irow*icol,1);   %creates a (N1*N2 x1 matrix
S=[S temp];     %X is a N1*N2xM matrix after finishing the sequence%this is our S
end%Here we change the mean and std of all images. We normalize all images.
%This is done to reduce the error due to lighting conditions.
for i=1:size(S,2)
temp=double(S(:,i));
m=mean(temp);
st=std(temp);
S(:,i)=(temp-m)*ustd/st+um;
end%show normalized images
%figure(2);
%for i=1:M
% str=strcat(int2str(i),'.jpg');
% img=reshape(S(:,i),icol,irow);
% img=img';
% eval('imwrite(img,str)');  
% subplot(ceil(sqrt(M)),ceil(sqrt(M)),i)
% imshow(img)
% drawnow;
% if i==3
%   title('Normalized Image Data Set','fontsize',18)
% end
%end%mean image;
m=mean(S,2); %obtains the mean of each row instead of each column
tmimg=uint8(m); %converts to unsigned 8-bit integer. Values range from 0 to 255
img=reshape(tmimg,icol,irow); %takes the N1*N2x1 vector and creates a N2xN1 matrix
img=img';   %creates a N1xN2 matrix by transposing the image.
figure(3);
imshow(img);
title('Mean Image','fontsize',18)% Change image for manipulation
ImgMan=[]; % A matrix 
for i=1:M
temp=double(S(:,i));
ImgMan=[ImgMan temp];
end%Covariance matrix C=A'A, L=AA'
A=ImgMan';
L=A*A';
% vv are the eigenvector for L
% dd are the eigenvalue for both L=ImgMan'*ImgMan and C=ImgMan*ImgMan';
[vv dd]=eig(L);
% Sort and eliminate those whose eigenvalue is zero
v=[];
d=[];
for i=1:size(vv,2)
if(dd(i,i)>1e-4)v=[v vv(:,i)];d=[d dd(i,i)];
end
end%sort, will return an ascending sequence
[B index]=sort(d);
ind=zeros(size(index));
dtemp=zeros(size(index));
vtemp=zeros(size(v));
len=length(index);
for i=1:len
dtemp(i)=B(len+1-i);
ind(i)=len+1-index(i);
vtemp(:,ind(i))=v(:,i);
end
d=dtemp;
v=vtemp;%Normalization of eigenvectors
for i=1:size(v,2)   %access each column
kk=v(:,i);
temp=sqrt(sum(kk.^2));
v(:,i)=v(:,i)./temp;
end%Eigenvectors of C matrix
u=[];
for i=1:size(v,2)
temp=sqrt(d(i));
u=[u (ImgMan*v(:,i))./temp];
end%Normalization of eigenvectors
for i=1:size(u,2)
kk=u(:,i);
temp=sqrt(sum(kk.^2));
u(:,i)=u(:,i)./temp;
end% show eigenfaces;
figure(4);
for i=1:size(u,2)
img=reshape(u(:,i),icol,irow);
img=img';
img=histeq(img,255);
subplot(ceil(sqrt(M)),ceil(sqrt(M)),i)
imshow(img)
drawnow;
if i==3title('Eigenfaces','fontsize',18)
end
end% Find the weight of each face in the training set.
omega = [];
for h=1:size(ImgMan,2)
WW=[];  
for i=1:size(u,2)t = u(:,i)';  WeightOfImage = dot(t,ImgMan(:,h)');WW = [WW; WeightOfImage];
end
omega = [omega WW];
end% Acquire new image
% Note: the input image must have a pgm or jpg extension. 
%   It should have the same size as the ones in your training set. 
%   It should be placed on your desktop 
InputImage = input('Please enter the name of the image and its extension \n','s');
InputImage = imread(strcat('',InputImage));
figure(5)
subplot(1,2,1)
imshow(InputImage); colormap('gray');title('Input image','fontsize',18)
InImage=reshape(double(InputImage)',irow*icol,1); 
temp=InImage;
me=mean(temp);
st=std(temp);
temp=(temp-me)*ustd/st+um;
NormImage = temp;
Difference = temp-m;p = [];
aa=size(u,2);
for i = 1:aa
pare = dot(NormImage,u(:,i));
p = [p; pare];
end
ReshapedImage = m + u(:,1:aa)*p; %m is the mean image, u is the eigenvector
ReshapedImage = reshape(ReshapedImage,icol,irow);
ReshapedImage = ReshapedImage';
%show the reconstructed image.
subplot(1,2,2)
imagesc(ReshapedImage); colormap('gray');
title('Matched image','fontsize',18)%InImWeight = [];
%for i=1:size(u,2)
% t = u(:,i)';
% WeightOfInputImage = dot(t,Difference');
% InImWeight = [InImWeight; WeightOfInputImage];
%end%ll = 1:M;
%figure(68)
%subplot(1,2,1)
%stem(ll,InImWeight)
%title('Weight of Input Face','fontsize',14)% Find Euclidean distance
%e=[];
%for i=1:size(omega,2)
% q = omega(:,i);
% DiffWeight = InImWeight-q;
% mag = norm(DiffWeight);
% e = [e mag];
%end%kk = 1:size(e,2);
%subplot(1,2,2)
%stem(kk,e)
%title('Eucledian distance of input image','fontsize',14)%MaximumValue = max(e)
%MinimumValue = min(e)

3 仿真结果

4 参考文献

[1]孙强, 叶玉堂, 邢同举,等. 基于主成分分析法的人脸识别的探讨与研究[J]. 电子设计工程, 2011, 19(20):101-104.

部分理论引用网络文献,若有侵权联系博主删除。

  相关解决方案