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

93 lines
2.5 KiB
Matlab
Executable file
Raw Permalink 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
% Lecture des defauts:
udi = imread('Images/catjam_masque.png');
% udr = reshape(ud, [], 1);
% Param<61>tre pour garantir la diffŽrentiabilitŽ 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éfauts dans l'image (le jaune)
ud = u0(:,:,1) >= 204 & u0(:,:,2) >= 204 & u0(:,:,3) <= 54;
% 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 = 10; % Poids de la rŽ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Ž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;
end
% Affichage de l'image restaurŽe ˆ chaque itŽ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