Salut, j'essaye d'applique le code de FisherFace à ma base de visage, mon problème c'est que le taux de reconnaissance obtenu est très faible 2%. Donc, je pense qu'il ya un erreur au niveau du code.
Vous trouvez ci dessus les fonctions que j'utilise:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 function T = CreateDatabase(TrainDatabasePath) % Align a set of face images (the training set T1, T2, ... , TM ) % % Description: This function reshapes all 2D images of the training database % into 1D column vectors. Then, it puts these 1D column vectors in a row to % construct 2D matrix 'T'. Each column of 'T' is a training image, which has been reshaped into a 1D vector. % Also, P is the total number of MxN training images and C is the number of % classes. % % % Argument: TrainDatabasePath - Path of the training database % % Returns: T - A 2D matrix, containing all 1D image vectors. % The length of 1D column vectors is MN and 'T' will be a MNxP 2D matrix. % % See also: STRCMP, STRCAT, RESHAPE % Original version by Amir Hossein Omidvarnia, October 2007 % Email: aomidvar@ece.ut.ac.ir %%%%%%%%%%%%%%%%%%%%%%%% File management TrainFiles = dir(TrainDatabasePath); Train_Number = 0; for i = 1:size(TrainFiles,1) if not(strcmp(TrainFiles(i).name,'.')|strcmp(TrainFiles(i).name,'..')|strcmp(TrainFiles(i).name,'Thumbs.db')) Train_Number = Train_Number + 1; % Number of all images in the training database end end %%%%%%%%%%%%%%%%%%%%%%%% Construction of 2D matrix from 1D image vectors T = []; for i = 1 : Train_Number % I have chosen the name of each image in databases as a corresponding % number. However, it is not mandatory! str = int2str(i); str = strcat('\',str,'.jpg'); str = strcat(TrainDatabasePath,str); img = imread(str); img = rgb2gray(img); [irow icol] = size(img); temp = reshape(img',irow*icol,1); % Reshaping 2D images into 1D image vectors T = [T temp]; % 'T' grows after each turn end T = double(T);
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95 function [m_database V_PCA V_Fisher ProjectedImages_Fisher] = FisherfaceCore(T) % Use Principle Component Analysis (PCA) and Fisher Linear Discriminant (FLD) to determine the most % discriminating features between images of faces. % % Description: This function gets a 2D matrix, containing all training image vectors % and returns 4 outputs which are extracted from training database. % Suppose Ti is a training image, which has been reshaped into a 1D vector. % Also, P is the total number of MxN training images and C is the number of % classes. At first, centered Ti is mapped onto a (P-C) linear subspace by V_PCA % transfer matrix: Zi = V_PCA * (Ti - m_database). % Then, Zi is converted to Yi by projecting onto a (C-1) linear subspace, so that % images of the same class (or person) move closer together and images of difference % classes move further apart: Yi = V_Fisher' * Zi = V_Fisher' * V_PCA' * (Ti - m_database) % % Argument: T - (M*NxP) A 2D matrix, containing all 1D image vectors. % All of 1D column vectors have the same length of M*N % and 'T' will be a MNxP 2D matrix. % % Returns: m_database - (M*Nx1) Mean of the training database % V_PCA - (M*Nx(P-C)) Eigen vectors of the covariance matrix of the % training database % V_Fisher - ((P-C)x(C-1)) Largest (C-1) eigen vectors of matrix J = inv(Sw) * Sb % ProjectedImages_Fisher - ((C-1)xP) Training images, which are projected onto Fisher linear space % % See also: EIG % Original version by Amir Hossein Omidvarnia, October 2007 % Email: aomidvar@ece.ut.ac.ir Class_number = 48%( size(T,2) )/4 % Number of classes (or persons) Class_population = 6%2; % Number of images in each class P = 288%Class_population * Class_number; % Total number of training images %%%%%%%%%%%%%%%%%%%%%%%% calculating the mean image m_database = mean(T,2); %%%%%%%%%%%%%%%%%%%%%%%% Calculating the deviation of each image from mean image A = T - repmat(m_database,1,P); %%%%%%%%%%%%%%%%%%%%%%%% Snapshot method of Eigenface algorithm L = A'*A; % L is the surrogate of covariance matrix C=A*A'. [V D] = eig(L); % Diagonal elements of D are the eigenvalues for both L=A'*A and C=A*A'. %%%%%%%%%%%%%%%%%%%%%%%% Sorting and eliminating small eigenvalues L_eig_vec = []; for i = 1 : P-Class_number L_eig_vec = [L_eig_vec V(:,i)]; end %%%%%%%%%%%%%%%%%%%%%%%% Calculating the eigenvectors of covariance matrix 'C' V_PCA = A * L_eig_vec; % A: centered image vectors %%%%%%%%%%%%%%%%%%%%%%%% Projecting centered image vectors onto eigenspace % Zi = V_PCA' * (Ti-m_database) ProjectedImages_PCA = []; for i = 1 : P temp = V_PCA'*A(:,i); ProjectedImages_PCA = [ProjectedImages_PCA temp]; end %%%%%%%%%%%%%%%%%%%%%%%% Calculating the mean of each class in eigenspace m_PCA = mean(ProjectedImages_PCA,2); % Total mean in eigenspace m = zeros(P-Class_number,Class_number); Sw = zeros(P-Class_number,P-Class_number); % Initialization os Within Scatter Matrix Sb = zeros(P-Class_number,P-Class_number); % Initialization of Between Scatter Matrix for i = 1 : Class_number m(:,i) = mean( ( ProjectedImages_PCA(:,((i-1)*Class_population+1):i*Class_population) ), 2 )'; S = zeros(P-Class_number,P-Class_number); for j = ( (i-1)*Class_population+1 ) : ( i*Class_population ) S = S + (ProjectedImages_PCA(:,j)-m(:,i))*(ProjectedImages_PCA(:,j)-m(:,i))'; end Sw = Sw + S; % Within Scatter Matrix Sb = Sb + (m(:,i)-m_PCA) * (m(:,i)-m_PCA)'; % Between Scatter Matrix end %%%%%%%%%%%%%%%%%%%%%%%% Calculating Fisher discriminant basis's % We want to maximise the Between Scatter Matrix, while minimising the % Within Scatter Matrix. Thus, a cost function J is defined, so that this condition is satisfied. [J_eig_vec, J_eig_val] = eig(Sb,Sw); % Cost function J = inv(Sw) * Sb J_eig_vec = fliplr(J_eig_vec); %%%%%%%%%%%%%%%%%%%%%%%% Eliminating zero eigens and sorting in descend order for i = 1 : Class_number-1 V_Fisher(:,i) = J_eig_vec(:,i); % Largest (C-1) eigen vectors of matrix J end %%%%%%%%%%%%%%%%%%%%%%%% Projecting images onto Fisher linear space % Yi = V_Fisher' * V_PCA' * (Ti - m_database) for i = 1 : Class_number*Class_population ProjectedImages_Fisher(:,i) = V_Fisher' * ProjectedImages_PCA(:,i); end
et voila la fonction que j'utilise pour faire l'appel de ces fonctions:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 function OutputName = Recognition(TestImage, m_database, V_PCA, V_Fisher, ProjectedImages_Fisher) % Recognizing step.... % % Description: This function compares two faces by projecting the images into facespace and % measuring the Euclidean distance between them. % % Argument: TestImage - Path of the input test image % % m_database - (M*Nx1) Mean of the training database % database, which is output of 'EigenfaceCore' function. % % V_PCA - (M*Nx(P-1)) Eigen vectors of the covariance matrix of % the training database % V_Fisher - ((P-1)x(C-1)) Largest (C-1) eigen vectors of matrix J = inv(Sw) * Sb % ProjectedImages_Fisher - ((C-1)xP) Training images, which % are projected onto Fisher linear space % % Returns: OutputName - Name of the recognized image in the training database. % % See also: RESHAPE, STRCAT % Original version by Amir Hossein Omidvarnia, October 2007 % Email: aomidvar@ece.ut.ac.ir Train_Number = size(ProjectedImages_Fisher,2) %%%%%%%%%%%%%%%%%%%%%%%% Extracting the FLD features from test image %InputImage = imread(TestImage); temp = TestImage(:,:,1); %tic; [irow icol] = size(temp); InImage = reshape(temp',irow*icol,1); Difference = double(InImage)-m_database; % Centered test image ProjectedTestImage = V_Fisher' * V_PCA' * Difference; % Test image feature vector %%%%%%%%%%%%%%%%%%%%%%%% Calculating Euclidean distances % Euclidean distances between the projected test image and the projection % of all centered training images are calculated. Test image is % supposed to have minimum distance with its corresponding image in the % training database. Euc_dist = []; for i = 1 : Train_Number q = ProjectedImages_Fisher(:,i); temp = ( norm( ProjectedTestImage - q ) )^2; Euc_dist = [Euc_dist temp]; end [Euc_dist_min , Recognized_index] = min(Euc_dist); %toc OutputName = Recognized_index; %OutputName = strcat(int2str(Recognized_index),'.bmp');
Pouvez-vous m'aidez à corriger ce code car je me sens que le résultat obtenu n'est pas logique??
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 clear all clc close all TrainDatabasePath = uigetdir(strcat(matlabroot,'\work'), 'Select training database path' ); TestDatabasePath = uigetdir(strcat(matlabroot,'\work'), 'Select test database path'); T = CreateDatabase(TrainDatabasePath); [m V_PCA V_Fisher ProjectedImages_Fisher] = FisherfaceCore(T); T=[]; ix1=[] froot = './RenommerImageTest'; list = dir(sprintf('%s\\*.jpg', froot)); frootProbe = './ImageTest'; listProbe = dir(sprintf('%s\\*.jpg', frootProbe)); frootGalerie = './ImageGalerie'; listGalerie = dir(sprintf('%s\\*.jpg', frootGalerie)); for index = 1:length(list) chemain_image=sprintf('%s\\%s', froot, list(index).name); TestImage = imread(chemain_image); OutputName = Recognition(TestImage, m, V_PCA, V_Fisher, ProjectedImages_Fisher); SortieReconnaissance=OutputName chemain_imageProbe=sprintf('%s\\%s', frootProbe, listProbe(index).name); ProbeImage = imread(chemain_imageProbe); NomProbeImage= listProbe(index).name fg1=[]; for ml=1:4 fg1=[fg1 NomProbeImage(ml)]; end fg1=fg1 chemain_imageGaleie=sprintf('%s\\%s', frootGalerie, listGalerie(OutputName).name); GalerieImage = imread(chemain_imageGaleie); NomGalerieImage=listGalerie(OutputName).name fp1=[]; for ml=1:4 fp1=[fp1 NomGalerieImage(ml)]; end fp1=fp1 if(strcmp(fp1,fg1)) c=[1 0] else c=[0 1] end; T=[T;c]; end Taux_False_Classification= False_Erreur_Rate(T) Taux_recognitionGauche=1-Taux_False_Classification
Partager