opencv 双边滤波有封装好的函数
bilateralFilter(in_32f, out, 3, 30, 3);
不多说了。
讲讲matlab的双边滤波吧。
网上有关于双边滤波的matlab程序,但是有BUG
在研究生阶段自己用C写的,
于是自己matlab写了一个新的
双边滤波做的单通道的,目前用于灰度图
function resultI = BilateralFiltGray(I,d,sigma)
[m n] = size(I);
newI = ReflectEdge(I,d);
resultI = zeros(m,n);
width = 2*d+1;
%Distance
D = fspecial('gaussian',[width,width],sigma(1));%空间核
S = zeros(width,width);%pix Similarity
h = waitbar(0,'Applying bilateral filter...');
set(h,'Name','Bilateral Filter Progress');
for i=1+d:m+d
for j=1+d:n+d
pixValue = newI(i-d:i+d,j-d:j+d);
subValue = pixValue-newI(i,j);
S = exp(-subValue.^2/(2*sigma(2)^2)); %值域核
H = S.*D; %权重
resultI(i-d,j-d) = sum(pixValue(:).*H(:))/sum(H(:));
end
waitbar(i/m);
end
close(h);
end
下面是边界镜像程序,做了多通道的,备用彩图滤波
function newI = ReflectEdge(I,d)
[m n p] = size(I);
newI = zeros(m+2*d,n+2*d,p);
newI(d+1:d+m,d+1:d+n,1:p) = I; %中间部分
newI(1:d,d+1:d+n,1:p) = I(d+1:-1:2,:,1:p); %上
newI(end-d+1:end,d+1:d+n,1:p) = I(end-1:-1:end-d,:,1:p); %下
newI(:,1:d,1:p) = newI(:,2*d+1:-1:d+2,1:p); %左
newI(:,n+d+1:n+2*d,1:p) = newI(:,n+d-1:-1:n,1:p); %右
end
example code
filtered = BilateralFilt2(double(cutted),2,[1 1000]);