歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

雙邊濾波Matlab實現<The Bilateral Filter>

雙邊濾波與一般的高斯濾波的不同就是:雙邊濾波既利用了位置信息<or 幾何信息——高斯濾波只用了位置信息>又利用了像素信息來定義濾波窗口的權重。


像素值越接近,權重越大。雙邊濾波會去除圖像的細節信息,又能保持邊界。


對於彩色圖像,像素值的接近與否不能使用RGB空間值,雙邊濾波的原始文獻建議使用CIE顏色空間。

代碼如下: 

function resultI = BilateralFilt2(I,d,sigma)
%%%
%Author:LiFeiteng
%Version:1.0——灰色圖像  Time:2013/05/01
%Version:1.1——灰色/彩色圖像  Time:2013/05/02  2013/05/05
%d 半窗口寬度
I = double(I);
if size(I,3)==1
    resultI = BilateralFiltGray(I,d,sigma);
elseif size(I,3)==3
    resultI = BilateralFiltColor(I,d,sigma);
else
    error('Incorrect image size')   
end
end

function resultI = BilateralFiltGray(I,d,sigma)

[m n] = size(I);
newI = ReflectEdge(I,d);
resultI = zeros(m,n);
width = 2*d+1;
%Distance
D = fspecial('gaussian',[width,width],sigma(1));
S = zeros(width,width);%pix Similarity
h = waitbar(0,'Applying bilateral filter...');
set(h,'Name','Bilateral Filter Progress');
for i=1+d:m+d
    for j=1+d:n+d
        pixValue = newI(i-d:i+d,j-d:j+d);
        subValue = pixValue-newI(i,j);
        S = exp(-subValue.^2/(2*sigma(2)^2));
        H = S.*D;
        resultI(i-d,j-d) = sum(pixValue(:).*H(:))/sum(H(:));
    end
    waitbar(i/m);
end
close(h);
end

function resultI = BilateralFiltColor(I,d,sigma)
I = applycform(I,makecform('srgb2lab'));
[m n ~] = size(I);
newI = ReflectEdge(I,d);
resultI = zeros(m,n,3);
width = 2*d+1;
%Distance
D = fspecial('gaussian',[width,width],sigma(1));
% [X,Y] = meshgrid(-d:d,-d:d);
% D = exp(-(X.^2+Y.^2)/(2*sigma(1)^2));
S = zeros(width,width);%pix Similarity
h = waitbar(0,'Applying bilateral filter...');
set(h,'Name','Bilateral Filter Progress');
sigma_r = 100*sigma(2);
for i=1+d:m+d
    for j=1+d:n+d
        pixValue = newI(i-d:i+d,j-d:j+d,1:3);
        %subValue = pixValue-repmat(newI(i,j,1:3),width,width);
        dL = pixValue(:,:,1)-newI(i,j,1);
        da = pixValue(:,:,2)-newI(i,j,2);
        db = pixValue(:,:,3)-newI(i,j,3);
        S = exp(-(dL.^2+da.^2+db.^2)/(2*sigma_r^2));
        H = S.*D;
        H = H./sum(H(:));
        resultI(i-d,j-d,1) = sum(sum(pixValue(:,:,1).*H));
        resultI(i-d,j-d,2) = sum(sum(pixValue(:,:,2).*H));   
        resultI(i-d,j-d,3) = sum(sum(pixValue(:,:,3).*H));   
    end
    waitbar(i/m);
end
close(h);
resultI = applycform(resultI,makecform('lab2srgb'));
end

Copyright © Linux教程網 All Rights Reserved