表面滤波是一种保边滤波方法,它由当前灰度差的绝对值分配权重,具体公式见https://blog.csdn.net/xiaota00/article/details/74908785。下面给出表面滤波用于人脸磨皮实验的matlab代码及处理结果(这里给出了两种融合策略,其中一种是所谓“平湖法”,见https://www.cnblogs.com/Imageshop/p/4709710.html,另一种是由原图+平滑图像+精细纹理三成分组合的融合方法):
clear all;close all;clc;
img=imread('test1.png');
figure('name','原图'),imshow(img);
img=double(img);
% lpImg=denoiseBasedLocalStat(uint8(img),10,30);
lpImg=surfaceBlur(img,10,25);
figure('name','滤波结果'),imshow(lpImg);
lpImg=double(lpImg);
diff=double(img)-double(lpImg)+128;
figure('name','高频成分'),imshow(uint8(diff));
f=fspecial('gaussian',5,1.5);
fDiff=imfilter(diff,f);
figure('name','弱化的高频'),imshow(uint8(fDiff));
blurredImg=img-fDiff+128;
figure('name','保留精细纹理的去噪图像'),imshow(uint8(blurredImg));
w=0.8;
mergedImg2=(1-w)*img+w*blurredImg;
figure('name','第一种融合结果'),imshow(uint8(mergedImg2));
fineImg=diff-fDiff;
figure('name','精细纹理(放大结果)'),imshow(uint8(5*fineImg+128));
w1=1.0;w2=0.5;
mergedImg=(1-w1)*img+w1*lpImg+w2*fineImg;
figure('name','原图+低频+精细纹理'),imshow(uint8(mergedImg));
function blurredImg=surfaceBlur(img,r,th)
% 表面滤波
% 只接受值域为[0,255]的图像
if size(img,3)==1
img=double(img);
blurredImg=surfaceBlur_gray(img,r,th);
blurredImg=uint8(blurredImg);
elseif size(img,3)==3
img=double(img);
for i=1:3
img(:,:,i)=surfaceBlur_gray(img(:,:,i),r,th);
end
blurredImg=uint8(img);
end
end
function blurredImg=surfaceBlur_gray(img, r, th)
paddedImg=padarray(img,[r,r],'symmetric','both');
blurredImg=img;
for i=1+r : size(paddedImg,1)-r
for j=1+r : size(paddedImg,2)-r
block=paddedImg(i-r:i+r, j-r:j+r);
w=max(1-abs(block-paddedImg(i,j))/(2.5*th),0);
blurredImg(i-r, j-r)=sum(sum(block.*w))/sum(w(:));
end
end
end
![](https://i-blog.csdnimg.cn/blog_migrate/d76e2edbd84e5ca7ce0bfb2bbd2910a2.png)
![](https://i-blog.csdnimg.cn/blog_migrate/d139b817f8050974d69ef947f47ae23f.png)
![](https://i-blog.csdnimg.cn/blog_migrate/673e2759e031268a770342ff8d63c31a.png)
![](https://i-blog.csdnimg.cn/blog_migrate/85b114f3ee243e2f806021d7a943bdc1.png)
![](https://i-blog.csdnimg.cn/blog_migrate/52cb82970f166160776ecd7aa993e53d.png)
![](https://i-blog.csdnimg.cn/blog_migrate/79964dcaf374c6a41450b8b9d34d8fa4.png)
![](https://i-blog.csdnimg.cn/blog_migrate/166cf290f002b8d9e4c8761bf9c6a7b4.png)
![](https://i-blog.csdnimg.cn/blog_migrate/a8c8661bb1a53a08ee5fc30157ce1123.png)