大家好,又见面了,我是你们的朋友全栈君。
思维闭塞时可外出采采风。
1、双边滤波简介:
双边滤波(Bilateral filter)是一种非线性滤波方法(空间权值+相似权值)——空间权值:模糊去噪;相似权值:保护边缘。
2、双边滤波原理
双边滤波具有两个权重:空间权重与相似权重
1)空间权重:与像素位置有关,为像素之间的距离(欧式距离,空间度量),故可定义为全局变量放在循环外,通常定义为
其中表示两个像素间的距离(欧式距离)。该过程滤波如下:
权值为:
2)相似权重:与像素值大小有关,为像素值之间的距离(辐射距离,相似性度量),根据像素值不同而不同,需要放在循环内,通常定义为
其中表示两个像素值之间的距离。该过程滤波如下:
权值为:
3)两者结合,得到基于空间距离、相似程度整体考虑的双边滤波如下:
权值为:
3、双边滤波实现:
实际应用时,根据需要对积分采用离散形式表示,滤波半径根据需要自行设置。
在进行滤波前,需将数据转换为浮点型等。
4、matlab源代码
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%主函数%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 读取
f = imread(filename);
% 设置参数
r = 5;% 滤波半径
a = 3;% 全局方差
b = 0.1;% 局部方差
% 判断二维图还是三维图
if ismatrix(f)
g = bfilt_gray(f,r,a,b);
else
g = bfilt_rgb(f,r,a,b);
end
% 显示
subplot(121)
imshow(f)
subplot(122)
imshow(g)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%灰度图双边滤波%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function g = bfilt_gray(f,r,a,b)
% f灰度图;r滤波半径;a全局方差;b局部方差
[x,y] = meshgrid(-r:r);
w1 = exp(-(x.^2+y.^2)/(2*a^2));
f = tofloat(f);%f = im2double(f);
h = waitbar(0,'Applying bilateral filter...');
set(h,'Name','Bilateral Filter Progress');
[m,n] = size(f);
f_temp = padarray(f,[r r],'symmetric');
g = zeros(m,n);
for i = r+1:m+r
for j = r+1:n+r
temp = f_temp(i-r:i+r,j-r:j+r);
w2 = exp(-(temp-f(i-r,j-r)).^2/(2*b^2));
w = w1.*w2;
s = temp.*w;
g(i-r,j-r) = sum(s(:))/sum(w(:));
end
waitbar((i-r)/m);
end
% g = revertclass(g);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%彩色图双边滤波%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function g = bfilt_rgb(f,r,a,b)
% f灰度图;r滤波半径;a全局方差;b局部方差
[x,y] = meshgrid(-r:r);
w1 = exp(-(x.^2+y.^2)/(2*a^2));
f = tofloat(f);%f = im2double(f);
h = waitbar(0,'Applying bilateral filter...');
set(h,'Name','Bilateral Filter Progress');
fr = f(:,:,1);
fg = f(:,:,2);
fb = f(:,:,3);
[m,n] = size(fr);
fr_temp = padarray(fr,[r r],'symmetric');
fg_temp = padarray(fg,[r r],'symmetric');
fb_temp = padarray(fb,[r r],'symmetric');
[gr,gg,gb] = deal(zeros(size(fr)));
for i = r+1:m+r
for j = r+1:n+r
temp1 = fr_temp(i-r:i+r,j-r:j+r);
temp2 = fg_temp(i-r:i+r,j-r:j+r);
temp3 = fb_temp(i-r:i+r,j-r:j+r);
dr = temp1 - fr_temp(i,j);
dg = temp2 - fg_temp(i,j);
db = temp3 - fb_temp(i,j);
w2 = exp(-(dr.^2+dg.^2+db.^2)/(2*b^2));
w = w1.*w2;
gr(i-r,j-r) = sum(sum(temp1.*w))/sum(w(:));
gg(i-r,j-r) = sum(sum(temp2.*w))/sum(w(:));
gb(i-r,j-r) = sum(sum(temp3.*w))/sum(w(:));
end
waitbar((i-r)/n);
end
g = cat(3,gr,gg,gb);
% g = revertclass(g);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%可以用到的函数%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [out,revertclass]=tofloat(in)
identity=@(x) x;
tosingle=@im2single;
table={'uint8',tosingle,@im2uint8
'uint16',tosingle,@im2uint16
'int16',tosingle,@im2int16
'logical',tosingle,@logical
'double',identity,identity
'single',identity,identity};
classIndex=find(strcmp(class(in),table(:,1)));
if isempty(classIndex)
error('unsupported input immage class.');
end
out=table{classIndex,2}(in);
revertclass=table{classIndex,3};
5、实验结果
6、参考资料
https://blog.csdn.net/abcjennifer/article/details/7616663
https://blog.csdn.net/bugrunner/article/details/7170471
https://www.cnblogs.com/qiqibaby/p/5296681.html
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/127320.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...