# how 2x2 median filter works

34 views (last 30 days)
sumeet on 29 Aug 2017
Commented: Image Analyst on 30 Sep 2017
hi I tried this
• a = [ 1 2 3 ; 4 5 6; 7 8 9]
• b = medfilt2(a);
Output is [0,2,0;2,5,3;0,5,0]
How were values of b calculated from a ?
I can't figure out the logic.
Thanks.

KSSV on 29 Aug 2017
When ever you use a function in MATLAB.....it should be a common practice to read the documentation part of it. Read the documentation: https://in.mathworks.com/help/images/ref/medfilt2.html. The doc mentions the reference for the function. You may refer that for further idea.
Lim, Jae S., Two-Dimensional Signal and Image Processing, Englewood Cliffs, NJ, Prentice Hall, 1990, pp. 469-476.
sumeet on 30 Sep 2017
The problem is how is window decided for a particular pixel ? The documentation taught that medfilt2 will take 4 values , find their average , discard the fraction and give whole number as output . Is that right ? Then , let the window have values called top left , top right , bottom left and bottom right . 1 maps to 0 would mean that avg of 4 values < 1 . similiarly for 3,7,9 .
2 maps to 2 would mean that avg of 4 values >= 2 and <3 . similairly for 4.
5 maps to 5 would mean that avg of 4 values >= 5 and <6 . similairly for 8.
6 maps to 3 would mean that avg of 4 values >= 3 and <4 .
Now can you tell me four values considered for each case above ? I am unable to find neighbour choosing criterion such that above mapping happens .
Thanks a lot.

Image Analyst on 30 Sep 2017
When the image is this:
a =
1 2 3
4 5 6
7 8 9
and you're using a 2x2 window, and the 2x2 window is in the upper left, it will see
0 0
0 1
The zeros are where the window is off the array. So the median of three zeros and a 1 is zero, so that's why you got a zero in the upper left element of your output.
##### 2 CommentsShowHide 1 older comment
Image Analyst on 30 Sep 2017
Are you using a 2x2, like your subject line says, or a 3x3 like your code does?
a = [ 1 2 3 ; 4 5 6; 7 8 9]
am3x3 = medfilt2(a)
am2x2 = medfilt2(a, [2, 2])
a =
1 2 3
4 5 6
7 8 9
am3x3 =
0 2 0
2 5 3
0 5 0
am2x2 =
3 4 1.5
6 7 3
3.5 4 0