双边滤波(bilateral filter)快速算法 matlab实现代码

本算法参考自论文"Paris S, Durand F. A fast approximation of the bilateral filter using a signal processing approach[M]//Computer Vision–ECCV 2006. Springer Berlin Heidelberg, 2006: 568-580."下面的代码也是作者团队编写的。

下面是函数代码:

%
% original src: http://people.csail.mit.edu/jiawen/software/bilateralFilter.m
% original author: Jiawen (Kevin) Chen 
%   <jiawen@csail.mit.edu>
%   http://people.csail.mit.edu/jiawen/
%

% output = bilateralFilter( data, edge, ...
%                          edgeMin, edgeMax, ...
%                          sigmaSpatial, sigmaRange, ...
%                          samplingSpatial, samplingRange )
%
% Bilateral and Cross-Bilateral Filter using the Bilateral Grid.
%
% Bilaterally filters the image 'data' using the edges in the image 'edge'.
% If 'data' == 'edge', then it the standard bilateral filter.
% Otherwise, it is the 'cross' or 'joint' bilateral filter.
% For convenience, you can also pass in [] for 'edge' for the normal
% bilateral filter.
%
% Note that for the cross bilateral filter, data does not need to be
% defined everywhere.  Undefined values can be set to 'NaN'.  However, edge
% *does* need to be defined everywhere.
%
% data and edge should be of the greyscale, double-precision floating point
% matrices of the same size (i.e. they should be [ height x width ])
%
% data is the only required argument
%
% edgeMin and edgeMax specifies the min and max values of 'edge' (or 'data'
% for the normal bilateral filter) and is useful when the input is
  • 2
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
双边滤波算法Bilateral Filter)是一种非线性图像平滑技术,它结合了空间域和灰度值域的信息,能够在保持边缘细节的同时进行平滑处理。在MATLAB中,你可以使用`imgaussfilt`函数结合自定义权重函数来实现双边滤波。以下是一个简单的代码示例: ```matlab function filtered_img = bilateralFilter(img, sigma_spatial, sigma_color) % 计算颜色空间的高斯核 [height, width, channels] = size(img); colorSpace = zeros(height, width, channels, 2); % 假设我们使用RGB for c = 1:channels colorSpace(:, :, c, :) = img(:,:,c) - mean(img(:,:,c)); end colorKernel = fspecial('gaussian', [sigma_color*2+1, sigma_color*2+1], sigma_color); % 计算空间域的高斯核 spatialKernel = fspecial('gaussian', [sigma_spatial*2+1, sigma_spatial*2+1], sigma_spatial); % 创建加权平均的二维数组 weights = spatialKernel .* exp(-0.5 * (colorKernel.^2)); weights = weights ./ sum(weights(:)); % 归一化权重 % 应用双边滤波 filtered_img = imfilter(img, weights, 'replicate'); end % 使用方法: img = imread('your_image.jpg'); % 替换为你要处理的图片路径 sigma_spatial = 5; % 空间平滑程度 sigma_color = 50; % 颜色平滑程度 filtered_img = bilateralFilter(img, sigma_spatial, sigma_color); imshow(filtered_img); ``` 在这个代码中,`sigma_spatial`控制空间滤波的强度,`sigma_color`控制颜色滤波的强度。`imfilter`函数用于滤波操作,`replicate`边界处理方式用于保持边缘信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值