3x3 Average filter in matlab
        Posted  
        
            by 
                turingcomplete
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by turingcomplete
        
        
        
        Published on 2012-11-18T22:34:51Z
        Indexed on 
            2012/11/18
            23:00 UTC
        
        
        Read the original article
        Hit count: 228
        
matlab
|image-processing
I've written code to smooth an image using a 3x3 averaging filter, however the output is strange, it is almost all black. Here's my code.
function [filtered_img] = average_filter(noisy_img)
    [m,n] = size(noisy_img);
    filtered_img = zeros(m,n);
    for i = 1:m-2
        for j = 1:n-2
            sum = 0;
            for k = i:i+2
                for l = j:j+2
                    sum = sum+noisy_img(k,l);
                end
            end
            filtered_img(i+1,j+1) = sum/9.0;
        end
    end
end
I call the function as follows:
img=imread('img.bmp');
filtered = average_filter(img);
imshow(uint8(filtered));
I can't see anything wrong in the code logic so far, I'd appreciate it if someone can spot the problem.
© Stack Overflow or respective owner