TP-modelisation-images/TP_maillage.m

382 lines
9.8 KiB
Mathematica
Raw Normal View History

2022-03-15 09:45:22 +00:00
clear;
close all;
2022-04-05 18:15:48 +00:00
2022-03-15 09:45:22 +00:00
nb_images = 36; % Nombre d'images
% chargement des images
for i = 1:nb_images
2022-03-22 07:13:49 +00:00
if i <= 10
nom = sprintf('images/viff.00%d.ppm', i - 1);
2022-03-15 09:45:22 +00:00
else
2022-03-22 07:13:49 +00:00
nom = sprintf('images/viff.0%d.ppm', i - 1);
2022-03-15 11:06:17 +00:00
end
2022-03-22 07:13:49 +00:00
2022-03-15 09:45:22 +00:00
% L'ensemble des images de taille : nb_lignes x nb_colonnes x nb_canaux
% x nb_images
2022-03-22 07:13:49 +00:00
im(:, :, :, i) = imread(nom);
2022-03-15 11:06:17 +00:00
end
2022-03-22 07:13:49 +00:00
2022-03-15 09:45:22 +00:00
% chargement des points 2D suivis
% pts de taille nb_points x (2 x nb_images)
2022-03-22 07:13:49 +00:00
% sur chaque ligne de pts
2022-03-15 09:45:22 +00:00
% tous les appariements possibles pour un point 3D donne
% on affiche les coordonnees (xi,yi) de Pi dans les colonnes 2i-1 et 2i
% tout le reste vaut -1
pts = load('viff.xy');
2022-03-15 09:45:22 +00:00
% Chargement des matrices de projection
2022-03-22 07:13:49 +00:00
% Chaque P{i} contient la matrice de projection associee a l'image i
2022-03-15 09:45:22 +00:00
% RAPPEL : P{i} est de taille 3 x 4
load dino_Ps;
2022-03-15 09:45:22 +00:00
% chargement des masques (pour l'elimination des fonds bleus)
% de taille nb_lignes x nb_colonnes x nb_images
% A COMPLETER quand vous aurez termine la premiere partie permettant de
% binariser les images
load("mask.mat");
% cleaning des masks
im_mask = im_mask(:, :, :) == 0;
im_mask(1:8, :, :) = 0;
im_mask(end - 5:end, :, :) = 0;
im_mask(:, 1:5, :) = 0;
im_mask(:, end - 30:end, :) = 0;
2022-03-15 09:45:22 +00:00
% Affichage des images
2022-03-15 11:06:17 +00:00
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% A COMPLETER %
% Calculs des superpixels %
% Conseil : afficher les germes + les régions %
% à chaque étape / à chaque itération %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2022-03-31 11:24:20 +00:00
2022-03-25 10:31:23 +00:00
K = 100;
2022-03-22 07:13:49 +00:00
m = 0.1;
n = 3;
seuil_E = 10;
2022-03-31 11:24:20 +00:00
q_max = 5;
2022-04-06 09:12:38 +00:00
ind_img = 1;
2022-04-06 17:47:56 +00:00
figure(1);
2022-03-31 11:24:20 +00:00
imshow(im(:, :, :, ind_img)); title("Image " + num2str(ind_img));
2022-03-22 07:13:49 +00:00
2022-04-06 17:47:56 +00:00
figure(2);
2022-03-31 11:24:20 +00:00
imshow(im(:, :, :, ind_img)); title("Image " + num2str(ind_img));
2022-03-22 07:13:49 +00:00
hold on;
[germes, image_labelise, E] = super_pixel(im(:, :, :, ind_img), K, m, n, seuil_E, q_max);
% subplot(2, 2, 2); imshow(im(:, :, :, 9)); title('Image 9');
% hold on;
% germes = super_pixel(im(:, :, :, 9), K, m, n, q_max);
%
% subplot(2, 2, 3); imshow(im(:, :, :, 17)); title('Image 17');
% hold on;
% germes = super_pixel(im(:, :, :, 17), K, m, n, q_max);
%
% subplot(2, 2, 4); imshow(im(:, :, :, 25)); title('Image 25');
% hold on;
% germes = super_pixel(im(:, :, :, 25), K, m, n, q_max);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% A COMPLETER %
% Binarisation de l'image à partir des superpixels %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2022-03-22 07:13:49 +00:00
2022-04-06 17:47:56 +00:00
figure(3);
2022-03-25 10:31:23 +00:00
R = germes(:, 3);
B = germes(:, 5);
scatter(R, B);
2022-03-25 10:31:23 +00:00
hold on
2022-03-22 07:13:49 +00:00
2022-03-25 10:31:23 +00:00
a = (0.7 - 0.1) / (0.6 - 0.2);
b = 0.1 - a * 0.2;
plot([0 1], [b a + b]);
W = a * R + b - B > 0;
germes = [germes W];
2022-03-15 11:06:17 +00:00
2022-03-25 10:31:23 +00:00
%%
2022-04-06 17:47:56 +00:00
figure(4);
2022-04-06 20:17:11 +00:00
tiledlayout(2, 2, 'Padding', 'none', 'TileSpacing', 'compact');
2022-04-06 17:47:56 +00:00
2022-03-25 10:31:23 +00:00
bw_img = reshape(germes(image_labelise, 6), size(im, 1), []);
2022-04-06 17:47:56 +00:00
nexttile;
imshow(bw_img);
2022-04-06 20:17:11 +00:00
CC = bwconncomp(bw_img, 4);
2022-04-06 17:47:56 +00:00
bw_img = zeros(size(bw_img));
bw_img(CC.PixelIdxList{1}) = 1;
nexttile;
imshow(bw_img);
bw_img = ~bw_img;
2022-04-06 20:17:11 +00:00
CC = bwconncomp(bw_img, 4);
2022-04-06 17:47:56 +00:00
bw_img = zeros(size(bw_img));
bw_img(CC.PixelIdxList{1}) = 1;
bw_img = ~bw_img;
nexttile;
imshow(bw_img);
2022-03-22 07:13:49 +00:00
2022-04-06 20:17:11 +00:00
bw_img = im_mask(:, :, ind_img);
2022-04-06 17:47:56 +00:00
nexttile;
imshow(bw_img);
figure(5);
2022-04-06 20:17:11 +00:00
tiledlayout(2, 2, 'Padding', 'none', 'TileSpacing', 'compact');
2022-04-06 17:47:56 +00:00
nexttile;
2022-03-25 10:31:23 +00:00
imshow(bw_img);
2022-03-31 12:53:56 +00:00
hold on
2022-03-25 10:31:23 +00:00
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% A FAIRE SI VOUS UTILISEZ LES MASQUES BINAIRES FOURNIS %
% Chargement des masques binaires %
% de taille nb_lignes x nb_colonnes x nb_images %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2022-03-25 10:31:23 +00:00
pixel_b = find(bw_img == 1);
[r, c] = ind2sub(size(bw_img), pixel_b(1));
contour = bwtraceboundary(bw_img, [r c], 'W', 8);
2022-04-06 20:17:11 +00:00
plot(contour(:, 2), contour(:, 1), 'g', 'LineWidth', 3);
2022-03-25 10:31:23 +00:00
% r = delaunay(contour);
% barycentres = (contour(r(:,1),:) + contour(r(:,2),:) + contour(r(:,3),:)) / 3;
% scatter(barycentres(:,2), barycentres(:,1));
% % triplot(r, contour(:,2), contour(:,1));
2022-04-06 17:47:56 +00:00
T = 1;
2022-04-06 20:17:11 +00:00
[vx, vy] = voronoi(contour(1:T:end, 1), contour(1:T:end, 2));
2022-03-25 10:31:23 +00:00
2022-03-31 12:53:56 +00:00
plot(vy, vx, 'b');
% Selection des segments qui ont leurs extrémités dans l'image
2022-04-06 20:17:11 +00:00
ok = vx(1, :) > 1 & vx(1, :) < size(bw_img, 1) & ...
vx(2, :) > 1 & vx(2, :) < size(bw_img, 1) & ...
vy(1, :) > 1 & vy(1, :) < size(bw_img, 2) & ...
vy(2, :) > 1 & vy(2, :) < size(bw_img, 2);
vx = floor(vx(:, ok));
vy = floor(vy(:, ok));
2022-03-25 10:31:23 +00:00
2022-04-06 17:47:56 +00:00
% subplot(2,2,2);
nexttile;
2022-03-31 12:53:56 +00:00
imshow(bw_img);
hold on
plot(vy, vx, 'b');
% Selection des segments avec les extremités dans la forme
2022-03-25 10:31:23 +00:00
2022-04-06 20:17:11 +00:00
ind1 = sub2ind(size(bw_img), vx(1, :), vy(1, :));
2022-03-25 10:31:23 +00:00
ok1 = bw_img(ind1) > 0;
2022-04-06 20:17:11 +00:00
ind2 = sub2ind(size(bw_img), vx(2, :), vy(2, :));
2022-03-25 10:31:23 +00:00
ok2 = bw_img(ind2) > 0;
ok = ok1 & ok2;
2022-04-06 20:17:11 +00:00
vx = vx(:, ok);
vy = vy(:, ok);
2022-03-25 10:31:23 +00:00
2022-04-06 17:47:56 +00:00
% subplot(2,2,3);
nexttile;
2022-03-31 12:53:56 +00:00
imshow(bw_img);
hold on
plot(vy, vx, 'b');
% Remise en forme de vx et vy
2022-03-31 11:24:20 +00:00
vx_ = vx';
2022-04-06 20:17:11 +00:00
vx_ = [vx_(:, 1); vx_(:, 2)];
2022-03-31 11:24:20 +00:00
vy_ = vy';
2022-04-06 20:17:11 +00:00
vy_ = [vy_(:, 1); vy_(:, 2)];
2022-03-31 11:24:20 +00:00
V_ = [vx_ vy_];
% Calcule des rayons
contour_ = contour';
2022-04-06 20:17:11 +00:00
R = complex(V_(:, 1), V_(:, 2)) - complex(contour_(1, :), contour_(2, :));
2022-03-31 11:24:20 +00:00
R = abs(R);
R = min(R, [], 2);
R = R';
2022-04-06 20:17:11 +00:00
vx_vy = [vy(1, :) vy(2, :); vx(1, :) vx(2, :)]';
viscircles(vx_vy(1:1:end, :), R(1:1:end));
2022-04-06 17:47:56 +00:00
2022-03-31 11:24:20 +00:00
% Filtrage naif
% truc = find(R < 20);
% truc = mod(truc - 1, length(vx)) + 1;
2022-04-06 20:17:11 +00:00
%
2022-03-31 11:24:20 +00:00
% vx(:,truc) = [];
% vy(:,truc) = [];
2022-03-31 12:53:56 +00:00
% Filtrage scalaire
2022-04-06 17:47:56 +00:00
R_scaled = 1.05 * R;
2022-04-06 20:17:11 +00:00
dist = abs(complex(V_(:, 1), V_(:, 2)) - transpose(complex(V_(:, 1), V_(:, 2))));
2022-03-31 11:24:20 +00:00
2022-04-06 20:17:11 +00:00
R_vertical = ones(length(R_scaled), 1) * R_scaled;
R_horizontal = R_scaled' * ones(1, length(R_scaled));
2022-03-31 11:24:20 +00:00
2022-03-31 12:53:56 +00:00
[~, c] = ind2sub(size(dist), find(dist + R_vertical < R_horizontal));
2022-03-31 11:24:20 +00:00
2022-03-31 12:53:56 +00:00
vx(:, mod(c - 1, length(vx)) + 1) = [];
vy(:, mod(c - 1, length(vy)) + 1) = [];
2022-04-06 20:17:11 +00:00
R = [R(1:length(R) / 2); R(length(R) / 2 + 1:end)];
2022-04-06 17:47:56 +00:00
R(:, mod(c - 1, length(R)) + 1) = [];
2022-04-06 20:17:11 +00:00
R = [R(1, :) R(2, :)];
2022-04-06 17:47:56 +00:00
% subplot(2,2,4);
nexttile;
2022-03-31 12:53:56 +00:00
imshow(bw_img);
hold on
2022-03-31 11:24:20 +00:00
plot(vy, vx, 'b');
2022-04-06 20:17:11 +00:00
vx_vy = [vy(1, :) vy(2, :); vx(1, :) vx(2, :)]';
viscircles(vx_vy(1:1:end, :), R(1:1:end));
2022-03-25 10:31:23 +00:00
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% A DECOMMENTER ET COMPLETER %
% quand vous aurez les images segmentées %
% Affichage des masques associes %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2022-03-15 09:45:22 +00:00
% Affichage des masques associes
% figure;
% subplot(2,2,1); ... ; title('Masque image 1');
% subplot(2,2,2); ... ; title('Masque image 9');
% subplot(2,2,3); ... ; title('Masque image 17');
% subplot(2,2,4); ... ; title('Masque image 25');
% Reconstruction des points 3D
X = []; % Contient les coordonnees des points en 3D
color = []; % Contient la couleur associee
% Pour chaque coupple de points apparies
2022-03-22 07:13:49 +00:00
for i = 1:size(pts, 1)
2022-03-15 09:45:22 +00:00
% Recuperation des ensembles de points apparies
2022-03-22 07:13:49 +00:00
l = find(pts(i, 1:2:end) ~= -1);
2022-03-15 09:45:22 +00:00
% Verification qu'il existe bien des points apparies dans cette image
2022-03-22 07:13:49 +00:00
if size(l, 2) > 1 & max(l) - min(l) > 1 & max(l) - min(l) < 36
2022-03-15 09:45:22 +00:00
A = [];
R = 0;
G = 0;
B = 0;
% Pour chaque point recupere, calcul des coordonnees en 3D
for j = l
2022-03-22 07:13:49 +00:00
A = [A; P{j}(1, :) - pts(i, (j - 1) * 2 + 1) * P{j}(3, :);
P{j}(2, :) - pts(i, (j - 1) * 2 + 2) * P{j}(3, :)];
R = R + double(im(int16(pts(i, (j - 1) * 2 + 1)), int16(pts(i, (j - 1) * 2 + 2)), 1, j));
G = G + double(im(int16(pts(i, (j - 1) * 2 + 1)), int16(pts(i, (j - 1) * 2 + 2)), 2, j));
B = B + double(im(int16(pts(i, (j - 1) * 2 + 1)), int16(pts(i, (j - 1) * 2 + 2)), 3, j));
2022-03-15 09:45:22 +00:00
end;
2022-03-22 07:13:49 +00:00
[U, S, V] = svd(A);
X = [X V(:, end) / V(end, end)];
color = [color [R / size(l, 2); G / size(l, 2); B / size(l, 2)]];
2022-03-15 09:45:22 +00:00
end;
2022-03-22 07:13:49 +00:00
2022-03-15 09:45:22 +00:00
end;
2022-03-22 07:13:49 +00:00
fprintf('Calcul des points 3D termine : %d points trouves. \n', size(X, 2));
2022-03-15 09:45:22 +00:00
%affichage du nuage de points 3D
2022-04-06 17:47:56 +00:00
figure(6);
2022-04-05 18:15:48 +00:00
hold on;
for i = 1:size(X, 2)
plot3(X(1, i), X(2, i), X(3, i), '.', 'col', color(:, i) / 255);
end;
axis equal;
2022-04-06 17:47:56 +00:00
view(80, -10);
2022-03-15 09:45:22 +00:00
% A COMPLETER
% Tetraedrisation de Delaunay
2022-04-06 20:17:11 +00:00
T = delaunayTriangulation(X(1, :)', X(2, :)', X(3, :)');
2022-03-15 09:45:22 +00:00
% A DECOMMENTER POUR AFFICHER LE MAILLAGE
% fprintf('Tetraedrisation terminee : %d tetraedres trouves. \n',size(T,1));
% Affichage de la tetraedrisation de Delaunay
% figure;
% tetramesh(T);
% A DECOMMENTER ET A COMPLETER
% Calcul des barycentres de chacun des tetraedres
2022-04-05 18:15:48 +00:00
poids = [1 1 1 1] / 4;
nb_barycentres = size(T.ConnectivityList, 1);
2022-04-06 20:17:11 +00:00
for i = 1:size(T, 1)
2022-04-05 18:15:48 +00:00
% Calcul des barycentres differents en fonction des poids differents
% En commencant par le barycentre avec poids uniformes
C_g(1:3, i) = poids * T.Points(T.ConnectivityList(i, :), :);
C_g(4, i) = 1;
end
2022-03-15 09:45:22 +00:00
2022-03-22 07:13:49 +00:00
% A DECOMMENTER POUR VERIFICATION
2022-03-15 09:45:22 +00:00
% A RE-COMMENTER UNE FOIS LA VERIFICATION FAITE
% Visualisation pour vérifier le bon calcul des barycentres
% for i = 1:nb_images
% for k = 1:nb_barycentres
2022-04-05 18:15:48 +00:00
% o = P{i}*C_g(:,k);
2022-03-15 09:45:22 +00:00
% o = o./repmat(o(3,:),3,1);
% imshow(im_mask(:,:,i));
% hold on;
% plot(o(2,:),o(1,:),'rx');
% pause;
% close;
% end
2022-04-05 18:15:48 +00:00
% end
2022-03-15 09:45:22 +00:00
% A DECOMMENTER ET A COMPLETER
% Copie de la triangulation pour pouvoir supprimer des tetraedres
2022-04-05 18:15:48 +00:00
tri = T.ConnectivityList;
2022-03-22 07:13:49 +00:00
% Retrait des tetraedres dont au moins un des barycentres
2022-03-15 09:45:22 +00:00
% ne se trouvent pas dans au moins un des masques des images de travail
% Pour chaque barycentre
2022-04-05 18:15:48 +00:00
to_save = [];
2022-04-06 20:17:11 +00:00
2022-04-05 18:15:48 +00:00
for k = 1:nb_barycentres
valide = 0;
2022-04-06 20:17:11 +00:00
2022-04-05 18:15:48 +00:00
for i = 1:nb_images
2022-04-06 20:17:11 +00:00
o = P{i} * C_g(:, k);
2022-04-05 18:15:48 +00:00
o = o / o(3);
x = floor(o(1));
y = floor(o(2));
2022-04-06 20:17:11 +00:00
if im_mask(x, y, i) == 0
2022-04-05 18:15:48 +00:00
valide = 1;
break
end
2022-04-06 20:17:11 +00:00
2022-04-05 18:15:48 +00:00
end
if valide
continue
end
to_save = [to_save k];
end
2022-04-06 20:17:11 +00:00
triBis = tri(to_save, :);
2022-04-06 09:12:38 +00:00
nb_barycentres = length(to_save);
2022-03-15 09:45:22 +00:00
% A DECOMMENTER POUR AFFICHER LE MAILLAGE RESULTAT
% Affichage des tetraedres restants
2022-04-06 20:17:11 +00:00
fprintf('Retrait des tetraedres exterieurs a la forme 3D termine : %d tetraedres restants. \n', size(T, 1));
2022-04-06 17:47:56 +00:00
figure(7);
2022-04-05 18:15:48 +00:00
tetramesh(triBis, T.Points);
2022-04-06 09:49:55 +00:00
view(80, -10);
2022-03-15 09:45:22 +00:00
% Sauvegarde des donnees
2022-04-06 20:17:11 +00:00
save donnees;