121 lines
3.7 KiB
Mathematica
121 lines
3.7 KiB
Mathematica
|
clear;
|
||
|
close all;
|
||
|
|
||
|
load eigenfaces;
|
||
|
|
||
|
% Tirage aleatoire d'une image de test :
|
||
|
% personne = randi(nb_personnes);
|
||
|
% posture = randi(nb_postures);
|
||
|
% si on veut tester/mettre au point, on fixe l'individu
|
||
|
personne = 1;
|
||
|
posture = 6;
|
||
|
|
||
|
ficF = strcat('./Data/', liste_personnes{personne}, liste_postures{posture}, '-300x400.gif');
|
||
|
img = imread(ficF);
|
||
|
img(ligne_min:ligne_max,colonne_min:colonne_max) = 0;
|
||
|
image_test = double(transpose(img(:)));
|
||
|
|
||
|
|
||
|
% Pourcentage d'information
|
||
|
per = 0.95;
|
||
|
|
||
|
% Nombre N de composantes principales a prendre en compte
|
||
|
% [dans un second temps, N peut etre calcule pour atteindre le pourcentage
|
||
|
% d'information avec N valeurs propres (contraste)] :
|
||
|
cumul = cumsum(lambda/sum(lambda));
|
||
|
N = find( cumul >= per, 1 );
|
||
|
%N = 2;
|
||
|
|
||
|
%%% plus proche voisin, k = 1
|
||
|
|
||
|
% Composantes principales des donnees d'apprentissage
|
||
|
C = X_centre_masque * W_masque;
|
||
|
|
||
|
% Composantes principales de l'image de test
|
||
|
C_test = (image_test - individu_moyen_masque ) * W_masque;
|
||
|
|
||
|
% on calcule les distances entre l'individu test et ceux des données
|
||
|
% d'apprentissage
|
||
|
distance = vecnorm(transpose(C(:,1:N) - C_test(:,1:N)));
|
||
|
|
||
|
% on trouve le plus proche voisin
|
||
|
[~, I] = min(distance);
|
||
|
|
||
|
% on trouve le plus proche à partir de I
|
||
|
personne_proche = floor( (I-1) / nb_postures_base ) + 1;
|
||
|
posture_proche = mod(I-1, nb_postures_base) + 1;
|
||
|
|
||
|
dx = 150;
|
||
|
dy = 150;
|
||
|
hold on;
|
||
|
for i = 1:nb_personnes_base
|
||
|
lignes = (i-1)*nb_postures_base+1:i*nb_postures_base;
|
||
|
plot(C(lignes,1), C(lignes,2), '*');
|
||
|
text(C(lignes,1) + dx, C(lignes,2) + dy, liste_personnes_base(i) + "p" + string([1 2 3 4]));
|
||
|
end
|
||
|
|
||
|
plot(C_test(1),C_test(2), '+', 'MarkerSize',10, 'LineWidth',2); % visage de test
|
||
|
line([C_test(1) C(I, 1)], [C_test(2) C(I, 2)]);
|
||
|
axis equal;
|
||
|
set(gca,'FontSize',20);
|
||
|
xlabel('$C_1$','FontSize',30,'Interpreter','Latex');
|
||
|
ylabel('$C_2$','FontSize',30,'Interpreter','Latex');
|
||
|
|
||
|
figure('Name','Image tiree aleatoirement','Position',[0.2*L,0.2*H,0.8*L,0.5*H]);
|
||
|
|
||
|
subplot(1, 3, 1);
|
||
|
% Affichage de l'image de test :
|
||
|
colormap gray;
|
||
|
imagesc(img);
|
||
|
title({'Individu de test' ; ['posture ' num2str(posture) ' de ', liste_personnes{personne}]}, 'FontSize', 20);
|
||
|
axis image;
|
||
|
|
||
|
|
||
|
ficF = strcat('./Data/', liste_personnes_base{personne_proche}, liste_postures{posture_proche}, '-300x400.gif');
|
||
|
img = imread(ficF);
|
||
|
img(ligne_min:ligne_max,colonne_min:colonne_max) = 0;
|
||
|
|
||
|
subplot(1, 3, 2);
|
||
|
imagesc(img);
|
||
|
title({'Individu le plus proche' ; ['posture ' num2str(posture_proche) ' de ', liste_personnes_base{personne_proche}]}, 'FontSize', 20);
|
||
|
axis image;
|
||
|
|
||
|
subplot(1, 3, 3);
|
||
|
img_demasq = reshape(image_test, nb_lignes, nb_colonnes);
|
||
|
ficF = strcat('./Data/', liste_personnes_base{personne_proche}, liste_postures{posture_proche}, '-300x400.gif');
|
||
|
img = imread(ficF);
|
||
|
img_demasq(ligne_min:ligne_max,colonne_min:colonne_max) = img(ligne_min:ligne_max,colonne_min:colonne_max);
|
||
|
imagesc(img_demasq);
|
||
|
title('Individu démasqué', 'FontSize', 20);
|
||
|
axis image;
|
||
|
|
||
|
%% évaluation
|
||
|
% on cherche les composantes principales de notre nouvelle image et la
|
||
|
% compare à ce que l'on cherche.
|
||
|
|
||
|
load eigenfaces_all_4posture;
|
||
|
|
||
|
figure;
|
||
|
|
||
|
image_demasq = double(transpose(img_demasq(:)));
|
||
|
|
||
|
C_demasq = (image_demasq - individu_moyen) * W;
|
||
|
C_nomasq = X_centre * W;
|
||
|
|
||
|
dx = 50;
|
||
|
dy = 50;
|
||
|
hold on;
|
||
|
for i = 1:nb_personnes_base
|
||
|
lignes = (i-1)*nb_postures_base+1:i*nb_postures_base;
|
||
|
plot(C_nomasq(lignes,1), C_nomasq(lignes,2), '*');
|
||
|
text(C_nomasq(lignes,1) + dx, C_nomasq(lignes,2) + dy, liste_personnes_base(i) + "p" + string([1 2 3 4]));
|
||
|
end
|
||
|
|
||
|
plot(C_demasq(1),C_demasq(2), 'x', 'MarkerSize',10, 'LineWidth',2); % visage de test
|
||
|
|
||
|
ligne = (personne-1)*nb_postures_base + posture;
|
||
|
line([C_demasq(1) C_nomasq(ligne, 1)], [C_demasq(2) C_nomasq(ligne, 2)]);
|
||
|
axis equal;
|
||
|
set(gca,'FontSize',20);
|
||
|
xlabel('$C_1$','FontSize',30,'Interpreter','Latex');
|
||
|
ylabel('$C_2$','FontSize',30,'Interpreter','Latex');
|