双边滤波器的原理及实现(2)

% Convert input sRGB image to CIELab color space.
if exist('applycform','file')
  A = applycform(A,makecform('srgb2lab'));
else
  A = colorspace('Lab<-RGB',A);
end

% Pre-compute Gaussian domain weights.
[X,Y] = meshgrid(-w:w,-w:w);
G = exp(-(X.^2+Y.^2)/(2*sigma_d^2));

% Rescale range variance (using maximum luminance).
sigma_r = 100*sigma_r;

% Create waitbar.
h = waitbar(0,'Applying bilateral filter...');
set(h,'Name','Bilateral Filter Progress');

% Apply bilateral filter.
dim = size(A);
B = zeros(dim);
for i = 1:dim(1)
  for j = 1:dim(2)
     
        % Extract local region.
        iMin = max(i-w,1);
        iMax = min(i+w,dim(1));
        jMin = max(j-w,1);
        jMax = min(j+w,dim(2));
        I = A(iMin:iMax,jMin:jMax,:);
     
        % Compute Gaussian range weights.
        dL = I(:,:,1)-A(i,j,1);
        da = I(:,:,2)-A(i,j,2);
        db = I(:,:,3)-A(i,j,3);
        H = exp(-(dL.^2+da.^2+db.^2)/(2*sigma_r^2));
     
        % Calculate bilateral filter response.
        F = H.*G((iMin:iMax)-i+w+1,(jMin:jMax)-j+w+1);
        norm_F = sum(F(:));
        B(i,j,1) = sum(sum(F.*I(:,:,1)))/norm_F;
        B(i,j,2) = sum(sum(F.*I(:,:,2)))/norm_F;
        B(i,j,3) = sum(sum(F.*I(:,:,3)))/norm_F;
               
  end
  waitbar(i/dim(1));
end

% Convert filtered image back to sRGB color space.
if exist('applycform','file')
  B = applycform(B,makecform('lab2srgb'));
else 
  B = colorspace('RGB<-Lab',B);
end

% Close waitbar.
close(h);

调用方法:

I=imread('einstein.jpg');
I=double(I)/255;

w    = 5;      % bilateral filter half-width
sigma = [3 0.1]; % bilateral filter standard deviations

I1=bfilter2(I,w,sigma);

subplot(1,2,1);
imshow(I);
subplot(1,2,2);
imshow(I1)

实验结果:

双边滤波器的原理及实现

参考资料:

1.《Computer Vision Algorithms and Applications》

2. 

3.~tomasi/papers/tomasi/tomasiIccv98.pdf

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/f5a80cb1b862296adfae211f0770708a.html