93 lines
2.5 KiB
Mathematica
93 lines
2.5 KiB
Mathematica
|
%--------------------------------------------------------------------------
|
|||
|
% ENSEEIHT - 2SN MM - Traitement des donn<EFBFBD>es audio-visuelles
|
|||
|
% TP6 - Restauration d'images
|
|||
|
% exercice_2
|
|||
|
%--------------------------------------------------------------------------
|
|||
|
|
|||
|
clear
|
|||
|
close all
|
|||
|
clc
|
|||
|
|
|||
|
% Lecture des defauts:
|
|||
|
udi = imread('Images/catjam_masque.png');
|
|||
|
% udr = reshape(ud, [], 1);
|
|||
|
|
|||
|
% Param<EFBFBD>tre pour garantir la diff<EFBFBD>rentiabilit<EFBFBD> de la variation totale :
|
|||
|
epsilon = 0.01;
|
|||
|
|
|||
|
u0 = imread('Images/catjam/frame-0.jpg');
|
|||
|
u_max = max(u0(:));
|
|||
|
u0 = max(0,min(1,u0/u_max));
|
|||
|
[fimage,map] = rgb2ind(u0, 256);
|
|||
|
% imwrite(fimage, map, "saves/exo2ter_clean.gif", 'gif', 'Loopcount', inf);
|
|||
|
|
|||
|
for i=0:156
|
|||
|
|
|||
|
% lecture d'une frame
|
|||
|
u0 = double(imread(sprintf('Images/catjam/frame-%d.jpg', i)));
|
|||
|
[nb_lignes,nb_colonnes,nb_canaux] = size(u0);
|
|||
|
u_max = max(u0(:));
|
|||
|
|
|||
|
% on applique le texte
|
|||
|
for c=1:3
|
|||
|
img = u0(:,:,c);
|
|||
|
if c == 3
|
|||
|
color = 0;
|
|||
|
else
|
|||
|
color = 255;
|
|||
|
end
|
|||
|
img(udi(:,:,1) == 255) = color;
|
|||
|
u0(:,:,c) = img;
|
|||
|
end
|
|||
|
|
|||
|
[nb_lignes,nb_colonnes,nb_canaux] = size(u0);
|
|||
|
nb_pixels = nb_lignes * nb_colonnes;
|
|||
|
u0 = reshape(u0,[], 1, nb_canaux);
|
|||
|
|
|||
|
% on cherche les d<EFBFBD>fauts dans l'image (le jaune)
|
|||
|
ud = u0(:,:,1) >= 204 & u0(:,:,2) >= 204 & u0(:,:,3) <= 54;
|
|||
|
|
|||
|
% 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;
|
|||
|
|
|||
|
image = max(0,min(1,reshape(u_k,[nb_lignes nb_colonnes nb_canaux])/u_max));
|
|||
|
[fimage,fmap] = rgb2ind(image, 256);
|
|||
|
imwrite(fimage, fmap, sprintf("saves/exo2catjam_bruit_%05d.jpg", i))
|
|||
|
|
|||
|
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;
|
|||
|
end
|
|||
|
|
|||
|
% Affichage de l'image restaur<EFBFBD>e <EFBFBD> chaque it<EFBFBD>ration :
|
|||
|
image = max(0,min(1,reshape(u_k,[nb_lignes nb_colonnes nb_canaux])/u_max));
|
|||
|
|
|||
|
% Write to the GIF File
|
|||
|
[fimage,fmap] = rgb2ind(image, 256);
|
|||
|
imwrite(fimage, fmap, sprintf("saves/exo2catjam_clean_%05d.jpg", i));
|
|||
|
|
|||
|
end
|