TP-traitement-audio-visuel/TP7/collage.m
2023-06-22 20:47:16 +02:00

52 lines
1.2 KiB
Matlab

function u = collage(r, s, interieur)
r = double(r);
s = double(s);
[nb_lignes,nb_colonnes,nb_canaux] = size(r);
nb_pixels = nb_lignes * nb_colonnes;
e = ones(nb_pixels, 1);
Dx = spdiags([-e e],[0 nb_lignes], nb_pixels, nb_pixels);
Dx(end-nb_lignes+1:end,:) = 0;
Dy = spdiags([-e e],[0 1],nb_pixels, nb_pixels);
Dy(nb_lignes:nb_lignes:end,:) = 0;
X = zeros(size(r(:,:,1)));
X(1, :) = 1;
X(nb_lignes, :) = 1;
X(:, 1) = 1;
X(:, nb_colonnes) = 1;
indices_bord_r = find(X);
n_bord_r = length(indices_bord_r);
n_r = nb_pixels;
A = Dx' * Dx + Dy' * Dy;
A(indices_bord_r,:) = sparse(1:n_bord_r,indices_bord_r,ones(n_bord_r,1),n_bord_r,n_r);
% Calcul de l'imagette résultat u, canal par canal :
u = r;
for k = 1:nb_canaux
u_k = u(:,:,k);
s_k = s(:,:,k);
r_k = r(:,:,k);
grad_s = [Dx * s_k(:) , Dy * s_k(:)];
grad_r = [Dx * u_k(:) , Dy * u_k(:)];
grad_g = grad_r;
grad_g(interieur, :) = grad_s(interieur, :);
b = Dx' * grad_g(:,1) + Dy' * grad_g(:,2);
b(indices_bord_r) = r_k(indices_bord_r);
u_k = -A \ -b;
u_k = reshape(u_k, nb_lignes, nb_colonnes);
u(:,:,k) = u_k;
end