matlab 双边滤波

来源:互联网 发布:韩国大学知乎 编辑:程序博客网 时间:2024/04/28 17:09

双边滤波原理:

http://scien.stanford.edu/pages/labsite/2006/psych221/projects/06/imagescaling/bilati.html

双边滤波matlab程序

双边滤波原理:

http://scien.stanford.edu/pages/labsite/2006/psych221/projects/06/imagescaling/bilati.html

双边滤波matlab程序

函数help说明,当在command窗口输入help bfilter2时会显示这些说明文字。这些说明文字在m文档中以注释形式出现,在遇到第一个空行(这行什么也么有)或者没有注释的语句结束。

% BFILTER2 Twodimensional bilateral filtering.

%    This function implements 2-D bilateralfiltering using

%    the method outlined in:

%

%       C. Tomasi and R. Manduchi. BilateralFiltering for

%       Gray and Color Images. In Proceedings ofthe IEEE

%       International Conference on ComputerVision, 1998.

%

%    B = bfilter2(A,W,SIGMA) performs 2-Dbilateral filtering

%    for the grayscale or color image A. Ashould be a double

%    precision matrix of size NxMx1 or NxMx3(i.e., grayscale

%    or color images, respectively) withnormalized values in

%    the closed interval [0,1]. The half-size ofthe Gaussian

%    bilateral filter window is defined by W.The standard

%    deviations of the bilateral filter aregiven by SIGMA,

%    where the spatial-domain standard deviationis given by

%    SIGMA(1) and the intensity-domain standarddeviation is

%    given by SIGMA(2).

%

% Douglas R. Lanman, BrownUniversity,September 2006.

% dlanman@brown.edu,http://mesh.brown.edu/dlanman

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Pre-process input andselect appropriate filter.

functionB = bfilter2(A,w,sigma)

对输入参数做预处理:这部分是自己变成经常忽略的

exist : 变量或或者文件是否存在

isempty:变量是否为空

isfloat:检验变量类型

error:出错输出并退出程序,自己常用return退出程序,用disp输出退出原因

% Verify that the inputimage exists and is valid.

if~exist('A','var') || isempty(A)

   error('Input image A is undefined or invalid.');

end

[1,3]==size(A,3),返回一个矩阵(向量),matlab默认和[1,3]中每个元素比较

if~isfloat(A) || ~sum([1,3] == size(A,3)) || ...

      min(A(:)) < 0 || max(A(:)) > 1

   error(['Input image A must be a double precision ',...

          'matrix of size NxMx1 or NxMx3 on the closed ',...

          'interval [0,1].']);     

end

% Verify bilateral filterwindow size.

if~exist('w','var') || isempty(w) || ...

      numel(w) ~= 1 || w < 1

   w = 5;

end

w = ceil(w);

numel:矩阵中数的个数,自己常用size,length

% Verify bilateral filterstandard deviations.

if~exist('sigma','var') || isempty(sigma) || ...

      numel(sigma) ~= 2 || sigma(1) <= 0 ||sigma(2) <= 0

   sigma = [3 0.1];

end

% Apply either grayscaleor color bilateral filtering.

ifsize(A,3) == 1

   B = bfltGray(A,w,sigma(1),sigma(2));

else

   B = bfltColor(A,w,sigma(1),sigma(2));

end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Implements bilateralfiltering for grayscale images.

functionB = bfltGray(A,w,sigma_d,sigma_r)

高斯滤波器还可以这样生成~

% Pre-compute Gaussiandistance weights.

[X,Y] = meshgrid(-w:w,-w:w);

G =exp(-(X.^2+Y.^2)/(2*sigma_d^2));

程序运行进度条,可以锦上添花

% Create waitbar.

h = waitbar(0,'Applying bilateral filter...');

set(h,'Name','BilateralFilter Progress');

% Apply bilateral filter.

dim = size(A);

B = zeros(dim);

fori = 1:dim(1)

   forj = 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 intensity weights.

         H = exp(-(I-A(i,j)).^2/(2*sigma_r^2));

     

         % Calculate bilateral filter response.

         F =H.*G((iMin:iMax)-i+w+1,(jMin:jMax)-j+w+1);

         B(i,j) = sum(F(:).*I(:))/sum(F(:));

              

   end

   waitbar(i/dim(1));

end

close 和figure的用法一样 ,但clear 就不能加括号了

% Close waitbar.

close(h);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Implements bilateralfilter for color images.

functionB = bfltColor(A,w,sigma_d,sigma_r)

% Convert input sRGBimage to CIELab color space.

ifexist('applycform','file')

   A = applycform(A,makecform('srgb2lab'));

else

   A = colorspace('Lab<-RGB',A);

end

% Pre-compute Gaussiandomain 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','BilateralFilter Progress');

% Apply bilateral filter.

dim = size(A);

B = zeros(dim);

fori = 1:dim(1)

   forj = 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 imageback to sRGB color space.

ifexist('applycform','file')

   B = applycform(B,makecform('lab2srgb'));

else 

   B = colorspace('RGB<-Lab',B);

end

% Close waitbar.

close(h);

 

原创粉丝点击