How does Mean Filter work?

I created a small code to see how does Mean Filter work.
How does it get values?? What is the equation?
A = [ 1 2 3; 4 5 6; 7 8 9]
h = ones(3,3)
imfilter(A,h)
12 21 16
27 45 33
24 39 28
I get this as output, I don't understand how exactly 12,21,16,27 is calculated using imfilter command with 3 by 3 filter.

2 Comments

Its taking the sum of the neighboring pixels in the example shown here and not the average. I did not understand how whether it takes sum or average of its neighboring pixels....
This is taking the sum, according to the weighting stored in h. Except for the edge cases you can use
imfilter(A, h/numel(h))
to get the average.
Getting the edge cases right is a little tougher:
imfilter(A, h) ./ imfilter(ones(size(A)), h)
The denominator is effectively counting the number of pixels that are in the neighborhood -- the number of pixels whose value contributed to the sum.

Sign in to comment.

Answers (1)

>> conv2(A,fliplr(flipud(h)))
ans =
1 3 6 5 3
5 12 21 16 9
12 27 45 33 18
11 24 39 28 15
7 15 24 17 9
>> conv2(A,fliplr(flipud(h)), 'same')
ans =
12 21 16
27 45 33
24 39 28
>> imfilter(A,h)
ans =
12 21 16
27 45 33
24 39 28

Asked:

on 26 May 2016

Commented:

on 3 Jul 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!