This article uses matlab use PCA Complete dimensionality reduction of data , Reconstruction and face recognition .

Reference article ：http://blog.csdn.net/watkinsong/article/details/38536463
<http://blog.csdn.net/watkinsong/article/details/38536463>

In my eyes PCA：

The dimension of data is too high , It's time-consuming and laborious , So I was wondering if I could just deal with some dimensions , And the results are consistent with the results of all dimensions . Dangdang ,PCA It's coming out . In a nutshell , It's a picture with 2000 Characteristic dimensions , And it's actually only one of them 100 dimension （ Even less ）, Has a huge impact on the outcome .

eg: For the Emperor , Cabinet chief assistant > Secondary > Three auxiliary > Four auxiliary >> Other unknown officials . So for the emperor , The proportion of effective governance strategies provided by the whole cabinet can be regarded as 60%, The whole civil service class can be regarded as 75%, Military official class 20%, rank and file 5%. That is to say, although there are many ordinary people , But there are few solutions , So I think we can ignore their proposal selectively . thirdly , Military officers can be ignored , Civil servant ..

To summarize, it's , We only focus on the most influential feature dimensions , Give up the characteristic dimension of insufficient influence .
<http://blog.csdn.net/watkinsong/article/details/38536463>

PCA The thinking process is as follows ：

1, Subtract mean , Centralization

2, Calculate covariance matrix

3, Selecting eigenvalues and eigenvectors

4, Transform training set into vector space composed of eigenvectors to reduce dimensions

5, Transpose test set times eigenvector , Add the centralized mean value to complete the reconstruction

6
, distinguish ： Select a photo of each person for registration , Subtract mean , Multiply by dimension reduction matrix （ I.e. eigenvector ）, And record the values of the recordset in the reduced dimension matrix . Traverse picture library , And do the same with the photos . The person of the picture whose distance between the value of the picture in the reduced dimension matrix and the value of the record set is the minimum Euclidean , Classification of the picture .

Matlab The code is as follows ：

%% Read in picture clear ; close all; clc %m = 1680; % number of samples trainset =
zeros(10, 50 * 40); % Picture size 50 * 40 file_path =
'C:\Users\zyfls\Desktop\ML\ Chapter V data dimensionality reduction \ data \AR\AR\';% Image folder path img_path_list =
dir(strcat(file_path,'*.bmp'));% Get all in this folder bmp Image in format img_num =
length(img_path_list);% Total number of acquired images for i = 10: img_num % Take out the first ten photos as the training set , Top ten as tests
image_name = img_path_list(i).name;% Image name end               %% before training
PCA, do feature normalization       mu = mean(trainset);%mean Function to find
The average value of elements along different dimensions in an array .     trainset_norm = bsxfun(@minus, trainset, mu);% Training set minus average
sigma = std(trainset_norm); %std Calculate standard deviation     trainset_norm =
bsxfun(@rdivide, trainset_norm, sigma);  %trainset_norm Point division sigma( standard deviation )
%% we could save the mean face mu to take a look the mean face
imwrite(uint8(reshape(mu, 50, 40)),
'C:\Users\zyfls\Desktop\ML Various screenshots \5\ at sixes and sevens PCA\meanface.bmp');       fprintf('mean
face saved. paused\n');     %% Calculating reduced dimension matrix     X = trainset; % just for convience
[m, n] = size(X);            U = zeros(n);     S = zeros(n);            Cov
= (1 / m) * X' * X;  % Calculate covariance matrix     [U, S, V] = svd(Cov);% singular value decomposition , Return a and X
Diagonal matrix of the same size S, Two orthogonal matrices U and V, And meet = U*S*V'. if A by m×n front , be U by m×m front ,V by n×n front . Singular value in S
On the diagonal of , Nonnegative in descending order .     % use SVD It can be used for non square array PCA handle , The following notes can be used to deal with the matrix     E = diag(S);
contribution = cumsum(E)./sum(E);% Calculate contribution rate %     [U,D]=eig(Cov);
% Calculation matrix R Eigenvector matrix of V And eigenvalue matrix D, Eigenvalue from small to large %     U=(rot90(U))';      % The eigenvector matrix U Sort from large to small %
D=rot90(rot90(D));  % Sorting eigenvalue matrix from large to small %     E=diag(D);          % Convert eigenvalue matrix to eigenvalue vector
%     ratio=0; % Cumulative contribution rate %     for k=1:n %         r=E(k)/sum(E);   % The first k Contribution rate of main components
%         ratio=ratio+r;  % Cumulative contribution rate %         if(ratio>=0.9)  % Take cumulative contribution rate greater than or equal to 90% Principal components of
%             break; %         end %     end     fprintf('compute cov
done.\n');     % Reduced dimension matrix U Eigenvectors in , In the dimension reduction of human face , Also known as feature face ,  U Each eigenvector in is equivalent to a direction of the found dimension reduction space .
utilize U You can map features to this space .       %% Show feature faces   U Top 10     for i = 1:10         ef = U(:, i);
img = ef;           minVal = min(img);           img = img - minVal;
max_val = max(abs(img));           img = img / max_val;           img =
reshape(img, 50, 40);         imwrite(img,
strcat('C:\Users\zyfls\Desktop\ML Various screenshots \5\ at sixes and sevens PCA\','eigenface', int2str(i),
'.bmp'));       end              fprintf('eigen face saved, paused.\n');
pause;            %% dimension reduction       k = 100; % reduce to 100
dimension       test = zeros(10, 50 * 40);       file_path =
'C:\Users\zyfls\Desktop\ML\ Chapter V data dimensionality reduction \ data \AR\AR\';% Image folder path       img_path_list =
dir(strcat(file_path,'*.bmp'));% Get all in this folder bmp Image in format     for i = 1:10  % Top ten test sets
image_name = img_path_list(i).name;% Image name         img =
imread(strcat('C:\Users\zyfls\Desktop\ML Various screenshots \5\', int2str(i), '.bmp'));
img = double(img);           test(i, :) = img(:);       end
% test set need to do normalization       test = bsxfun(@minus, test, mu);
% reduction  Dimensionality reduction     Uk = U(:, 1:k);  % Take from 1 reach dimsion Feature vector of as dimension reduction space     Z =
test * Uk;     fprintf('reduce done.\n');           %% Test set refactoring     %% for the
test set images, we only minus the mean face,       % so in the reconstruct
process, we need add the mean face back       Xp = Z * Uk';       % show
reconstructed face       for i = 1:10         face = Xp(i, :);         %face =
face .* sigma;         face = face + mu;         face = reshape((face), 50,
40);           imwrite(uint8(face),
strcat('C:\Users\zyfls\Desktop\ML Various screenshots \5\ at sixes and sevens PCA\','reconstructionface',
int2str(i), '.bmp'));         Face_re(i,:)=Xp(i,:)+mu;       end       e =
Face_re-test;     error(1,i)=norm(e);         %dispaly error rate
error_rate=error(1,i);       display(error_rate);    %1.9061e+04
% Reconstruction of training set , Because there are more than one training set sigma The matrix is multiplied here     %% for the train set reconstruction, we
minus the mean face and divide by standard deviation during the train       %
so in the reconstruction process, we need to multiby standard deviation first,
% and then add the mean face back       trainset_re = trainset_norm * Uk;
% reduction       trainset_re = trainset_re * Uk'; % reconstruction     for i =
11:25           train = trainset_re(i, :);           train = train .* sigma;
train = train + mu;         train = reshape(train, 50, 40);
imwrite(uint8(train), strcat('C:\Users\zyfls\Desktop\ML Various screenshots \5\ at sixes and sevens PCA\',
'reconstruction',int2str(i), 'train.bmp'));       end  The above code completes dimension reduction and reconstruction ：

The average face obtained is shown on the right ：

Characteristic face ：

Reconstruction image ：

<http://blog.csdn.net/watkinsong/article/details/38536463>

The above parts have completed dimension reduction , restructure .

distinguish ：

The identification code is as follows （ Including downsizing and reconstruction ）：

%% Read in picture clear ; close all; clc %m = 1680; % number of samples trainset =
zeros(10, 50 * 40); % Picture size 50 * 40 file_path =
'C:\Users\zyfls\Desktop\ML\ Chapter V data dimensionality reduction \ data \AR\AR\';% Image folder path img_path_list =
dir(strcat(file_path,'*.bmp'));% Get all in this folder bmp Image in format img_num =
length(img_path_list);% Total number of acquired images j=1; for i = 1: img_num % Take all the photos as a training set if(mod(i,14)
== 0)% Everyone's last one left for the test set continue; end image_name = img_path_list(i).name;% Image name %
name = image_name(1:3); % if strcmp(name,'001') img =
imread(strcat(file_path,image_name)); img = double(img); trainset(j, :) =
img(:); j=j+1; % end end %% before training PCA, do feature normalization mu =
mean(trainset);%mean Function to find The average value of elements along different dimensions in an array . trainset_norm = bsxfun(@minus,
trainset, mu);% Training set minus average sigma = std(trainset_norm); %std Calculate standard deviation trainset_norm =
bsxfun(@rdivide, trainset_norm, sigma); %trainset_norm Point division sigma( standard deviation ) %% we
could save the mean face mu to take a look the mean face
imwrite(uint8(reshape(mu, 50, 40)),
'C:\Users\zyfls\Desktop\ML Various screenshots \5\Recognition\meanface.bmp'); %% Computational dimension reduction matrix X =
trainset; % just for convience [m, n] = size(X); U = zeros(n); S = zeros(n);
Cov = (1 / m) * X' * X; % Calculate covariance matrix [U, S, V] = svd(Cov);% singular value decomposition , Return a and cov
Diagonal matrix of the same size S, Two orthogonal matrices U and V, And meet = U*S*V'. if A by m×n front , be U by m×m front ,V by n×n front . Singular value in S
On the diagonal of , Nonnegative in descending order . % use SVD It can be used for non square array PCA handle E = diag(S); contribution =
cumsum(E)./sum(E);% Calculate contribution rate fprintf('compute cov done.\n'); % Reduced dimension matrix U Eigenvectors in ,
In the dimension reduction of human face , Also known as feature face , U Each eigenvector in is equivalent to a direction of the found dimension reduction space . utilize U You can map features to this space . %% Show feature faces U Top 10
for i = 1:10 ef = U(:, i); img = ef; minVal = min(img); img = img - minVal;
max_val = max(abs(img)); img = img / max_val; img = reshape(img, 50, 40);
imwrite(img, strcat('C:\Users\zyfls\Desktop\ML Various screenshots \5\Recognition\','eigenface',
int2str(i), '.bmp')); end fprintf('eigen face saved, paused.\n'); pause; j=1;
regis = zeros(120,50*40); for i = 1:14: img_num % Take the first photo of each group and make a registration record set image_name =
img_path_list(i).name;% Image name % name = image_name(1:3); % if strcmp(name,'001')
img = imread(strcat(file_path,image_name)); img = double(img); regis(j, :) =
img(:); j=j+1; % end end regis = bsxfun(@minus,regis,mu); Uk = U(:, 1:100);
% Take from 1 reach 100 Feature vector of as dimension reduction space Zregis = regis * Uk;% The value of record registration record set in dimension reduction matrix %% dimension reduction
k = 100; % reduce to 100 dimension test = zeros(1680, 50 * 40); file_path =
'C:\Users\zyfls\Desktop\ML\ Chapter V data dimensionality reduction \ data \AR\AR\';% Image folder path img_path_list =
dir(strcat(file_path,'*.bmp'));% Get all in this folder bmp Image in format success = 0; for i = 1:
img_num % Traverse each photo to compare it with the registration record , Classify image_name = img_path_list(i).name;% Image name img =
imread(strcat('C:\Users\zyfls\Desktop\ML Various screenshots \5\', int2str(i), '.bmp')); img =
double(img); test(i, :) = img(:); test(i,:) = test(i,:) - mu; Uk = U(:, 1:100);
% Take from 1 reach dimsion Feature vector of as dimension reduction space Ztest = test * Uk;% The value of test set in reduced dimension matrix for j=1:120
mdist(j)=norm(Ztest(i,:)-Zregis(j,:));% Calculate distance from registration record end [C,I] =
min(mdist);% Return the minimum distance , And its location if(I<10) I = num2str(I); I = strcat('00',I); elseif
(I<100) I = num2str(I); I = strcat('0',I); else I = num2str(I); end name =
image_name(1:3);% Take the first three digits of the current picture to determine whether the classification is correct if strcmp(name,I) success = success + 1;
end end suc_rate = success/1680;
PS： The format of my picture library name is that the first three numbers represent different people , So this is the way to identify the correctness of classification .

That completes the whole thing PCA Dimension reduction of , Reconstruction and recognition , Finally finished the big homework .

Next one SR（ Sparse dictionary ） distinguish .

<http://blog.csdn.net/watkinsong/article/details/38536463>

<http://blog.csdn.net/watkinsong/article/details/38536463>

<http://blog.csdn.net/watkinsong/article/details/38536463>