function [out]=robust_scale_filter(img_bruit,image_no_bruit,fonc_diff,nbre_iter,seuil_contour,dt)
% Peronna-Malick Anisotropic Diffusion
% I:input gray or color image
%% img_bruit:image contenant du bruit en niveau de gris ou
% couleur
% fonc_diff:(fonction de diffusion ou d'arrêt contour)
% ='lin':linear diffusion, g=1
% ='pm1':perona_malik, g=1/(1+(x/K)^2)
% ='pm2':perona-malik, g=exp(-(x/K)^2)
% ='tky':Tukey's biweight, g= / (1-(x/K).^2).^2 ,|x|<=K
% \ 0 ,otherwise
% method='pm1 ou pm2':Peronna et Malick, ut=div(g(|Du|)*Du)
% nbre_iter:nombre d'itération défaut= 100
% seuil_contour: paramètre seuil contour
[row,col,nchannel]=size(img_bruit);
i=1;
psnr_sup=1;
psnr_inf=0;
% val_mae=1;
%while val_mae>seuil_contour%
while round(psnr_sup,3)>round(psnr_inf,3)%for i=1:nbre_iter
disp('RSF')
i=i+1
%%
%calcul des valeurs du gradient dans les quatre directions (N,S,E,O)
% N
% O C E
% S
grad_N=[img_bruit(1,:,:);img_bruit(1:row-1,:,:)]-img_bruit; %N-C
grad_S=[img_bruit(2:row,:,:);img_bruit(row,:,:)]-img_bruit; %S-C
grad_E=[img_bruit(:,2:col,:) img_bruit(:,col,:)]-img_bruit; %E-C
grad_O=[img_bruit(:,1,:) img_bruit(:,1:col-1,:)]-img_bruit; %O-C
%Compute the coefficient (k,a) of normalization g()
if strcmp(fonc_diff,'lin')
k_N=1;
k_S=1;
k_E=1;
k_O=1;
elseif strcmp(fonc_diff,'pm1')
k_N=2;
k_S=2;
k_E=2;
k_O=2;
a=1;%amplitude normalization
elseif strcmp(fonc_diff,'pm2')
k_N=1.4826*mad(grad_N(:),1)*(2^0.5);
k_S=1.4826*mad(grad_S(:),1)*(2^0.5);
k_E=1.4826*mad(grad_E(:),1)*(2^0.5);
k_O=1.4826*mad(grad_O(:),1)*(2^0.5);
a=1/(2*exp(-0.5));
elseif strcmp(fonc_diff,'tky')
k_N=1.4826*mad(grad_N(:),1)*(5^0.5);
k_S=1.4826*mad(grad_S(:),1)*(5^0.5);
k_E=1.4826*mad(grad_E(:),1)*(5^0.5);
k_O=1.4826*mad(grad_O(:),1)*(5^0.5);
a=25/32;
end
%% calcul de la fonction de diffusion
if strcmp(fonc_diff,'lin')%isotropic diffusion (gaussian)
g_N=k_N;g_S=k_S;g_E=k_E;g_O=k_O;
elseif strcmp(fonc_diff,'pm1')%anisotropic diffusion(PM1)
g_N=1./sqrt(1+(grad_N/k_N).^2).*a;
g_S=1./sqrt(1+(grad_S/k_S).^2).*a;
g_E=1./sqrt(1+(grad_E/k_E).^2).*a;
g_O=1./sqrt(1+(grad_O/k_O).^2).*a;
elseif strcmp(fonc_diff,'pm2')%anisotropic diffusion (PM2)
g_N=1./(1+(grad_N/k_N).^2).*a;
g_S=1./(1+(grad_S/k_S).^2).*a;
g_E=1./(1+(grad_E/k_E).^2).*a;
g_O=1./(1+(grad_O/k_O).^2).*a;
elseif strcmp(fonc_diff,'tky')%anisotropic diffusion (Tukey)
g_N=zeros(row,col,nchannel);g_S=g_N;g_E=g_N;g_O=g_N;
indexn=find(abs(grad_N)<=k_N);
indexs=find(abs(grad_S)<=k_S);
indexe=find(abs(grad_E)<=k_E);
indexw=find(abs(grad_O)<=k_O);
g_N(indexn)=(1-(grad_N(indexn)/k_N).^2).^2*a;
g_S(indexs)=(1-(grad_S(indexs)/k_S).^2).^2*a;
g_E(indexe)=(1-(grad_E(indexe)/k_E).^2).^2*a;
g_O(indexw)=(1-(grad_O(indexw)/k_O).^2).^2*a;
end
%%
img_bruit_n1=img_bruit;
%équation de diffusion
img_bruit=img_bruit_n1+dt*((g_N.*grad_N+g_S.*grad_S+g_E.*grad_E+g_O.*grad_O)./(g_N+g_S+g_E+g_O));
TABSCISSE(i)=i;
TPSNR(i)=psnr(img_bruit,image_no_bruit);
TMAE(i) =mean2(abs(img_bruit-img_bruit_n1));
TSSIM(i)=ssim(img_bruit,image_no_bruit);
%val_mae=MAE(i)
psnr_sup=TPSNR(i)
psnr_inf=TPSNR(i-1)
end
out.NBR_ITER=max(TABSCISSE);
if TPSNR(out.NBR_ITER)>TPSNR(out.NBR_ITER-1)
out.PSNR=TPSNR(out.NBR_ITER);
out.SSIM=TSSIM(out.NBR_ITER);
out.NBR_ITER=out.NBR_ITER;
else
out.PSNR=TPSNR(out.NBR_ITER-1);
out.SSIM=TSSIM(out.NBR_ITER-1);
out.NBR_ITER=out.NBR_ITER;
end
out.img_filtre=img_bruit;
end