用MAtlab写一个双边滤波算法代码
时间: 2023-02-24 11:36:22 浏览: 390
双边滤波(bilateral filter) matlab代码
5星 · 资源好评率100%
可以这样写:
```
function output = bilateral_filter(input, sigma_d, sigma_r)
% bilateral_filter - Bilateral filtering of a grayscale image
%
% input - input image
% sigma_d - spatial standard deviation
% sigma_r - range standard deviation
% Get the size of the input image
[m, n] = size(input);
% Create a coordinate grid
[X, Y] = meshgrid(1:n, 1:m);
% Compute the Gaussian spatial weight
G_d = exp(-(X - n/2).^2/(2*sigma_d^2) - (Y - m/2).^2/(2*sigma_d^2));
% Initialize the output image
output = zeros(m, n);
% Iterate over each pixel
for i = 1:m
for j = 1:n
% Compute the range weight
range_weights = exp(-(input - input(i,j)).^2/(2*sigma_r^2));
% Compute the bilateral filter response
output(i,j) = sum(sum(G_d.*range_weights.*input))/sum(sum(G_d.*range_weights));
end
end
```
这是双边滤波算法的基本代码实现,sigma_d 和 sigma_r 分别表示空间标准差和范围标准差,input 表示输入图像,output 表示输出图像。
阅读全文