Thread: Median filter help

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    6

    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.

    [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
    Last edited by JTEK24; 07-04-2009 at 11:15 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    Quote Originally Posted by tabstop View Post
    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.
    Last edited by JTEK24; 07-04-2009 at 11:42 AM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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?

  5. #5
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    Quote Originally Posted by tabstop View Post
    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.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  7. #7
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    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?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  9. #9
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    Ok, explain to me how to trace it on paper. Thats why I asked how it works so I could get an explaination.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by JTEK24 View Post
    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.

  11. #11
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    Alright, Im about to write it out now. I'll post if I have further questions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-30-2007, 10:08 AM
  2. moving median function
    By supermeew in forum C Programming
    Replies: 0
    Last Post: 05-04-2006, 02:37 PM
  3. Simple Filter
    By 00Sven in forum C Programming
    Replies: 3
    Last Post: 03-14-2006, 08:46 PM
  4. Computing Mean, Median and Mode Using Arrays
    By Rodneo in forum C++ Programming
    Replies: 0
    Last Post: 05-29-2002, 11:40 PM
  5. median
    By frank in forum C++ Programming
    Replies: 4
    Last Post: 10-28-2001, 04:32 PM