雙邊濾波matlab

直接上代碼


function img = myBilateralFilter(Image, kerSize, delta)
% Image 待濾波圖像
% kerSize 濾波核大小
% delta 標準差
% img 輸出圖像

%%
% c,r分別爲核kerSize的垂直半徑和水平半徑
c = floor(kerSize(1)/2);
r = floor(kerSize(2)/2);

% 鏡像填充邊界
padImage = padarray(Image, [c, r], 'symmetric');
img = zeros(size(Image));

% G distance weight
siz = (kerSize-1)/2;
std = delta;
[x, y] = meshgrid(-siz(2): siz(2), -siz(1): siz(1));
arg    = -(x.*x + y.*y)/(2*std*std);
G      = exp(arg);
G(G<eps*max(G(:))) = 0;
sumG = sum(G(:));
if sumG ~= 0
   G  = G/sumG;
end;

% H爲intensity weight 
[iHeight, iWidth] = size(Image);
padImage = double(padImage);
for i = c+1: iHeight+c
    for j = r+1: iWidth+r
        tmp = padImage(i-c: i+c, j-r: j+r);
        t = tmp - double(Image(i-c, j-r)).*ones(2*c+1, 2*r+1);
        H   = exp(-t.*t/(2*delta*delta));
        sumH = sum(H(:));
        if sumH ~= 0
            H = H/sumH;
        end
        W   = G.*H;
        s = tmp.*W;
        img(i-r, j-r) = sum(s(:))/sum(W(:));
    end
end

        

函數調用

delta = 5;
bilateralImg = myBilateralFilter(Image, [5, 5], delta);
bilateralImg = uint8(bilateralImg);

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章