我的代码以下列方式工作:
1.首先,它从训练集中获得了几张图像
2.加载这些图像后,我们找到标准化的面部,平均面部并进行多次计算。
3.接下来,我们要求我们想要识别的图像的名称
4.然后我们将输入图像投影到本征空间,并根据与特征脸的差异我们做出决定。
5.根据每个输入图像的特征权重向量,我们使用kmeans命令创建聚类。
我试过的源代码:
clear all
close all
clc
% number of images on your training set.
M=1200;
%Chosen std and mean.
%It can be any number that it is close to the std and mean of most of the images.
um=60;
ustd=32;
%read and show images(bmp);
S=[]; %img matrix
for i=1:M
str=strcat(int2str(i),'.jpg'); %concatenates two strings that form the name of the image
eval('img=imread(str);');
[irow icol d]=size(img); % get the number of rows (N1) and columns (N2)
temp=reshape(permute(img,[2,1,3]),[irow*icol,d]); %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
for i=1:M
str=strcat(int2str(i),'.jpg');
img=reshape(S(:,i),icol,irow);
img=img';
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.
% Change image for manipulation
dbx=[]; % A matrix
for i=1:M
temp=double(S(:,i));
dbx=[dbx temp];
end
%Covariance matrix C=A'A, L=AA'
A=dbx';
L=A*A';
% vv are the eigenvector for L
% dd are the eigenvalue for both L=dbx'*dbx and C=dbx*dbx';
[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 (dbx*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;
for i=1:size(u,2)
img=reshape(u(:,i),icol,irow);
img=img';
img=histeq(img,255);
end
% Find the weight of each face in the training set.
omega = [];
for h=1:size(dbx,2)
WW=[];
for i=1:size(u,2)
t = u(:,i)';
WeightOfImage = dot(t,dbx(:,h)');
WW = [WW; WeightOfImage];
end
omega = [omega WW];
end
% Acquire new image
% Note: the input image must have a bmp or jpg extension.
% It should have the same size as the ones in your training set.
% It should be placed on your desktop
ed_min=[];
srcFiles = dir('G:\newdatabase\*.jpg'); % the folder in which ur images exists
for b = 1 : length(srcFiles)
filename = strcat('G:\newdatabase\',srcFiles(b).name);
Imgdata = imread(filename);
InputImage=Imgdata;
InImage=reshape(permute((double(InputImage)),[2,1,3]),[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
InImWeight = [];
for i=1:size(u,2)
t = u(:,i)';
WeightOfInputImage = dot(t,Difference');
InImWeight = [InImWeight; WeightOfInputImage];
end
noe=numel(InImWeight);
% Find Euclidean distance
e=[];
for i=1:size(omega,2)
q = omega(:,i);
DiffWeight = InImWeight-q;
mag = norm(DiffWeight);
e = [e mag];
end
ed_min=[ed_min MinimumValue];
theta=6.0e+03;
%disp(e)
z(b,:)=InImWeight;
end
IDX = kmeans(z,5);
clustercount=accumarray(IDX, ones(size(IDX)));
disp(clustercount);
100张图像的运行时间:经过的时间是103.947573秒。
问题:
1.M = 50工作正常(即训练集包含50张图像)但M = 1200则没有(即训练集包含1200张图像)。它没有显示任何错误。没有输出。我等了10分钟仍然没有输出。问题是什么?我哪里错了?
答案 0 :(得分:1)
要回答第二个问题,您只需将生成的变量“保存”为工作目录(当前文件夹)中的.mat文件,以后可以访问。因此,在您的代码中,如果'训练特征脸'由变量'u'给出,您可以使用以下内容:
save('eigenface.mat','u')
这将创建一个名为 eigenface.mat 的.mat文件,其中包含变量'u',即特征脸。请注意,此变量保存在当前文件夹中。
在稍后的实例中,当您尝试使用测试数据时,您可以简单地“加载”此变量:
load('eigenface.mat')
这会自动将'u'加载到您的工作区中。 如有必要,您还可以在同一.mat文件中保存其他变量
save('eigenface.mat','u','v'...)
第一个问题的答案是代码尚未完成运行。如上面的评论部分所示,对代码进行矢量化(而不是使用 for 循环)可以显着提高速度。
<强> [编辑] 强>
由于图像不是很大,因此第一个for循环的代码不会显着减慢。您可以通过向量化代码来提高其余代码的性能。矢量化操作比for循环更快。此链接可能有助于理解矢量化:
http://www.mathworks.com/help/matlab/matlab_prog/vectorization.html
例如,第二个for循环可以用以下矢量化形式替换,如评论中所示
tempS = double(S);
meanS = mean(S,1);
stdS = std(S,0,1);
S = (tempS - meanS) ./ stdS;
使用MATLAB的定时器函数tic
和toc
来查找第一个for循环执行的时间。在for循环之前添加tic
,在之后添加toc
。如果50张图像所需的时间约为104秒,那么对于1200张图像,它将显着显着。