displaying images
- From: kfs <just.5o5a@xxxxxxxxxxx>
- Date: Sun, 28 Dec 2008 18:05:23 EST
hi all
i have an algorithm that displays 5 top matches of an image from a database, using the following code
matrice=zeros(size(data{1,1},1),face_number);
for ii=1:face_number
matrice(:,ii)=double(data{ii,1});
end
somma=sum(matrice,2);
media=somma/face_number;
for ii=1:face_number
matrice(:,ii)=matrice(:,ii)-media;
end
matrice=matrice/sqrt(face_number);
% up to now matrix "matrice" is matrix "A" of Turk's paper
elle=matrice'*matrice;
% matrix "elle" is matrix "L" of Turk's paper
% eigenvalues and eigenvectors of the "reduced" matrix A'*A
[V,D] = eig(elle);
% the following multiplication is performed to obtain the
% eigenvectors of the original matrix A*A' (see Turk's paper)
% See also Karhunen-Loeve algorithm, for face recognition
if det(D)~=0
% This modification to the original algorithm improves
% the recognition rate (if applicable!)
Vtrue=matrice*V*(abs(D))^-0.5;
else
Vtrue=matrice*V;
end
%Vtrue=matrice*V;
Dtrue=diag(D);
% the eigenvalues are sorted by order and only M' of them
% are taken. We impose M' equal to the number of classes
% (max_class-1)
[Dtrue,ordine]=sort(Dtrue);
Dtrue=flipud(Dtrue);
ordine=flipud(ordine);
Vtrue(:,1:face_number)=Vtrue(:,ordine);
Vtrue=Vtrue(:,1:max_class-1);
Dtrue=Dtrue(1:max_class-1);
% Eigenvectors and eigenvalues of EigenFaces method
Vtrue0 = Vtrue;
Dtrue0 = Dtrue;
% Vtrue0 will be used for "space - reduction"
%----------------------------------------------------------
% FISHERFACES ALGORITHM
L = length(media);
Sb = zeros(L,L); % Between-class scatter matrix
Sw = zeros(L,L); % Within-class scatter matrix
mean_classes = zeros(L,max_class-1);
person = zeros(max_class-1,1); % number of images for each class (=person)
for ii=1:face_number
ID = data{ii,2};
mean_classes(:,ID) = mean_classes(:,ID)+double(data{ii,1});
person(ID) = person(ID)+1;
end
for ii=1:(max_class-1)
mean_classes(:,ii) = mean_classes(:,ii)/person(ii);
end
% Sb computation
for ii=1:(max_class-1)
v = mean_classes(:,ii)-media;
Sb = Sb + v*v';
end
% Sw computation
for ii=1:face_number
ID = data{ii,2};
v = double(data{ii,1})-mean_classes(:,ID);
Sw = Sw + v*v';
end
% I now pass into eigenfaces space (reduction)
Sbr = Vtrue0'*Sb*Vtrue0;
Swr = Vtrue0'*Sw*Vtrue0;
[V,D] = eig(Sbr,Swr);
% now I return to original space
Vtrue = Vtrue0*V;
Dtrue = diag(D);
[Dtrue,ordine] = sort(Dtrue);
Dtrue = flipud(Dtrue);
ordine = flipud(ordine);
nordine = length(ordine);
Vtrue(:,1:nordine) = Vtrue(:,ordine);
Vtrue=Vtrue(:,1:max_class-1);
Dtrue=Dtrue(1:max_class-1);
% Normalization to 1
% The recognition rate improves with such normalization
lengthV = size(Vtrue,2);
for ii=1:lengthV
if norm(Vtrue(:,ii))~=0 && norm(Vtrue(:,ii))~=Inf
Vtrue(:,ii)=Vtrue(:,ii)/norm(Vtrue(:,ii));
end
end
%----------------------------------------------------------
% we calculate the eigenface components of
% the normalized input (mean-adjusted). I.e. the input
% image is projected into "face-space"
pesi=Vtrue'*(ingresso-media);
pesi_database = zeros(max_class-1,max_class-1);
pesi_database_mediati = zeros(max_class-1,max_class-1);
numero_elementi_classe=zeros(max_class-1,1);
for ii=1:face_number
ingresso_database=double(data{ii,1});
classe_database=data{ii,2};
pesi_correnti=Vtrue'*(ingresso_database-media);
pesi_database(:,classe_database)=pesi_database(:,classe_database)+pesi_correnti;
numero_elementi_classe(classe_database)=numero_elementi_classe(classe_database)+1;
end
for ii=1:(max_class-1)
pesi_database_mediati(:,ii)=pesi_database(:,ii)/numero_elementi_classe(ii);
end
% pesi_database_mediati is a matrix with the averaged eigenface components of the images
% present in database. Each class has its averaged eigenface.
% We want to find the nearest (in norm) vector to the input
% eigenface components.
distanze_pesi=zeros(max_class-1,1);
for ii=1:(max_class-1)
distanze_pesi(ii)=norm(pesi-pesi_database_mediati(:,ii));
disp(distanze_pesi(ii));
namehanadi(ii)=data{ii,3};
%distanze_pesi(ii) = sum((abs(pesi-pesi_database_mediati(:,ii))));
end
prompt={strcat(messaggio,'thresssssssssh**** ')};
answer=inputdlg(prompt);
threshhold=double(str2num(char(answer)));
flag=1;
maxi=0;
index=0;
xx=0;
hanadi=zeros(5,2);
for ii=1:(max_class-1)
maxi=0;
if(distanze_pesi(ii) < threshhold)
if(flag <= 5)
hanadi(flag,1)=distanze_pesi(ii);
hanadi(flag,2)=ii;
flag = flag + 1;
else
for xx=1:5
if(maxi < hanadi(xx,1))
maxi=hanadi(xx,1);
index=xx;
end
end
if(distanze_pesi(ii) < maxi)
hanadi(index,1)=distanze_pesi(ii);
hanadi(index,2)=ii;
end
end
end
end
% [minimo_pesi,posizione_minimo_pesi]=min(distanze_pesi);
% % now we are evaluating the distance of the mean-normalized
% % input face from the "space-face" in order to detrmine if
% % the input image is a face or not.
% proiezione=zeros(size(data{1,1},1),1);
% for ii=1:(max_class-1)
% proiezione=proiezione+pesi(ii)*Vtrue(:,ii);
% end
% distanza_spazio_facce=norm((ingresso-media)-proiezione);
messaggio1='See Matlab Command Window to see matching result.';
messaggio2='';
messaggio3='';
messaggio4='';
msgbox(strcat(messaggio1,messaggio2,messaggio3,messaggio4),'Matching result','help');
flag=0;
msg='';
msg2='';
count=0;
nearst_five=zeros(5,2);
nearst_five=sort(hanadi);
for ii=1:5
disp(nearst_five(ii,1));
end
for y=1:5
for ii=1:(max_class-1)
if(nearst_five(y,1)==distanze_pesi(ii))
nearst_five(y,2)=ii;
end
end
end
for y=1:5
for ii=1:(max_class-1)
if(ii == nearst_five(y,2))
nearst_name(y)= namehanadi(ii);
end
end
end
disp('The nearest five class are:');
for ii=1:5
if(nearst_five(ii)~=0 && flag<=5)
%disp(hanadi(ii));
msg= strcat('the class is: ' ,num2str(nearst_five(ii,2)));
msg2= strcat(msg,strcat(' with distance:',num2str(nearst_five(ii))), 'Name :' ,nearst_name(ii));
disp(msg2);
flag= flag + 1;
end
end
% disp(strcat('the class is:' ,num2str(hanadi(ii,2))));
%disp(hanadi(ii,2));
%disp(posizione_minimo_pesi);
% disp('with a distance equal to ');
%disp(minimo_pesi);
% disp('The distance from Face Space is ');
% disp(distanza_spazio_facce);
else
warndlg('No image processing is possible. Database is empty.',' Warning ')
end
else
warndlg('Input image must be selected.',' Warning ')
end
and i need to display the top 5 images , i treid doing imread and then imshow but it doesn't seam to work
can any one help
thanx in advance
.
- Follow-Ups:
- Re: displaying images
- From: ImageAnalyst
- Re: displaying images
- Prev by Date: Re: build up 3-D Gaussian Gaussian Kernel
- Next by Date: Re: matrix columns rearangement
- Previous by thread: solving equation using lambertW
- Next by thread: Re: displaying images
- Index(es):
Relevant Pages
|
Loading