接上篇文章,这次双边滤波处理的是彩色图。
下面是函数代码:
function J=btfColorImage(I,G,sigma_d,sigma_r,filterRadius)
x=-filterRadius:filterRadius;
y=-filterRadius:filterRadius;
[xx,yy]=meshgrid(x,y);
spatialKernel=exp(- (xx.^2+yy.^2)/(2*sigma_d^2));
[rows,cols,channels]=size(I);
rc=zeros(size(I(:,:,1)));
gc=zeros(size(rc));
bc=zeros(size(gc));
if size(G,3)==1
temp=G;
G(:,:,1)=temp;
G(:,:,2)=temp;
G(:,:,3)=temp;
end
parfor y=filterRadius+1:rows-filterRadius
for x=filterRadius+1:cols-filterRadius
roi= I(y-filterRadius:y+filterRadius,x-filterRadius:x+filterRadius,:);
roidif=zeros(size(roi));
roidif(:,:,1)=roi(:,:,1)-G(y,x,1);
roidif(:,:,2)=roi(:,:,2)-G(y,x,2);
roidif(:,:,3)=roi(:,:,3)-G(y,x,3);
roidif=roidif.^2;
roidif=roidif(:,:,1)+roidif(:,:,2)+roidif(:,:,3);
tonalKernel =exp(- roidif/(2*sigma_r^2));
W=(tonalKernel.*spatialKernel);
k=sum(W(:));
RC=W.*roi(:,:,1);
GC=W.*roi(:,:,2);
BC=W.*roi(:,:,3);
rc(y,x)=sum(RC(:))/k;
gc(y,x)=sum(GC(:))/k;
bc(y,x)=sum(BC(:))/k;
end
end
rc=rc(filterRadius+1:end-filterRadius,filterRadius+1:end-filterRadius);
gc=gc(filterRadius+1:end-filterRadius,filterRadius+1:end-filterRadius);
bc=bc(filterRadius+1:end-filterRadius,filterRadius+1:end-filterRadius);
J=cat(3,rc,gc,bc);
end
下面是运行代码:
clear all
g = double(imread('woman.png'))/255.0;
G=g;%guidance image
sigma_d=2;
sigma_r=0.3;
filterSize=double(uint8(3*sigma_d)*2+1);
filterRadius=ceil((filterSize-1)/2);
I=padarray(g,[filterRadius,filterRadius],'replicate');
G=padarray(G,[filterRadius,filterRadius],'replicate');
J=btfColorImage(I,G,sigma_d,sigma_r,filterRadius);
figure;imshow([g,J]);title('input,output');
下面是效果图: