matlab 双边滤波

本文介绍了MATLAB中实现双边滤波的原理和程序,包括bfilter2函数的详细说明,该函数根据输入参数进行预处理,并应用于灰度或彩色图像,通过高斯距离权重和强度权重计算双边滤波响应。
部署运行你感兴趣的模型镜像

双边滤波原理:

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);

 

您可能感兴趣的与本文相关的镜像

GPT-SoVITS

GPT-SoVITS

AI应用

GPT-SoVITS 是一个开源的文本到语音(TTS)和语音转换模型,它结合了 GPT 的生成能力和 SoVITS 的语音转换技术。该项目以其强大的声音克隆能力而闻名,仅需少量语音样本(如5秒)即可实现高质量的即时语音合成,也可通过更长的音频(如1分钟)进行微调以获得更逼真的效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值