How could I compare two images?

Hi, I'm new using matlab and I was sent an assignment that consists on creating a face recognizer. I have almost finished the code but I'm having problems at the end. The steps that I'm doing are
1st. Load images and convert them to gray scale
2nd. Create an array of each image and from those arrays, create a single matrix of all the images
3rd. Do the SVD (I know that I can do with the eigen faces but they told us to use this technique instead)
4th. Load an image that will be compared to the matrix of images
5th. Calculate a similar image
The problem that I have is that it isn't showing the similar image that was found on the matrix. I'm doing the norm between the components of A (the matrix of images) in base of U transposed and the test image. And then for looking for the minimum value between the A and the result of the norm I'm using the function called min. And after that I reshape the result and show it on screen. Here is the code, I think that everything is ok till the part that I calculate a similar image.
clear all, close all;clc;
N = 200; %Size of the images
M = 12; %Number of images
%1. Load the images
%George Clooney
C1 = imread('C1.jpg','jpg');
C1 = rgb2gray(C1);
C1 = double(C1);
C1 = reshape(C1,40000,1);
C2 = imread('C2.jpg','jpg');
C2 = rgb2gray(C2);
C2 = double(C2);
C2 = reshape(C2,40000,1);
C3 = imread('C3.jpg','jpg');
C3 = rgb2gray(C3);
C3 = double(C3);
C3 = reshape(C3,40000,1);
%Sandra Bullock
B1 = imread('B1.jpg','jpg');
B1 = rgb2gray(B1);
B1 = double(B1);
B1 = reshape(B1,40000,1);
B2 = imread('B2.jpg','jpg');
B2 = rgb2gray(B2);
B2 = double(B2);
B2 = reshape(B2,40000,1);
B3 = imread('B3.jpg','jpg');
B3 = rgb2gray(B3);
B3 = double(B3);
B3 = reshape(B3,40000,1);
%Jared Leto
L1 = imread('L1.jpg','jpg');
L1 = rgb2gray(L1);
L1 = double(L1);
L1 = reshape(L1,40000,1);
L2 = imread('L2.jpg','jpg');
L2 = rgb2gray(L2);
L2 = double(L2);
L2 = reshape(L2,40000,1);
L3 = imread('L3.jpg','jpg');
L3 = rgb2gray(L3);
L3 = double(L3);
L3 = reshape(L3,40000,1);
%Will Smith
S1 = imread('S1.jpg','jpg');
S1 = rgb2gray(S1);
S1 = double(S1);
S1 = reshape(S1,40000,1);
S2 = imread('S2.jpg','jpg');
S2 = rgb2gray(S2);
S2 = double(S2);
S2 = reshape(S2,40000,1);
S3 = imread('S3.jpg','jpg');
S3 = rgb2gray(S3);
S3 = double(S3);
S3 = reshape(S3,40000,1);
%2. join all the images
std.data{1} = C1;
std.data{2} = C2;
std.data{3} = C3;
std.data{4} = B1;
std.data{5} = B2;
std.data{6} = B3;
std.data{7} = L1;
std.data{8} = L2;
std.data{9} = L3;
std.data{10} = S1;
std.data{11} = S2;
std.data{12} = S3;
A = zeros(N*N,M);
%convert the images to a single matrix
for k=1:M
A(:,k) = std.data{k}(:);
end
save('matrixOfImages.mat', 'A');
%3. do the SVD for compressing the data
[U,S,V] = svd(A);
p = 5;
Ut = U(:,1:p);
St = S(1:p,1:p);
Vt = V(:,1:p);
W = St*Vt';
save('componentsOfA.mat','W');
%4. test an image to compare
test = imread('C1.jpg','jpg');
test = rgb2gray(test);
imshow(test);title('Image to compare:')
test = double(test);
test = reshape(test,40000,1);
test = Ut'*test;
%calculate a similar image
for m=1:12
Y(m) = norm(W(:,m)-test);
end
u = min(A,Y);
t = reshape(u,200,200);
imshow(t);title('The similar image is:');
I'll also leave the file attached maybe testing the program could be usefull because if I change the value of the min for example:
u = min(A,Y); it shows an error saying
"Error using min Matrix dimensions must agree."
and if i use min([A(:); Y(:)]); another error appears saying
"Error using reshape To RESHAPE the number of elements must not change."
Or maybe there is a better way to compare using the SVD?
Thanks

Answers (1)

Asked:

on 13 May 2016

Answered:

on 13 May 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!