98 lines
2.6 KiB
Mathematica
98 lines
2.6 KiB
Mathematica
|
%--------------------------------------------------------------------------
|
|||
|
% ENSEEIHT - 2SN MM - Traitement des donn<EFBFBD>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/grenouille_avec_texte.png'));
|
|||
|
[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/exo2bis_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/exo2bis_clean.gif", 'gif', 'Loopcount', inf);
|
|||
|
drawnow nocallbacks
|
|||
|
|
|||
|
% Vectorisation de u0 :
|
|||
|
nb_pixels = nb_lignes * nb_colonnes;
|
|||
|
u0 = reshape(u0,[], 1, 3);
|
|||
|
|
|||
|
% cr<EFBFBD>ation du vecteur defaut:
|
|||
|
% ud = reshape(u0,[], 3) == [255 255 0];
|
|||
|
% ud = reshape(ud,[], 1);
|
|||
|
ud = u0(:,:,1) >= 204 & u0(:,:,2) >= 204 & u0(:,:,3) <= 54;
|
|||
|
|
|||
|
% 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 = inpainting(b,u_k,ud,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/exo2bis_clean.gif", 'gif', 'WriteMode', 'append');
|
|||
|
|
|||
|
drawnow nocallbacks
|
|||
|
pause(0.2)
|
|||
|
|
|||
|
end
|