Saturday, April 18, 2015

Matlab Implementation For Average Filter Using Integral Image

function [Filtered_image] = AverageFilter(A,s)
%S should be odd if s is even will increment by 1
%size of the filter is sxs

if(rem(s,2)==0 && s>1)
   s=s+1;
end  
       

II=integralImage(A);

x=zeros(size(II,1),size(II,2));
for m = 2:size(A,1)
    for n = 2:size(A,2)
       
        if((m+floor(s/2))<=size(II,1)&&(n+floor(s/2))<=size(II,2))
          x(m,n)=x(m,n)+II(m+floor(s/2),n+floor(s/2));
        end
        if((n-floor(s/2)-1)>=1&&(m-floor(s/2)-1)>=1)
         x(m,n)=x(m,n)+II(m-floor(s/2)-1,n-floor(s/2)-1);
        end  
        if((m-floor(s/2)-1)>=1&&(n+floor(s/2))<=size(II,2))
             x(m,n)=x(m,n)-II(m-floor(s/2)-1,n+floor(s/2));  
        end
        if((m+floor(s/2))<=size(II,1)&&(n-floor(s/2)-1)>=1)
             x(m,n)=x(m,n)-II(m+floor(s/2),n-floor(s/2)-1);
        end
       
       x(m,n)=round((x(m,n)/(s*s)));
       
       
    end
end

 Filtered_image= uint8(x(2:size(x,1),2:size(x,2)));
end

No comments:

Post a Comment