不难看出,导向图滤波的主要步骤就两个,计算每个公式中的各种平均值,然后根据平均值计算出
计算平均值可以通过积分图高效的实现,matlab代码如下:
矩形滤波代码(计算窗口平均值)
functionimDst=boxfilter(imSrc,r)%BOXFILTERO(1)timeboxfilteringusingcumulativesum % %-DefinitionimDst(x,y)=sum(sum(imSrc(x-r:x r,y-r:y r))); %-Runningtimeindependentofr; %-Equivalenttothefunction:colfilt(imSrc,[2*r 1,2*r 1],'sliding',@sum); %-Butmuchfaster.[hei,wid]=size(imSrc); imDst=zeros(size(imSrc));%cumulativesumoverYaxis imCum=cumsum(imSrc,1); %differenceoverYaxis imDst(1:r 1,:)=imCum(1 r:2*r 1,:); imDst(r 2:hei-r,:)=imCum(2*r 2:hei,:)-imCum(1:hei-2*r-1,:); imDst(hei-r 1:hei,:)=repmat(imCum(hei,:),[r,1])-imCum(hei-2*r:hei-r-1,:);%cumulativesumoverXaxis imCum=cumsum(imDst,2); %differenceoverYaxis imDst(:,1:r 1)=imCum(:,1 r:2*r 1); imDst(:,r 2:wid-r)=imCum(:,2*r 2:wid)-imCum(:,1:wid-2*r-1); imDst(:,wid-r 1:wid)=repmat(imCum(:,wid),[1,r])-imCum(:,wid-2*r:wid-r-1); end