2022-03-22 08:53:17 +00:00
|
|
|
function [germes, img_labelise, E] = super_pixel(img, K, m, n, seuil_E, q_max)
|
2022-03-22 07:13:49 +00:00
|
|
|
img = im2double(img);
|
|
|
|
|
|
|
|
width = size(img, 1);
|
|
|
|
height = size(img, 2);
|
|
|
|
N = width * height;
|
|
|
|
|
2022-03-22 08:53:17 +00:00
|
|
|
% création des germes
|
2022-03-15 11:07:41 +00:00
|
|
|
S = sqrt(N / K);
|
2022-03-22 07:13:49 +00:00
|
|
|
X = S / 2:S:width;
|
|
|
|
Y = S / 2:S:height;
|
|
|
|
germes = zeros(length(X) * length(Y), 5);
|
2022-03-15 11:07:41 +00:00
|
|
|
c = 1;
|
2022-03-22 07:13:49 +00:00
|
|
|
|
2022-03-15 11:07:41 +00:00
|
|
|
for x = X
|
2022-03-22 07:13:49 +00:00
|
|
|
|
2022-03-15 11:07:41 +00:00
|
|
|
for y = Y
|
2022-03-22 07:13:49 +00:00
|
|
|
germes(c, :) = [x y 0 0 0];
|
2022-03-15 11:07:41 +00:00
|
|
|
c = c + 1;
|
|
|
|
end
|
2022-03-22 07:13:49 +00:00
|
|
|
|
2022-03-15 11:07:41 +00:00
|
|
|
end
|
2022-03-22 07:13:49 +00:00
|
|
|
|
2022-03-22 08:53:17 +00:00
|
|
|
% affichage des germes
|
2022-03-22 07:13:49 +00:00
|
|
|
scatter(germes(:, 2), germes(:, 1), 'g.');
|
2022-03-22 08:53:17 +00:00
|
|
|
drawnow nocallbacks
|
2022-03-22 07:13:49 +00:00
|
|
|
|
2022-03-15 11:07:41 +00:00
|
|
|
c = 1;
|
2022-03-22 07:13:49 +00:00
|
|
|
|
2022-03-22 08:53:17 +00:00
|
|
|
% intialisation des germes (petit déplacement, pour ne pas se trouver sur un contour)
|
2022-03-15 11:07:41 +00:00
|
|
|
for germe = germes'
|
2022-03-22 07:13:49 +00:00
|
|
|
|
2022-03-15 11:07:41 +00:00
|
|
|
x = germe(1);
|
|
|
|
y = germe(2);
|
2022-03-22 07:13:49 +00:00
|
|
|
|
|
|
|
xmin = floor(max(x - n, 1));
|
|
|
|
interval_x = xmin:min(x + n, width);
|
|
|
|
|
|
|
|
ymin = floor(max(y - n, 1));
|
|
|
|
interval_y = ymin:min(y + n, height);
|
2022-03-22 08:53:17 +00:00
|
|
|
|
2022-03-15 11:07:41 +00:00
|
|
|
Gmag = imgradient(img(interval_x, interval_y));
|
2022-03-22 07:13:49 +00:00
|
|
|
[~, ind_min] = min(Gmag(:));
|
2022-03-15 11:07:41 +00:00
|
|
|
[nr, nc] = ind2sub(size(Gmag), ind_min);
|
2022-03-22 07:13:49 +00:00
|
|
|
|
2022-03-15 11:07:41 +00:00
|
|
|
x = xmin + nr - 1;
|
|
|
|
y = ymin + nc - 1;
|
2022-03-22 07:13:49 +00:00
|
|
|
|
|
|
|
germes(c, :) = [x y img(x, y, 1) img(x, y, 2) img(x, y, 3)];
|
|
|
|
|
2022-03-15 11:07:41 +00:00
|
|
|
c = c + 1;
|
|
|
|
end
|
|
|
|
|
2022-03-22 08:53:17 +00:00
|
|
|
% affichage des germes
|
2022-03-22 07:13:49 +00:00
|
|
|
scatter(germes(:, 2), germes(:, 1), 'r.');
|
2022-03-22 08:53:17 +00:00
|
|
|
drawnow nocallbacks
|
|
|
|
|
|
|
|
% création de l'image labelisée par les germes
|
|
|
|
img_labelise = zeros(width, height);
|
2022-03-22 07:13:49 +00:00
|
|
|
|
2022-03-22 08:53:17 +00:00
|
|
|
% mélange mélange mélange
|
2022-03-25 10:31:23 +00:00
|
|
|
germes = germes(randperm(length(germes)), :);
|
2022-03-22 07:13:49 +00:00
|
|
|
|
2022-03-22 08:53:17 +00:00
|
|
|
% boucle princiaple
|
2022-03-22 07:13:49 +00:00
|
|
|
for q = 1:q_max
|
|
|
|
|
|
|
|
for x = 1:width
|
|
|
|
|
|
|
|
for y = 1:height
|
|
|
|
|
|
|
|
% initialisation des minimums
|
|
|
|
min_Ds = 1e5;
|
|
|
|
|
|
|
|
for index_germe = 1:length(germes)
|
2022-03-22 08:53:17 +00:00
|
|
|
germe = germes(index_germe, :);
|
2022-03-22 07:13:49 +00:00
|
|
|
|
|
|
|
% on récupère les coordonnées du germe
|
|
|
|
xg = germe(1);
|
|
|
|
yg = germe(2);
|
|
|
|
|
|
|
|
% on calcule la distance spatiale entre le germe et le pixel
|
|
|
|
Dxy = norm([x - xg y - yg]);
|
|
|
|
|
|
|
|
% si le germe est trop loin on skip
|
|
|
|
if Dxy > S
|
|
|
|
continue;
|
|
|
|
end
|
|
|
|
|
|
|
|
% on récupère les couleurs du germe
|
|
|
|
rg = germe(3);
|
|
|
|
gg = germe(4);
|
|
|
|
bg = germe(5);
|
|
|
|
|
|
|
|
% on récupère les couleurs du pixel
|
|
|
|
r = img(x, y, 1);
|
|
|
|
g = img(x, y, 2);
|
|
|
|
b = img(x, y, 3);
|
|
|
|
|
|
|
|
% on calcule la distance chromatique entre le germe et le pixel
|
|
|
|
Dlab = norm([r - rg g - gg b - bg]);
|
|
|
|
|
|
|
|
% on calcule Ds
|
|
|
|
Ds = Dlab + m / S * Dxy;
|
|
|
|
|
|
|
|
% Si Ds est meilleur on le prend
|
|
|
|
if Ds < min_Ds
|
2022-03-22 08:53:17 +00:00
|
|
|
img_labelise(x, y) = index_germe;
|
2022-03-22 07:13:49 +00:00
|
|
|
min_Ds = Ds;
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2022-03-22 08:53:17 +00:00
|
|
|
% affichage des labels
|
|
|
|
image(img_labelise * 255 / length(germes));
|
|
|
|
colormap hsv;
|
|
|
|
drawnow nocallbacks
|
|
|
|
|
|
|
|
old_germes = germes;
|
|
|
|
|
|
|
|
% kmeans, déplacement des germes par rapport à leurs pixels associés
|
2022-03-22 07:13:49 +00:00
|
|
|
for index_germe = 1:length(germes)
|
2022-03-22 08:53:17 +00:00
|
|
|
truc = img_labelise == index_germe;
|
|
|
|
truc_rgb = reshape(img(repmat(truc, [1 1 3])), [], 3);
|
2022-03-22 07:13:49 +00:00
|
|
|
m_rgb = mean(truc_rgb);
|
|
|
|
mx = 0;
|
|
|
|
my = 0;
|
2022-03-22 08:53:17 +00:00
|
|
|
|
2022-03-22 07:13:49 +00:00
|
|
|
for i = find(truc == 1)'
|
|
|
|
[x, y] = ind2sub(size(img), i);
|
|
|
|
mx = mx + x;
|
|
|
|
my = my + y;
|
|
|
|
end
|
2022-03-22 08:53:17 +00:00
|
|
|
|
2022-03-22 07:13:49 +00:00
|
|
|
mx = mx / sum(truc, 'all');
|
|
|
|
my = my / sum(truc, 'all');
|
2022-03-22 08:53:17 +00:00
|
|
|
germes(index_germe, :) = [mx my m_rgb];
|
2022-03-22 07:13:49 +00:00
|
|
|
end
|
|
|
|
|
2022-03-22 08:53:17 +00:00
|
|
|
% affichage des germes
|
2022-03-25 10:31:23 +00:00
|
|
|
scatter(germes(:, 2), germes(:, 1), 'w.');
|
2022-03-22 08:53:17 +00:00
|
|
|
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
|
2022-03-22 07:13:49 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|