%% RUMORE
clear all
close all
img = rgb2gray(imread('lena.jpg'));

figure;
subplot(1,3,1)
imshow(img,[])
title('original','FontSize',14,'fontweight','bold')

img_noise = imnoise(img,'gaussian',0, 0.01);

subplot(1,3,2)
imshow(img_noise,[])
title('gaussian noise','FontSize',14,'fontweight','bold')

img_noise = imnoise(img,'salt & pepper',0.05);

subplot(1,3,3)
imshow(img_noise,[])
title('S&P noise','FontSize',14,'fontweight','bold')

%% RUMORE
clear all
close all
img = rgb2gray(imread('lena.jpg'));

figure;
subplot(2,2,1)
imshow(img,[])
title('original','FontSize',14,'fontweight','bold')

img_noise = imnoise(img,'gaussian',0, 0.01);

subplot(2,2,2)
imshow(img_noise,[])
title('noise','FontSize',14,'fontweight','bold')

fw = 5;



den_mean = mean_filter(img_noise,fw);

subplot(2,2,3)
imshow(den_mean,[])
title('mean filter','FontSize',14,'fontweight','bold')

h = fspecial('gaussian',fw,2.0);
den_gaus = imfilter(img_noise,h);
subplot(2,2,4)
imshow(den_gaus,[])
title('gaus filter','FontSize',14,'fontweight','bold')


error_noise = img_difference(img,img_noise,fw)
error_mean = img_difference(img,den_mean,fw)
error_gaus = img_difference(img,den_gaus,fw)

%% filtro mediano
clear all
close all
img = rgb2gray(imread('lena.jpg'));

figure;
subplot(2,2,1)
imshow(img,[])
title('original','FontSize',14,'fontweight','bold')
img_noise = imnoise(img,'salt & pepper',0.05);
fw = 5;
den_mean = mean_filter(img_noise,fw);

subplot(2,2,2)
imshow(den_mean,[])
title('mean filter','FontSize',14,'fontweight','bold')

h = fspecial('gaussian',fw,2.0);
den_gaus = imfilter(img_noise,h);
subplot(2,2,3)
imshow(den_gaus,[])
title('gaus filter','FontSize',14,'fontweight','bold')


den_median = median_filter(img_noise,fw);
subplot(2,2,4)
imshow(den_median,[])
title('median filter','FontSize',14,'fontweight','bold')


error_noise = img_difference(img,img_noise,fw)
error_mean = img_difference(img,den_mean,fw)
error_gaus = img_difference(img,den_gaus,fw)
error_median = img_difference(img,den_median,fw)


%% Segmentazione
clear all
close all
img =imread('coronal.png');
img = imresize(img,2);

t_o = iterative_tresholding(img,1);


figure;
subplot(1,2,1)
imshow(img)
title('original','FontSize',14,'fontweight','bold')
subplot(1,2,2)
imshow(img>t_o)
title('tresholded','FontSize',14,'fontweight','bold')



t_o = iterative_tresholding(img,1);

[IDX,sep] = otsu(img,4);
figure;
subplot(1,2,1)
imshow(img)
title('original','FontSize',14,'fontweight','bold')
subplot(1,2,2)
imshow(IDX,[])
title('OTSU','FontSize',14,'fontweight','bold')
