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

97 lines
2.5 KiB
Matlab
Executable file
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

%--------------------------------------------------------------------------
% ENSEEIHT - 2SN MM - Traitement des donnŽes audio-visuelles
% TP6 - Restauration d'images
% exercice_2
%--------------------------------------------------------------------------
clear
close all
clc
% Mise en place de la figure pour affichage :
taille_ecran = get(0,'ScreenSize');
L = taille_ecran(3);
H = taille_ecran(4);
figure('Name','Debruitage par variation totale',...
'Position',[0.06*L,0.1*H,0.9*L,0.7*H]);
% Lecture de l'image :
u0 = double(imread('Images/randonneur.jpg'));
[nb_lignes,nb_colonnes,nb_canaux] = size(u0);
u_max = max(u0(:));
% Lecture des defauts:
ud = imread('Images/masque_randonneur.png');
ud = reshape(ud, [], 1);
% Affichage de l'image :
subplot(1,2,1)
imagesc(max(0,min(1,u0/u_max)),[0 1])
colormap gray
axis image off
title('Image bruitee','FontSize',20)
imwrite(max(0,min(1,u0/u_max)), 'saves/exo2ter_bruit.png')
% Affichage de l'image restaurŽe ˆ l'itŽration 0 :
subplot(1,2,2)
imagesc(max(0,min(1,u0/u_max)),[0 1])
axis image off
title('Image restauree (iteration 0)','FontSize',20)
[fimage,map] = rgb2ind(max(0,min(1,u0/u_max)), 256);
imwrite(fimage, map, "saves/exo2ter_clean.gif", 'gif', 'Loopcount', inf);
drawnow nocallbacks
% Vectorisation de u0 :
nb_pixels = nb_lignes * nb_colonnes;
u0 = reshape(u0,[], 1, 3);
% Param<61>tre pour garantir la diffŽrentiabilitŽ de la variation totale :
epsilon = 0.01;
% OpŽrateur gradient :
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;
% Second membre b du syst<73>me :
b = u0;
% Point fixe :
lambda = 1; % Poids de la rŽgularisation
u_k = u0;
convergence = +Inf;
iteration = 0;
while convergence > 1e-3
% IncrŽmentation du nombre d'itŽrations :
iteration = iteration + 1;
% ItŽration (6) :
u_kp1 = inpainting(b,u_k,ud,lambda,Dx,Dy,epsilon);
% Test de convergence :
convergence = norm(u_kp1(:)-u_k(:))/norm(u_k(:));
% Mise ˆ jour de l'image courante u_k :
u_k = u_kp1;
% Affichage de l'image restaurŽe ˆ chaque itŽration :
subplot(1,2,2)
image = max(0,min(1,reshape(u_k,[nb_lignes nb_colonnes nb_canaux])/u_max));
imagesc(image,[0 1])
colormap gray
axis image off
title(['Image restauree (iteration ' num2str(iteration) ')'],'FontSize',20)
% Write to the GIF File
[fimage,map] = rgb2ind(image, 256);
imwrite(fimage, map, "saves/exo2ter_clean.gif", 'gif', 'WriteMode', 'append');
drawnow nocallbacks
pause(0.2)
end