-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f4ac4f0
commit 26a584e
Showing
5 changed files
with
287 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
capture.pgm | ||
/training/positive | ||
mean.png | ||
negative_eigenface.png | ||
positive_eigenface.png |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
|
||
%UNDERSTANDING PRINCIPAL COMPONENT ANALYSIS On face images | ||
|
||
%Here K=Total number of pixels of image=MN=112x92 | ||
|
||
%RMSE averaged over all images..........Change value of q for PCA | ||
%components | ||
|
||
%When q=6(All principal components retained) err = 00 | ||
%When q=5( 5 principal components retained) err = 9.3270 | ||
%When q=4( 4 principal components retained) err = 21.6476 | ||
%When q=3( 3 principal components retained) err = 29.9518 | ||
%-------------------------------------------------------------------------- | ||
|
||
clc; | ||
clear all; | ||
close all; | ||
|
||
%Reading and displaying 6 face images | ||
|
||
F1=imread('f1.pgm'); | ||
F2=imread('f2.pgm'); | ||
F3=imread('f3.pgm'); | ||
F4=imread('f4.pgm'); | ||
F5=imread('f5.pgm'); | ||
F6=imread('f6.pgm'); | ||
|
||
figure;imshow(F1);title('First image'); | ||
figure;imshow(F2);title('Second image'); | ||
figure;imshow(F3);title('Third image'); | ||
figure;imshow(F4);title('Forth image'); | ||
figure;imshow(F5);title('Fifth image'); | ||
figure;imshow(F6);title('Sixth image'); | ||
|
||
S=cat(3,F1,F2,F3,F4,F5,F6); %Representing images in 3 dimension stack | ||
[X,R]=imstack2vectors(S); %Convert images from matrix (MxN) to corresponding column vector [ColF1;ColF2;ColF3;ColF4;ColF5;ColF6] | ||
|
||
%Covariance matrix Generation (6x6) | ||
|
||
[K,n]=size(X); | ||
X=double(X); | ||
|
||
if n==1 | ||
C=0; | ||
m=X; | ||
else | ||
m=sum(X,1)/K; | ||
X1=X-m(ones(K,1),:); | ||
C=(X1'*X1)/(K-1); %Covariance matrix C . size=6x6 | ||
m=m'; | ||
end | ||
|
||
|
||
% PRINCIPAL COMPONENT IMPLEMENTATION | ||
|
||
m=m'; | ||
[V,D]=eig(C); %V =EIGEN VECTORED column matrix; D=Eigen valued diagonal matrix | ||
|
||
d=diag(D); %Extract diagonal elements of D | ||
|
||
[d,idx]=sort(d); | ||
d=flipud(d); | ||
idx=flipud(idx); | ||
|
||
D=diag(d); | ||
V=V(:,idx); | ||
|
||
|
||
q=input('Enter the number of principal eigenvectors (<= 6): '); %Use only first q principle components | ||
|
||
A=V(:,1:q)'; %q x n principal components x'formation matrix;rows are eigen vectors of C corresponding to the | ||
%first q largest eigenvalues. | ||
|
||
Mx=repmat(m,K,1); % Kx6 mean matrix | ||
|
||
X=X-Mx; %Mean subtracted images | ||
|
||
Y=A*(X') ; %size of Y=(qx6)*(6xK)=qxK | ||
|
||
X2=(A'*Y)'; %size of X2=((6x3)*(3xK))'=(6xK)'=Kx6 | ||
|
||
X2=X2+Mx; % Mean added to reconstruct the images | ||
|
||
Y=Y'; %Kxq matrix | ||
|
||
|
||
% m=m'; %nx1 vector | ||
|
||
d=diag(D); | ||
ems=sum(d(q+1:end)); | ||
|
||
%Covariance matrix of transformed images | ||
m2=sum(Y,1)/K; | ||
Y1=Y-m2(ones(K,1),:); | ||
CY=(Y1'*Y1)/(K-1); %Covariance matrix C . size=6x6 | ||
|
||
|
||
%%Cy=A*C*A'; | ||
|
||
% Displaying principal component images | ||
|
||
% g1=Y(:,1); | ||
% g1=reshape(g1,112,92); | ||
% figure;imshow(g1,[]); | ||
% title('First principle component image'); | ||
% | ||
% g2=Y(:,2); | ||
% g2=reshape(g2,112,92); | ||
% figure;imshow(g2,[]); | ||
% title('Second principle component image'); | ||
% | ||
% g3=Y(:,3); | ||
% g3=reshape(g3,112,92); | ||
% figure;imshow(g3,[]); | ||
% title('Third principle component image'); | ||
% | ||
% g4=Y(:,4); | ||
% g4=reshape(g4,112,92); | ||
% figure;imshow(g4,[]); | ||
% title('Fourth principle component image'); | ||
% | ||
% g5=Y(:,5); | ||
% g5=reshape(g5,112,92); | ||
% figure;imshow(g5,[]); | ||
% title('Fifth principle component image'); | ||
% | ||
% g6=Y(:,6); | ||
% g6=reshape(g6,112,92); | ||
% figure;imshow(g6,[]); | ||
% title('Sixth principle component image'); | ||
% %-------------------------------------------------------------------------- | ||
% Reconstructing images | ||
%-------------------------------------------------------------------------- | ||
h1=X2(:,1); | ||
h1=reshape(h1,112,92); | ||
figure;imshow(h1,[]); | ||
title('Reconstructed image1'); | ||
|
||
h2=X2(:,2); | ||
h2=reshape(h2,112,92); | ||
figure;imshow(h2,[]); | ||
title('Reconstructed image2'); | ||
|
||
h3=X2(:,3); | ||
h3=reshape(h3,112,92); | ||
figure;imshow(h3,[]); | ||
title('Reconstructed image3'); | ||
|
||
h4=X2(:,4); | ||
h4=reshape(h4,112,92); | ||
figure;imshow(h4,[]); | ||
title('Reconstructed image4'); | ||
|
||
h5=X2(:,5); | ||
h5=reshape(h5,112,92); | ||
figure;imshow(h5,[]); | ||
title('Reconstructed image5'); | ||
|
||
h6=X2(:,6); | ||
h6=reshape(h6,112,92); | ||
figure;imshow(h6,[]); | ||
title('Reconstructed image6'); | ||
|
||
%Finding Reconstruction error (rmse) | ||
|
||
|
||
|
||
disp('RMSE Errors in reconstruction:'); | ||
D1=double(F1(:,:))-double(h1); | ||
err1=sqrt(sum(sum(D1.^2))/(K)) | ||
|
||
D2=double(F2)-double(h2); | ||
err2=sqrt(sum(sum(D2.^2))/(K)) | ||
|
||
D3=double(F3)-double(h3); | ||
err3=sqrt(sum(sum(D3.^2))/(K)) | ||
|
||
D4=double(F4)-double(h4); | ||
err4=sqrt(sum(sum(D4.^2))/(K)) | ||
|
||
D5=double(F5)-double(h5); | ||
err5=sqrt(sum(sum(D5.^2))/(K)) | ||
|
||
D6=double(F6)-double(h6); | ||
err6=sqrt(sum(sum(D6.^2))/(K)) | ||
|
||
|
||
err=(err1+err2+err3+err4+err5+err6)/6; | ||
disp('RMSE averaged over all images='); | ||
disp(err); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import PIL | ||
from PIL import Image | ||
|
||
# taking 1 image and resizing as: 92x112 | ||
basewidth = 92 | ||
img = Image.open('../training/positive/positive_000.pgm') | ||
wpercent = (basewidth / float(img.size[0])) | ||
hsize = int((float(img.size[1]) * float(0.525))) | ||
img = img.resize((basewidth, hsize), PIL.Image.ANTIALIAS) | ||
img.save('f1.pgm') | ||
|
||
# taking 2 image and resizing as: 92x112 | ||
img = Image.open('../training/positive/positive_001.pgm') | ||
wpercent = (basewidth / float(img.size[0])) | ||
hsize = int((float(img.size[1]) * float(0.5))) | ||
img = img.resize((basewidth, hsize), PIL.Image.ANTIALIAS) | ||
img.save('f2.pgm') | ||
|
||
# taking 3 image and resizing as: 92x112 | ||
img = Image.open('../training/positive/positive_002.pgm') | ||
wpercent = (basewidth / float(img.size[0])) | ||
hsize = int((float(img.size[1]) * float(0.408))) | ||
img = img.resize((basewidth, hsize), PIL.Image.ANTIALIAS) | ||
img.save('f3.pgm') | ||
|
||
# taking 4 image and resizing as: 92x112 | ||
img = Image.open('../training/positive/positive_003.pgm') | ||
wpercent = (basewidth / float(img.size[0])) | ||
hsize = int((float(img.size[1]) * float(0.395))) | ||
img = img.resize((basewidth, hsize), PIL.Image.ANTIALIAS) | ||
img.save('f4.pgm') | ||
|
||
# taking 5 image and resizing as: 92x112 | ||
img = Image.open('../training/positive/positive_004.pgm') | ||
wpercent = (basewidth / float(img.size[0])) | ||
hsize = int((float(img.size[1]) * float(0.4))) | ||
img = img.resize((basewidth, hsize), PIL.Image.ANTIALIAS) | ||
img.save('f5.pgm') | ||
|
||
# taking 6 image and resizing as: 92x112 | ||
img = Image.open('../training/positive/positive_005.pgm') | ||
wpercent = (basewidth / float(img.size[0])) | ||
hsize = int((float(img.size[1]) * float(0.365))) | ||
img = img.resize((basewidth, hsize), PIL.Image.ANTIALIAS) | ||
img.save('f6.pgm') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
function [X, R] = imstack2vectors(S, MASK) | ||
%IMSTACK2VECTORS Extracts vectors from an image stack. | ||
% [X, R] = imstack2vectors(S, MASK) extracts vectors from S, which | ||
% is an M-by-N-by-n stack array of n registered images of size | ||
% M-by-N each (see Fig. 11.24). The extracted vectors are arranged | ||
% as the rows of array X. Input MASK is an M-by-N logical or | ||
% numeric image with nonzero values (1s if it is a logical array) | ||
% in the locations where elements of S are to be used in forming X | ||
% and 0s in locations to be ignored. The number of row vectors in X | ||
% is equal to the number of nonzero elements of MASK. If MASK is | ||
% omitted, all M*N locations are used in forming X. A simple way | ||
% to obtain MASK interactively is to use function roipoly. Finally, | ||
% R is an array whose rows are the 2-D coordinates containing the | ||
% region locations in MASK from which the vectors in S were | ||
% extracted to form X. | ||
|
||
% Copyright 2002-2004 R. C. Gonzalez, R. E. Woods, & S. L. Eddins | ||
% Digital Image Processing Using MATLAB, Prentice-Hall, 2004 | ||
% $Revision: 1.6 $ $Date: 2003/11/21 14:37:21 $ | ||
|
||
% Preliminaries. | ||
[M, N, n] = size(S); | ||
if nargin == 1 | ||
MASK = true(M, N); | ||
else | ||
MASK = MASK ~= 0; | ||
end | ||
|
||
% Find the set of locations where the vectors will be kept before | ||
% MASK is changed later in the program. | ||
[I, J] = find(MASK); | ||
R = [I, J]; | ||
|
||
% Now find X. | ||
|
||
% First reshape S into X by turning each set of n values along the third | ||
% dimension of S so that it becomes a row of X. The order is from top to | ||
% bottom along the first column, the second column, and so on. | ||
Q = M*N; | ||
X = reshape(S, Q, n); | ||
|
||
% Now reshape MASK so that it corresponds to the right locations | ||
% vertically along the elements of X. | ||
MASK = reshape(MASK, Q, 1); | ||
|
||
% Keep the rows of X at locations where MASK is not 0. | ||
X = X(MASK, :); |