TP-traitement-audio-visuel/TP6/exercice_1_bis.m

94 lines
2.5 KiB
Mathematica
Raw Permalink Normal View History

2023-06-22 18:47:16 +00:00
%--------------------------------------------------------------------------
% ENSEEIHT - 2SN MM - Traitement des donn<EFBFBD>es audio-visuelles
% TP6 - Restauration d'images
% exercice_1_bis : d<EFBFBD>bruitage avec variation totale (niveaux de gris)
%--------------------------------------------------------------------------
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/lena_avec_bruit.bmp'));
[nb_lignes,nb_colonnes,nb_canaux] = size(u0);
u_max = max(u0(:));
% 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)), sprintf('saves/exo1bis_bruit.png'))
% Affichage de l'image restaur<EFBFBD>e <EFBFBD> l'it<EFBFBD>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/exo1bis_clean.gif", 'gif', 'Loopcount', inf);
drawnow nocallbacks
% Vectorisation de u0 :
nb_pixels = nb_lignes * nb_colonnes;
u0 = reshape(u0,[], 1, 3);
% Param<EFBFBD>tre pour garantir la diff<EFBFBD>rentiabilit<EFBFBD> de la variation totale :
epsilon = 0.01;
% Op<EFBFBD>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<EFBFBD>me :
b = u0;
% Point fixe :
lambda = 10; % Poids de la r<EFBFBD>gularisation
u_k = u0;
convergence = +Inf;
iteration = 0;
while convergence > 1e-3
% Incr<EFBFBD>mentation du nombre d'it<EFBFBD>rations :
iteration = iteration + 1;
% It<EFBFBD>ration (6) :
u_kp1 = debruitage_bis(b,u_k,lambda,Dx,Dy,epsilon);
% Test de convergence :
convergence = norm(u_kp1(:)-u_k(:))/norm(u_k(:));
% Mise <EFBFBD> jour de l'image courante u_k :
u_k = u_kp1;
% Affichage de l'image restaur<EFBFBD>e <EFBFBD> chaque it<EFBFBD>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/exo1bis_clean.gif", 'gif', 'WriteMode', 'append');
drawnow nocallbacks
pause(0.2)
end