# Median filter help

• 07-04-2009
JTEK24
Median filter help
Hello everyone, I am trying to create a program(within MATLAB) that will take a m(rows) x n(columns) matrix(example a 3x3 matrix), surround the perimeter of the matrix with zeroes, making it a 5x5 matrix. What I am trying to do is create a 2d median filter.:confused:

[1 2 3]
[4 5 6]
[7 8 9]

[0 0 0 0 0]
[0 1 2 3 0]
[0 4 5 6 0]
[0 7 8 9 0]
[0 0 0 0 0]

also take the median(kernel) of odd sizes, such as 3x3, 5x5, 7x7 and so on. for example, take the first nine for a 3x3 which will be:

[0 0 0]
[0 1 2]
[0 4 5]

placing the numbers of the matrix in order: (0,0,0,0,0,1,2,4,5), taking the median which in this case of a 3x3...the 5th number(0) will replace the center value 1. and then the 3x3 kernel will slide to the left one and take the median of:
[0 0 0]
[1 2 3]
[4 5 6] and replacing the center value 2 and slide over again and repeat the sequence until all center values of the mxn matrix are replaced.

I was told for loops would work but ive had no luck so far. any help would be greatly appreciated and please make comments in the steps so I can understand what each line does.
Thanks
-Greg
• 07-04-2009
tabstop
For each element [i,j] in the original: what are the coordinates of it's location in the new matrix? That should be all the hint you need.
• 07-04-2009
JTEK24
Quote:

Originally Posted by tabstop
For each element [i,j] in the original: what are the coordinates of it's location in the new matrix? That should be all the hint you need.

They would be in the same location right? My professor told the class the same thing but we are clueless. We dont have a background in this software and we haven't taken a programing class since 4 years ago and im trying to get the for loops right but its not working.
• 07-04-2009
tabstop
Surely you can count past zero, still, though.

For instance, in your original example, consider the number 4. It starts in the [1][0] spot (second row, first column). Where does it end up? Consider the number 8. It starts in the [2][1] spot. Where does it end up?
• 07-04-2009
JTEK24
Quote:

Originally Posted by tabstop
Surely you can count past zero, still, though.

For instance, in your original example, consider the number 4. It starts in the [1][0] spot (second row, first column). Where does it end up? Consider the number 8. It starts in the [2][1] spot. Where does it end up?

the 4 would be replaced by 2 and the 8 will be replaced by 5...i know how it computes...but actually writing the code is not making sense.
• 07-04-2009
tabstop
We're also still asking you to read English sentences. Where does the 4 go? What coordinates, in your answer matrix, will have the number four in it? (And then, how does that relate to the original coordinates.) Obviously you can't compute the number from the old number -- things are moving, not being transformed by a function.
• 07-15-2009
JTEK24
q=9;
for k = 1:q-1;
b = k;
for r = k+1:q;
if v(r) < v(b);
b = r;

end
end
v([k,b])= v([b,k]);
end

how does this sort fuction work?
• 07-16-2009
tabstop
Have you traced through it on a piece of paper? (If 9 elements seems daunting, then maybe change the 9 to a 4 or so.) Watch it go, and you'll soon see how it works.
• 07-16-2009
JTEK24
Ok, explain to me how to trace it on paper. Thats why I asked how it works so I could get an explaination.
• 07-16-2009
tabstop
Quote:

Originally Posted by JTEK24
Ok, explain to me how to trace it on paper. Thats why I asked how it works so I could get an explaination.

You make a column that says "q", a column that says "k", a column that says "b", a column that says "r", and a big column that says "v". Under each letter you have the value of that variable, which you update as you step through the code. So originally you put 9 under q and some original vector under v; then you step to the for loop, which means that you put a 1 under k; the next statement assigns the current value of k (which your piece of paper tells you is 1) to b, so you write a 1 under the b; then the next statement is the for loop, so the value of k+1 (which your piece of paper tells you is 2) is assigned to r, so you write a 2 under r. Then the next statement requires a decision -- if v(2) [that's the current value of r] is less than v(1) [that's the current value of b], then you cross out the current value of b and replace it with the current value of r.

And you keep going through the code until you get to the end. Your indentation should keep you straight on your for loops.
• 07-16-2009
JTEK24
Alright, Im about to write it out now. I'll post if I have further questions.