CS295-3 Assigment 1

Christopher Cyr
9/20/00


Cumulative variance:





Bases Vectors:

Vectors 1->36

Vectors 37>70

Face Reconstruction:

Original Test Faces


 

Reconstruction with top 70 Bases:

Reconstruction with top 15 Bases:


Section A:

Linear interpolation between two faces using no training information:

5 Steps


10 Steps


Linear Interpolation Thru Eigenspace:

5 Steps

10 Steps:



Section B:

Original Un-occluded Image:

Occluded Image:
(see no evil w/ no depth perception?)
(speak no evil?)
(smell no evil?)
Corresponding Reconstructed Images:

(A)

(B)

(C) 


 

Part III

16x16 Texture Images:

Bases Vectors:

Singular Values:

32x32 Texture Images:

Bases Vectors:

Singular Values:

64x64 Texture Images:

Bases Vectors:

Singular Values:

Part I - Matlab Code:

This tells what each function below is used for in the process:


% Unknown Image reconstruction:

[data, mean] = imageFormat;

[U,S,V] = pca(data);

[weights, uk] = getWeights(U, 70);

reconstruct(uk, U, weights, mean, 70)
 
 

% Training Image reconstruction:

[data, mean] = imageFormat;

[U,S,V] = pca(data);

weights = getWeightsKnown(data, U, 70, mean);

reconstructKnown(U, weights, mean, 70, FileNumber)
 
 

%for eigen interp:

[data, mean] = imageFormat;

[U,S,V] = pca(data);

[weights, uk] = getInterpWeights(U, 70);

eigenfaceInterp(U, weights, mean, 70, _f1_, _f2_, _steps_)
 
 

%for linear face interp:

faceInterp( _f1_, _f2_, _steps_ )


function displayVector(U, numVectors)

I(1:128, 1:128) =0;

t = 1;

for i = 1:36

current = U(:, i);

size(current);

subplot(6, 6, t);

imagesc(reshape(current,size(I)));colormap(gray); drawnow;

t= t+1;

end

figure(2);

t = 1;

for i = 37:70

current = U(:, i);

size(current);

subplot(6, 6, t);

imagesc(reshape(current,size(I)));colormap(gray); drawnow;

t= t+1;

end



function eigenfaceInterp(Bases, Weights, mean, numvectors, f1, f2, steps)

I(1:128, 1:128) =0;

t=1;

temp(1:16384, 1) = 0;

recon(1:16384, 1) = 0;

F1_weights = Weights(:, f1);

F2_weights = Weights(:, f2);

stepD = (F2_weights - F1_weights)/steps;

tempweight = F1_weights;

size(stepD)

for i = 1:steps
 
 

F1_weights = tempweight + stepD;
 
 

for vectors = 1:numvectors
 
 

currentV = Bases(:, vectors);

recon = (currentV * F1_weights(vectors)) + temp;

temp = recon;

tempweight = F1_weights;

end

recon = temp + mean;

subplot(2, 3, t);

imagesc(reshape(recon,size(I)));colormap(gray); drawnow;

t=t+1;

temp(1:16384, 1) = 0;

recon(1:16384, 1) = 0;

end
 
 

faceInterp.m

function faceInterp(f1, f2, steps)

i2(1:128, 1:128) = 0;

fid = fopen('trainNames.m','r');

S = fscanf(fid,'%s');

Data=[];

N_Samples=10; % there are actually 195 training images

for i=1:N_Samples

name=S((i-1)*14+1:i*14);

[r,g,b]=ppmread(['/pro/web/web/courses/cs295-3/Assignment1/images/faces/' name ]);

I=rgb2gray(r,g,b);

I=I(1:2:size(I,1),1:2:size(I,2));

sizeim=size(I)

Data=[Data I(:)];

i

clear I;

end;

Image1 = Data(:, f1);

Image2 = Data(:, f2);

stepsD = (Image2 - Image1) / steps;

temp = Image1;

subplot(3, 4, 1)

imagesc(reshape(Image1,sizeim));colormap(gray); drawnow;

t=2;

for i = 1:steps

Image1 = stepsD + temp;

subplot(3, 4, t) ;

imagesc(reshape(Image1,sizeim));colormap(gray); drawnow;

t= t+1;

temp = Image1;

end

subplot(3, 4, t)

imagesc(reshape(Image2,sizeim));colormap(gray); drawnow;


function [Weights, UnknownIM] = getWeights(Bases, numvectors)

fid = fopen('trainNames.m','r');

S = fscanf(fid,'%s');

UnknownIM=[];

N_Unknowns=100; % there are actually 195 training images

figure(2);

for i=1:N_Unknowns

name=S((i-1)*14+1:i*14);

[r,g,b]=ppmread(['/pro/web/web/courses/cs295-3/Assignment1/images/faces/' name ]);

I=rgb2gray(r,g,b);

I=I(1:2:size(I,1),1:2:size(I,2));

sizeim=size(I)

UnknownIM=[UnknownIM I(:)];

subplot(3, 4, i)

imagesc(I); colormap(gray); drawnow;

clear I;

end;

figure(1);

size(UnknownIM)

Weights(1:numvectors, 1:N_Unknowns) = 0;

for x = 1:N_Unknowns

image = UnknownIM(:, x);

for y = 1:numvectors

vector = Bases(:, y);

Weights(y, x) = dot(image, vector);

end

end

function [Weights, UnknownIM] = getWeights(Bases, numvectors)

fid = fopen('testNames.m','r');

S = fscanf(fid,'%s');

UnknownIM=[];

N_Unknowns=12; % there are actually 195 training images

figure(2);

for i=1:N_Unknowns

name=S((i-1)*14+1:i*14);

[r,g,b]=ppmread(['/pro/web/web/courses/cs295-3/Assignment1/images/faces/' name ]);

I=rgb2gray(r,g,b);

I=I(1:2:size(I,1),1:2:size(I,2));

sizeim=size(I)

UnknownIM=[UnknownIM I(:)];

subplot(3, 4, i)

imagesc(I); colormap(gray); drawnow;

clear I;

end;

figure(1);

size(UnknownIM)

Weights(1:numvectors, 1:N_Unknowns) = 0;

for x = 1:N_Unknowns

image = UnknownIM(:, x);

for y = 1:numvectors

vector = Bases(:, y);

Weights(y, x) = dot(image, vector);

end

end
 
 

function Weights = getWeights(data, Bases, numvectors, mean)

N_Unknowns = 100;

Weights(1:numvectors, 1:N_Unknowns) = 0;

for x = 1:N_Unknowns

image = data(:, x);

image = image;

for y = 1:numvectors

vector = Bases(:, y);

Weights(y, x) = dot(image, vector);

end

end




function [Data2, meanD] = imageFormat()

clear all

fid = fopen('trainNames.m','r');

S = fscanf(fid,'%s');

Data=[];

N_Samples=100; % there are actually 195 training images

for i=1:N_Samples

name=S((i-1)*14+1:i*14);

[r,g,b]=ppmread(['/pro/web/web/courses/cs295-3/Assignment1/images/faces/' name ]);

I=rgb2gray(r,g,b);

I=I(1:2:size(I,1),1:2:size(I,2));

sizeim=size(I)

Data=[Data I(:)];

imagesc(I); colormap(gray); drawnow;

i

clear I;

end;

meanD = mean(Data, 2);

Data2 = Data;

for i=1:N_Samples

Data2(:, i) = Data(:, i) - meanD;

end
 
 

function [U, S, V] = pca(Data)

N_Samples=100;

[U,S,V] = svd(Data, 0);

Variance(1:N_Samples) = 0;

size(Variance)

Result = sum(S, 2);

size(Result)

temp = 0;

for i=1:N_Samples

Variance(i) = (Result(i) * Result(i)) + temp;

temp = Variance(i);

end

plot(Variance);



function reconstruct(unknowns, Bases, Weights, mean, numvectors)

I(1:128, 1:128) =0;

t = 1;

temp(1:16384, 1) = 0;

recon(1:16384, 1) = 0;

for imagecount = 1:12

for vectors = 1:numvectors

currentW = Weights(vectors, imagecount);

currentV = Bases(:, vectors);

recon = (currentV * currentW) + temp;

temp = recon;

end

recon = temp + mean;

r2 = reshape(recon,size(I));

uk2 = reshape(unknowns(:, imagecount),size(I));

err = r2 - uk2;

sqerr = err * err;

sum(sum(sqerr))

figure(t);

imagesc(reshape(recon,size(I)));colormap(gray); drawnow;

temp(1:16384, 1) = 0;

recon(1:16384, 1) = 0;

t = t+1;

end


function reconstruct(Bases, Weights, mean, numvectors, num)

I(1:128, 1:128) =0;

t = 1;

temp(1:16384, 1) = 0;

recon(1:16384, 1) = 0;

for vectors = 1:numvectors

currentW = Weights(vectors, num);

currentV = Bases(:, vectors);

recon = (currentV * currentW) + temp;

temp = recon;

end

recon = temp + mean;

imagesc(reshape(recon,size(I)));colormap(gray); drawnow;