一、双边滤波图像去噪简介

理论知识参考文献:基于联合双边滤波的图像去噪与融合方法研究

二、部分源代码

% Image Denoising using Optimally Weighted Bilateral Filters
%

%
%  Acronyms:
% 
%  SBF: Standard Bilateral Filter.
%  RBF: Robust Bilater Filter.
%  WBF: Weighted Bilateral Filter.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% parameters
sigma = 30;        %  noise 
sigmas = 4;        %  spatial gaussian kernel 
sigmar1 = 20;    %  range gaussian kernel for SBF
sigmar2 = 20;    %  range gaussian kernel  for RBF
w = 6*round(sigmas)+1;
tol = 0.01;
%
% read input image
%
f0 = double(imread('./images/cameraman.tif'));
[m,n]=size(f0);                                                     
%
% generate noise
%
noise=sigma*randn(m,n);
%
% noisy input for the algorithm
%
f = double(f0) + noise;
%
% compute SBF and divergence
%
L = 0;
[M, N]=computeTruncation(f, sigmar1, w, tol);
[f1, div1] = computeDivergence(f, f, sigmas,sigmar1,L,w,N,M);
%
% compute RBF and divergence
%
L = 1;
h = ones(2*L+1, 2*L+1) /((2*L+1)^2) ;
barf  =  imfilter(f,h);
[M, N] = computeTruncation(barf, sigmar2, w, tol);
[f2, div2] = computeDivergence(f, barf,sigmas,sigmar2,L,w,N,M);
%
% compute optimal weights
%
A = [norm(f1,'fro')^2,  sum(sum(f1.*f2));
         sum(sum(f1.*f2)), norm(f2,'fro')^2];
b = [sum(sum(f1.*f)) - sigma^2*div1; 
        sum(sum(f2.*f)) - sigma^2*div2];
theta = A\b; 
%
% form the WBF
%
bfoptSURE = theta(1)*f1 + theta(2)*f2;
% computing PSNR's 
peak=255;
PSNRnoisy = 10 * log10(m*n*peak^2/ ...
    sum(sum((f - f0).^2)) );         % noisy PSNR                                              
PSNR_f1 = 10 * log10(m * n * peak^2 ...                                  
    / sum(sum((f1 - f0).^2)) );       % SBF PSNR                                          
PSNR_f2 = 10 * log10(m * n * peak^2 ...                                  
    / sum(sum((f2 - f0).^2)) );       % RBF PSNR                                          
PSNRb_f = 10 * log10(m * n * peak^2 ...                       
    / sum(sum((bfoptSURE - f0).^2)) );  % WBF PSNR
%
% display
%
h=figure('Units','normalized','Position',[0 0.5 1 0.5]);
set(h,'name','Denoising Results')
colormap gray,
%
subplot(2,3,1)
imshow(uint8(f0));
title('Clean Image')
%
subplot(2,3,2)
imshow(uint8(f));
title(['Noisy Image, PSNR = ', ...
    num2str(PSNRnoisy,'%.2f')]);
%
subplot(2,3,3)
imshow(uint8(f1));
title(['SBF, PSNR = ', ...
    num2str(PSNR_f1,'%.2f')]);
%
subplot(2,3,4)
imshow(uint8(f2));
title(['RBF, PSNR = ', ...
    num2str(PSNR_f2,'%.2f')]);
%
subplot(2,3,5)
imshow(uint8(bfoptSURE));
title(['WBF, PSNR = ', ...
    num2str(PSNRb_f,'%.2f')]);
%
subplot(2,3,6)
imshow(uint8(bfoptSURE-f0));
title('Residual Noise in WBF');
% compute filter and divergence using shiftable algorithm
%
function [F, div] = computeDivergence ...
    (f,barf,sigmas,sigmar,L,w,N,M)
filt   = fspecial('gaussian', [w w], sigmas);
ct = (w+1)/2;
centerWt = filt(ct,ct);
gamma  =  1/(sqrt(N)*sigmar);
twoN    =  2^N;
[m,n] = size(f);

S = zeros(m,n);
delR = zeros(m,n);
delS = zeros(m,n);
if M == 0
    for k = 0 : N
        omegak = (2*k - N)*gamma;
        ck = nchoosek(N,k)/twoN;
        U = exp(-ii*omegak*barf);
        W = conj(U);
        V = W.*f;
        barV  = imfilter(V, filt);
        barW = imfilter(W, filt);
        B = ck*U.*barV;
        C = ck*U.*barW;
        R = R + B;
        S  = S + C;
        delR = delR + omegak*B;
        delS = delS  + omegak*C;
    end
    F = real(R./S);
    delR =  centerWt - (1/(2*L+1))^2*ii*delR;
    delS  = -ii*(1/(2*L+1))^2*delS;
else
    sumck = 0;
    sumckwk = 0;
    if N < 50
        for k = M : N - M
            omegak = (2*k - N)*gamma;
            warning('off'); %#ok<WNOFF>
            ck = nchoosek(N,k)/twoN;
            U = exp(-ii*omegak*barf);
            W = conj(U);
            V = W.*f;
            barV  = imfilter(V, filt);
            barW = imfilter(W, filt);
            B = ck*U.*barV;
            C = ck*U.*barW;
            sumck = sumck + ck;
            sumckwk = sumckwk + ck*omegak;
            R = R + B;
            S  = S + C;
            delR = delR + omegak*B;
            delS = delS  + omegak*C;
        end
    else
        for k = M : N - M
            omegak = (2*k - N)*gamma;
            warning('off'); %#ok<WNOFF>
            % Sterling's approximation
            ck = exp(logfactorial(N) - logfactorial(k) ...
            - logfactorial(N-k) - N*log(2));
           
            W = conj(U);
            V = W.*f;
            barV  = imfilter(V, filt);
            barW = imfilter(W, filt);
            B = ck*U.*barV;
            C = ck*U.*barW;
            sumck = sumck + ck;
            sumckwk = sumckwk + ck*omegak;
            R = R + B;
            S  = S + C;
            delR = delR + omegak*B;
            delS = delS  + omegak*C;
        end
    end
    F =real(R./S);
    delR = centerWt*sumck + ii*(1/(2*L+1))^2* ...
        (centerWt*sumckwk*f - delR);
    delS = ii*(1/(2*L+1))^2*(centerWt*sumckwk - delS);
end
div  = real(sum(sum((S.*delR - R.*delS)./S.^2)));
end


三、运行结果

【图像去噪】基于matlab最佳加权双边滤波图像去噪【含Matlab源码 459期】_双边滤波

四、matlab版本及参考文献

1 matlab版本
2014a

2 参考文献
[1] 蔡利梅.MATLAB图像处理——理论、算法与实例分析[M].清华大学出版社,2020.
[2]杨丹,赵海滨,龙哲.MATLAB图像处理实例详解[M].清华大学出版社,2013.
[3]周品.MATLAB图像处理与图形用户界面设计[M].清华大学出版社,2013.
[4]刘成龙.精通MATLAB图像处理[M].清华大学出版社,2015.