生活随笔
收集整理的這篇文章主要介紹了
双边滤波器的原理及实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
雙邊濾波器是什么?
雙邊濾波(Bilateral filter)是一種可以保邊去噪的濾波器。之所以可以達到此去噪效果,是因為濾波器是由兩個函數構成。一個函數是由幾何空間距離決定濾波器系數。另一個由像素差值決定濾波器系數。可以與其相比較的兩個filter:高斯低通濾波器(http://en.wikipedia.org/wiki/Gaussian_filter)和α-截尾均值濾波器(去掉百分率為α的最小值和最大之后剩下像素的均值作為濾波器),后文中將結合公式做詳細介紹。
雙邊濾波器中,輸出像素的值依賴于鄰域像素的值的加權組合,
權重系數w(i,j,k,l)取決于定義域核
和值域核
的乘積
同時考慮了空間域與值域的差別,而Gaussian Filter和α均值濾波分別只考慮了空間域和值域差別。
=======================================================================
雙邊濾波器的實現(MATLAB):function B = bfilter2(A,w,sigma)
CopyRight:
% Douglas R. Lanman, Brown University, September 2006.
% dlanman@brown.edu, http://mesh.brown.edu/dlanman
具體請見function B = bfltGray(A,w,sigma_d,sigma_r)函數說明。
[cpp]?view plain
?copy %簡單地說:?? %A為給定圖像,歸一化到[0,1]的矩陣?? %W為雙邊濾波器(核)的邊長/2?? %定義域方差σd記為SIGMA(1),值域方差σr記為SIGMA(2)?? ?? %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%?? %?Pre-process?input?and?select?appropriate?filter.?? function?B?=?bfilter2(A,w,sigma)?? ?? %?Verify?that?the?input?image?exists?and?is?valid.?? if?~exist('A','var')?||?isempty(A)?? ???error('Input?image?A?is?undefined?or?invalid.');?? end?? if?~isfloat(A)?||?~sum([1,3]?==?size(A,3))?||?...?? ??????min(A(:))?<?0?||?max(A(:))?>?1?? ???error(['Input?image?A?must?be?a?double?precision?',...?? ??????????'matrix?of?size?NxMx1?or?NxMx3?on?the?closed?',...?? ??????????'interval?[0,1].']);???????? end?? ?? %?Verify?bilateral?filter?window?size.?? if?~exist('w','var')?||?isempty(w)?||?...?? ??????numel(w)?~=?1?||?w?<?1?? ???w?=?5;?? end?? w?=?ceil(w);?? ?? %?Verify?bilateral?filter?standard?deviations.?? if?~exist('sigma','var')?||?isempty(sigma)?||?...?? ??????numel(sigma)?~=?2?||?sigma(1)?<=?0?||?sigma(2)?<=?0?? ???sigma?=?[3?0.1];?? end?? ?? %?Apply?either?grayscale?or?color?bilateral?filtering.?? if?size(A,3)?==?1?? ???B?=?bfltGray(A,w,sigma(1),sigma(2));?? else?? ???B?=?bfltColor(A,w,sigma(1),sigma(2));?? end?? ?? ?? %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%?? %?Implements?bilateral?filtering?for?grayscale?images.?? function?B?=?bfltGray(A,w,sigma_d,sigma_r)?? ?? %?Pre-compute?Gaussian?distance?weights.?? [X,Y]?=?meshgrid(-w:w,-w:w);?? %創建核距離矩陣,e.g.?? %??[x,y]=meshgrid(-1:1,-1:1)?? %??? %?x?=?? %??? %?????-1?????0?????1?? %?????-1?????0?????1?? %?????-1?????0?????1?? %??? %??? %?y?=?? %??? %?????-1????-1????-1?? %??????0?????0?????0?? %??????1?????1?????1?? %計算定義域核?? G?=?exp(-(X.^2+Y.^2)/(2*sigma_d^2));?? ?? %?Create?waitbar.?? h?=?waitbar(0,'Applying?bilateral?filter...');?? set(h,'Name','Bilateral?Filter?Progress');?? ?? %?Apply?bilateral?filter.?? %計算值域核H?并與定義域核G?乘積得到雙邊權重函數F?? dim?=?size(A);?? B?=?zeros(dim);?? for?i?=?1:dim(1)?? ???for?j?=?1:dim(2)?? ???????? ?????????%?Extract?local?region.?? ?????????iMin?=?max(i-w,1);?? ?????????iMax?=?min(i+w,dim(1));?? ?????????jMin?=?max(j-w,1);?? ?????????jMax?=?min(j+w,dim(2));?? ?????????%定義當前核所作用的區域為(iMin:iMax,jMin:jMax)?? ?????????I?=?A(iMin:iMax,jMin:jMax);%提取該區域的源圖像值賦給I?? ???????? ?????????%?Compute?Gaussian?intensity?weights.?? ?????????H?=?exp(-(I-A(i,j)).^2/(2*sigma_r^2));?? ???????? ?????????%?Calculate?bilateral?filter?response.?? ?????????F?=?H.*G((iMin:iMax)-i+w+1,(jMin:jMax)-j+w+1);?? ?????????B(i,j)?=?sum(F(:).*I(:))/sum(F(:));?? ????????????????? ???end?? ???waitbar(i/dim(1));?? end?? ?? %?Close?waitbar.?? close(h);?? ?? ?? %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%?? %?Implements?bilateral?filter?for?color?images.?? function?B?=?bfltColor(A,w,sigma_d,sigma_r)?? ?? %?Convert?input?sRGB?image?to?CIELab?color?space.?? if?exist('applycform','file')?? ???A?=?applycform(A,makecform('srgb2lab'));?? else?? ???A?=?colorspace('Lab<-RGB',A);?? end?? ?? %?Pre-compute?Gaussian?domain?weights.?? [X,Y]?=?meshgrid(-w:w,-w:w);?? G?=?exp(-(X.^2+Y.^2)/(2*sigma_d^2));?? ?? %?Rescale?range?variance?(using?maximum?luminance).?? sigma_r?=?100*sigma_r;?? ?? %?Create?waitbar.?? h?=?waitbar(0,'Applying?bilateral?filter...');?? set(h,'Name','Bilateral?Filter?Progress');?? ?? %?Apply?bilateral?filter.?? dim?=?size(A);?? B?=?zeros(dim);?? for?i?=?1:dim(1)?? ???for?j?=?1:dim(2)?? ???????? ?????????%?Extract?local?region.?? ?????????iMin?=?max(i-w,1);?? ?????????iMax?=?min(i+w,dim(1));?? ?????????jMin?=?max(j-w,1);?? ?????????jMax?=?min(j+w,dim(2));?? ?????????I?=?A(iMin:iMax,jMin:jMax,:);?? ???????? ?????????%?Compute?Gaussian?range?weights.?? ?????????dL?=?I(:,:,1)-A(i,j,1);?? ?????????da?=?I(:,:,2)-A(i,j,2);?? ?????????db?=?I(:,:,3)-A(i,j,3);?? ?????????H?=?exp(-(dL.^2+da.^2+db.^2)/(2*sigma_r^2));?? ???????? ?????????%?Calculate?bilateral?filter?response.?? ?????????F?=?H.*G((iMin:iMax)-i+w+1,(jMin:jMax)-j+w+1);?? ?????????norm_F?=?sum(F(:));?? ?????????B(i,j,1)?=?sum(sum(F.*I(:,:,1)))/norm_F;?? ?????????B(i,j,2)?=?sum(sum(F.*I(:,:,2)))/norm_F;?? ?????????B(i,j,3)?=?sum(sum(F.*I(:,:,3)))/norm_F;?? ?????????????????? ???end?? ???waitbar(i/dim(1));?? end?? ?? %?Convert?filtered?image?back?to?sRGB?color?space.?? if?exist('applycform','file')?? ???B?=?applycform(B,makecform('lab2srgb'));?? else???? ???B?=?colorspace('RGB<-Lab',B);?? end?? ?? %?Close?waitbar.?? close(h);??
調用方法:
[cpp]?view plain
?copy I=imread('einstein.jpg');?? I=double(I)/255;?? ?? w?????=?5;???????%?bilateral?filter?half-width?? sigma?=?[3?0.1];?%?bilateral?filter?standard?deviations?? ?? I1=bfilter2(I,w,sigma);?? ?? subplot(1,2,1);?? imshow(I);?? subplot(1,2,2);?? imshow(I1)??
實驗結果:
參考資料:
1.《Computer Vision Algorithms and Applications》
2.?http://de.wikipedia.org/wiki/Bilaterale_Filterung
3.http://www.cs.duke.edu/~tomasi/papers/tomasi/tomasiIccv98.pdf
4.?http://homepages.inf.ed.ac.uk/rbf/CVonline/LOCAL_COPIES/MANDUCHI1/Bilateral_Filtering.html
5.?http://mesh.brown.edu/dlanman
from:?http://blog.csdn.net/abcjennifer/article/details/7616663
總結
以上是生活随笔為你收集整理的双边滤波器的原理及实现的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。