⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gettrainingdata.m

📁 先用“gettrainingdata”获取人脸样本库的特征
💻 M
字号:
clear all
close all
clc
% number of images on your training set.
M=300;

%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;%for '1.bmp',the 'um' is 100.1504.//tropic zhang 4/14
ustd=80;%for '1.bmp',the 'ustd' is 79.7198.//tropic zhang 4/14

%read and show images(bmp);
S=[];   %img matrix
%figure(1);
for i=1:M
    str=strcat(int2str(i),'.bmp'); %concatenates two strings that form the name of the image
    str=strcat('F:\zgq\work\sampleface\',str);
    eval('img=imread(str);');
    %subplot(ceil(sqrt(M)),ceil(sqrt(M)),i)
    %imshow(img);
    %str=int2str(i);// 4.6,tropic zhang.
    %title(i,'fontsize',12)
    %if i==2
    %    title('Training 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;img' mean doing in rows.
    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),'.bmp');
%    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==2
 %       title('Normalized Training 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
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)   %[dd] is a 正对角矩阵//4.6 tropic zhang. 
        v=[v vv(:,i)];
        d=[d dd(i,i)];  %[d] is row行向量//4.6 tropic zhang.
    end
 end
 
 %sort,  will return an ascending sequence
 [B index]=sort(d);%B return an ascending sequence;index is the position of new numder in original row.//4.6 tropic
 ind=zeros(size(index));%also the size of [d];a row//4.6 tropic zhang. 
 dtemp=zeros(size(index));%also the size of [d];a row //4.6 tropic zhang.
 vtemp=zeros(size(v));%a matrix
 len=length(index);
%????change the order of d and v into an descending sequence.//4.6 tropic.
 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;
%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==2
  %      title('Eigenfaces','fontsize',18)
%    end
%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

%%%---save the data of training procession
save train irow icol ustd um m u M omega

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -