% SAIF MATLAB toolbox

% H. Talebi, X. Zhu and P. Milanfar, "How to SAIF-ly Boost Denoising Performance", IEEE Transactions on Image Processing, vol 22, No. 4, pp. 1470-1485, April 2013.
% This is experimental software. It is provided for non-commercial research purposes only. Use at your own risk. No warranty is implied by 
% this distribution. Copyright © 2012 by University of California.

addpath('support');
clc;  
z = double(imread('parrot.png')); % clean image

sigma = 25;
randn('state', 1); % initialization
y = z + randn(size(z)) * sigma; % noisy image

kernel = 'NLM';
%        kernel: baseline kernel and also the pre-filter:
%                Non-loal Means: 'NLM' (default)
%                Locally adaptive kernel regression: 'LRK'
%                Bilateral: 'BLT'

estimator = 'PLGN';
%        Estimator: Plug-in: 'PLGN' (default)
%                   Stein's Unbiased Estimator: 'SURE'

[zh, zt] = SAIF(y,z,sigma,kernel,estimator);

% Display Noisy Image
en = y - z;
MSE_Noisy = mean(en(:).^2);
PSNR_Noisy = 10*log10((255^2)/MSE_Noisy);
figure,imshow(y,[0 255]);
title(sprintf('Noisy Image (PSNR = %.2f dB)', PSNR_Noisy), 'FontSize', 12);

% Display Prefiltered Image
et = zt - z;
MSE_PreFilter = mean(et(:).^2);
PSNR_PreFilter = 10*log10((255^2)/MSE_PreFilter);
figure,imshow(zt,[0 255]);
title(sprintf('PreFiltered Image (PSNR = %.2f dB)', PSNR_PreFilter), 'FontSize', 12);

% Display SAIF result
eh = zh - z;
MSE_SAIF = mean(eh(:).^2);
PSNR_SAIF = 10*log10((255^2)/MSE_SAIF);
disp(sprintf('SAIF PSNR = %.2f dB', PSNR_SAIF))
figure,imshow(zh,[0 255]);
title(sprintf('SAIF (PSNR = %.2f dB)', PSNR_SAIF), 'FontSize', 12);
