face recognition of an image

4 views (last 30 days)
chaithra k n
chaithra k n on 25 Mar 2015
Edited: Walter Roberson on 19 Mar 2017
sir i added new photos to the database . the data base is loading but its not taken in the recognition program myprogram is as follows
w=load_database();
%%Initializations
% We randomly pick an image from our database and use the rest of the
% images for training. Training is done on 399 pictues. We later
% use the randomly selectted picture to test the algorithm.
ri=round(400*rand(1,1)); % Randomly pick an index.
r=w(:,ri); % r contains the image we later on will use to test the algorithm
v=w(:,[1:ri-1 ri+1:end]); % v contains the rest of the 399 images.
N=20; % Number of signatures used for each image.
%%Subtracting the mean from v
O=uint8(ones(1,size(v,2)));
m=uint8(mean(v,2)); % m is the maen of all images.
vzm=v-uint8(single(m)*single(O)); % vzm is v with the mean removed.
%%Calculating eignevectors of the correlation matrix
% We are picking N of the 400 eigenfaces.
L=single(vzm)'*single(vzm);
[V,D]=eig(L);
V=single(vzm)*V;
V=V(:,end:-1:end-(N-1)); % Pick the eignevectors corresponding to the 10 largest eigenvalues.
%%Calculating the signature for each image
cv=zeros(size(v,2),N);
for i=1:size(v,2);
cv(i,:)=single(vzm(:,i))'*V; % Each row in cv is the signature for one image.
end
%%Recognition
% Now, we run the algorithm and see if we can correctly recognize the face.
subplot(121);
imshow(reshape(r,112,92));title('Looking for ...','FontWeight','bold','Fontsize',16,'color','red');
subplot(122);
p=r-m; % Subtract the mean
s=single(p)'*V;
z=[];
for i=1:size(v,2)
z=[z,norm(cv(i,:)-s,2)];
if(rem(i,20)==0),imshow(reshape(v(:,i),112,92)),end;
im getting error in line 2 pls help me to sort out this problem
  5 Comments
chaithra k n
chaithra k n on 31 Mar 2015
Edited: Walter Roberson on 18 Mar 2017
actually i removed all the images in the ready data base and tried to add add our photos, while loading data base i get error. this is the code for data base; function out=load_database(); % We load the database the first time we run the program.
persistent loaded;
persistent w;
if(isempty(loaded))
v=zeros(10304,400);
for i=1:40
cd(strcat('s',num2str(i)));
for j=1:10
a=imread(strcat(num2str(j),'.pgm'));
v(:,(i-1)*10+j)=reshape(a,size(a,1)*size(a,2),1);
end
cd ..
end
w=uint8(v); % Convert to unsigned 8 bit numbers to save memory.
end
loaded=1; % Set 'loaded' to aviod loading the database again.
out=w;
i was getting error in line v(:,(i-1)*10+j)=..... then i removed (:,(i-1)*10+j) then data base get trained but while running face recognition with my new data base its showing error in that line "size exceeds matrix dimention" .

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 25 Mar 2015
Edited: Image Analyst on 25 Mar 2015
Because you're using
ri=round(400*rand(1,1));
r=w(:,ri);
v=w(:,[1:ri-1 ri+1:end]);
you could run into problems, like if ri = 0 or ri is more than the number of rows in w. Plus if you happened to pick the first or last column, you won't be able to calculate v.
Try this more robust way
[rows, columns] = size(w);
ri = randperm(columns, 1);
r=w(:,ri);
allColumns = 1 : columns;
% Get a list of all columns other than ri.
otherColumns = setdiff(allColumns, ri)
v = w(:, otherColumns);
  5 Comments
Walter Roberson
Walter Roberson on 18 Mar 2017
Upgrade to R2016b. Or use bsxfun. Or repmat the mean to the same size as the data.
Mandeep Singh
Mandeep Singh on 19 Mar 2017
Edited: Walter Roberson on 19 Mar 2017
Am I converting the image properly or not.. I am getting the image matrix of correct size but not getting the result.
global ke1;
s=zeros(10304,1);
a=imresize(ke1,[112 92]);
s(:,1)=reshape(a,size(a,1)*size(a,2),1);
ab=uint8(s); % Convert to unsigned 8 bit numbers to save memory.
r=ab(:,1) % r contains the image we later on will use to test the algorithm
v=w(:,[1:399]); % v contains the rest of the 399 images.

Sign in to comment.

More Answers (1)

Mandeep Singh
Mandeep Singh on 18 Mar 2017
I just want to ask that in the above program how we can take user selected image instead of selecting random images.
  2 Comments
Walter Roberson
Walter Roberson on 18 Mar 2017
str2double the results of inputdlg prompting for the image number.
Or use uigetfile to get an image name and figure out from there which of the 39 images it was to calculate ri
Image Analyst
Image Analyst on 18 Mar 2017
Put the list of images into a listbox where the user can simply highlight one or more. For example, see this framework: MAGIC GUI

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!