% 1) bruit "sel et poivre" et filtre médian 

clear all, close all

I = imread('Noise_salt_and_pepper.png');
K = medfilt2(I);  % taille de voisinage par défaut: 3x3
figure, imshow(I), figure, imshow(K)

K = medfilt2(I,[21 21]); 
figure, imshow(I), figure, imshow(K)


% 2) bruit gaussien et lissage gaussien

clear all, close all
I=imread('house.tiff');
Inoise=imnoise(I,'gaussian',0,0.02); 
figure, imshow(Inoise); title('noisy image');
for sigma=1:2:10
    h=fspecial('gaussian',[6*sigma,6*sigma],sigma);
    J=imfilter(Inoise,h);
    figure, imshow(J), title(['Gaussian smoothing, sigma=',num2str(sigma)]);
end
figure, imshow(uint8(128+2*(Inoise-J)))

% wiener2
for sigma=2:6
    [J(:,:,1),Noise1]=wiener2(Inoise(:,:,1),[sigma,sigma]); 
    [J(:,:,2),Noise2]=wiener2(Inoise(:,:,2),[sigma,sigma]); 
    [J(:,:,3),Noise3]=wiener2(Inoise(:,:,3),[sigma,sigma]); 
    disp(['sigma = ',num2str(sigma),'  estimated noise = ',num2str(Noise1),' ',num2str(Noise2),' ',num2str(Noise3)]);
    figure, imshow(J), title(['Adaptive Wiener filtering, sigma=',num2str(sigma)]);
end
figure, imshow(uint8(128+2*(Inoise-J)))


% 3) Perona-Malik anisotropic diffusion

clear all, close all
I=imread('house.tiff');
Inoise=imnoise(I,'gaussian',0,0.02); 
figure, imshow(Inoise); title('noisy image');

kappa=50; % comme suggéré, essayez des valeurs entre 20 et 100
for i=1:3,
    J(:,:,i) = anisodiff(Inoise(:,:,i),20,kappa,0.1,1);
end;
figure, imshow(uint8(J));

figure, imshow(uint8(128+2*(double(Inoise)-J)))

% 4) TV denoising

clear all, close all
I=imread('house.tiff');
Inoise=imnoise(I,'gaussian',0,0.02); 
figure, imshow(Inoise); title('noisy image');

Niter=5000;
lambda=50;
J=zeros(size(Inoise));
J(:,:,1)=TVdenoising(double(Inoise(:,:,1)), Niter, lambda);
J(:,:,2)=TVdenoising(double(Inoise(:,:,2)), Niter, lambda);
J(:,:,3)=TVdenoising(double(Inoise(:,:,3)), Niter, lambda);
figure, imshow(uint8(J)), title(['TV denoising, lambda=',num2str(lambda)]);

figure, imshow(uint8(128+2*(double(Inoise)-J)))


