feat: binarisation pas degueu mais quand meme un peu ^^'

This commit is contained in:
Damien Guillotin 2022-03-22 09:53:17 +01:00
parent 4e149ed045
commit 678874bd75
2 changed files with 98 additions and 33 deletions

View file

@ -35,30 +35,68 @@ load dino_Ps;
% Affichage des images % Affichage des images
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% A COMPLETER %
% Calculs des superpixels %
% Conseil : afficher les germes + les régions %
% à chaque étape / à chaque itération %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
K = 50; K = 50;
m = 0.1; m = 0.1;
n = 3; n = 3;
q_max = 10; seuil_E = 10;
q_max = 20;
figure; figure;
subplot(2, 2, 1); imshow(im(:, :, :, 1)); title('Image 1'); % subplot(2, 2, 1);
imshow(im(:, :, :, 9)); title('Image 1');
figure;
imshow(im(:, :, :, 9)); title('Image 1');
hold on; hold on;
[germes, image_labelise, E] = super_pixel(im(:, :, :, 9), K, m, n, seuil_E, q_max);
germes = super_pixel(im(:, :, :, 1), K, m, n, 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);
subplot(2, 2, 2); imshow(im(:, :, :, 9)); title('Image 9'); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
hold on; % A COMPLETER %
% Binarisation de l'image à partir des superpixels %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
germes = super_pixel(im(:, :, :, 9), K, m, n, q_max); figure;
subplot(2, 2, 3); imshow(im(:, :, :, 17)); title('Image 17'); R = germes(:,3);
hold on; B = germes(:,5);
scatter(R, B);
germes = super_pixel(im(:, :, :, 17), K, m, n, q_max); W = germes(:,3) > 0.5;
subplot(2, 2, 4); imshow(im(:, :, :, 25)); title('Image 25'); germes = [germes W];
hold on;
germes = super_pixel(im(:, :, :, 25), K, m, n, q_max); figure;
imagesc(reshape(germes(image_labelise, 6), size(im,1), []));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% A FAIRE SI VOUS UTILISEZ LES MASQUES BINAIRES FOURNIS %
% Chargement des masques binaires %
% de taille nb_lignes x nb_colonnes x nb_images %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ...
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% A DECOMMENTER ET COMPLETER %
% quand vous aurez les images segmentées %
% Affichage des masques associes %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Affichage des masques associes % Affichage des masques associes
% figure; % figure;
@ -99,14 +137,14 @@ end;
fprintf('Calcul des points 3D termine : %d points trouves. \n', size(X, 2)); fprintf('Calcul des points 3D termine : %d points trouves. \n', size(X, 2));
%affichage du nuage de points 3D %affichage du nuage de points 3D
figure; % figure;
hold on; % hold on;
%
for i = 1:size(X, 2) % for i = 1:size(X, 2)
plot3(X(1, i), X(2, i), X(3, i), '.', 'col', color(:, i) / 255); % plot3(X(1, i), X(2, i), X(3, i), '.', 'col', color(:, i) / 255);
end; % end;
%
axis equal; % axis equal;
% A COMPLETER % A COMPLETER
% Tetraedrisation de Delaunay % Tetraedrisation de Delaunay

View file

@ -1,11 +1,11 @@
function germes = super_pixel(img, K, m, n, q_max) function [germes, img_labelise, E] = super_pixel(img, K, m, n, seuil_E, q_max)
img = im2double(img); img = im2double(img);
% img = img(1:10, 1:10, :);
width = size(img, 1); width = size(img, 1);
height = size(img, 2); height = size(img, 2);
N = width * height; N = width * height;
% création des germes
S = sqrt(N / K); S = sqrt(N / K);
X = S / 2:S:width; X = S / 2:S:width;
Y = S / 2:S:height; Y = S / 2:S:height;
@ -21,10 +21,13 @@ function germes = super_pixel(img, K, m, n, q_max)
end end
% affichage des germes
scatter(germes(:, 2), germes(:, 1), 'g.'); scatter(germes(:, 2), germes(:, 1), 'g.');
drawnow nocallbacks
c = 1; c = 1;
% intialisation des germes (petit déplacement, pour ne pas se trouver sur un contour)
for germe = germes' for germe = germes'
x = germe(1); x = germe(1);
@ -48,10 +51,17 @@ function germes = super_pixel(img, K, m, n, q_max)
c = c + 1; c = c + 1;
end end
% affichage des germes
scatter(germes(:, 2), germes(:, 1), 'r.'); scatter(germes(:, 2), germes(:, 1), 'r.');
drawnow nocallbacks
germe_per_pixel = zeros(width, height); % création de l'image labelisée par les germes
img_labelise = zeros(width, height);
% mélange mélange mélange
germes = germes(randperm(length(germes)),:);
% boucle princiaple
for q = 1:q_max for q = 1:q_max
for x = 1:width for x = 1:width
@ -62,7 +72,7 @@ function germes = super_pixel(img, K, m, n, q_max)
min_Ds = 1e5; min_Ds = 1e5;
for index_germe = 1:length(germes) for index_germe = 1:length(germes)
germe = germes(index_germe,:); germe = germes(index_germe, :);
% on récupère les coordonnées du germe % on récupère les coordonnées du germe
xg = germe(1); xg = germe(1);
@ -94,7 +104,7 @@ function germes = super_pixel(img, K, m, n, q_max)
% Si Ds est meilleur on le prend % Si Ds est meilleur on le prend
if Ds < min_Ds if Ds < min_Ds
germe_per_pixel(x, y) = index_germe; img_labelise(x, y) = index_germe;
min_Ds = Ds; min_Ds = Ds;
end end
@ -104,26 +114,43 @@ function germes = super_pixel(img, K, m, n, q_max)
end end
image(germe_per_pixel); % affichage des labels
colormap lines; image(img_labelise * 255 / length(germes));
colormap hsv;
drawnow nocallbacks
old_germes = germes;
% kmeans, déplacement des germes par rapport à leurs pixels associés
for index_germe = 1:length(germes) for index_germe = 1:length(germes)
truc = germe_per_pixel == index_germe; truc = img_labelise == index_germe;
truc_rgb = reshape(img(repmat(truc,[1 1 3])), [], 3); truc_rgb = reshape(img(repmat(truc, [1 1 3])), [], 3);
m_rgb = mean(truc_rgb); m_rgb = mean(truc_rgb);
mx = 0; mx = 0;
my = 0; my = 0;
for i = find(truc == 1)' for i = find(truc == 1)'
[x, y] = ind2sub(size(img), i); [x, y] = ind2sub(size(img), i);
mx = mx + x; mx = mx + x;
my = my + y; my = my + y;
end end
mx = mx / sum(truc, 'all'); mx = mx / sum(truc, 'all');
my = my / sum(truc, 'all'); my = my / sum(truc, 'all');
germes(index_germe,:) = [mx my m_rgb]; germes(index_germe, :) = [mx my m_rgb];
end end
% affichage des germes
scatter(germes(:, 2), germes(:, 1), 'b.'); scatter(germes(:, 2), germes(:, 1), 'b.');
drawnow nocallbacks
% calcul de l'erreur résiduel
E = norm(old_germes(:, 1:2) - germes(:, 1:2));
% si notre erreur est inférieur au seuil, on stop l'algo
if E < seuil_E
break
end
end end