C Board  

Go Back   C Board > Community Boards > Tech Board

Reply
 
LinkBack Thread Tools Display Modes
Old 07-04-2009, 10:59 AM   #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.
JTEK24 is offline   Reply With Quote
Old 07-04-2009, 11:31 AM   #2
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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.
tabstop is offline   Reply With Quote
Old 07-04-2009, 11:34 AM   #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.
JTEK24 is offline   Reply With Quote
Old 07-04-2009, 06:47 PM   #4
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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?
tabstop is offline   Reply With Quote
Old 07-04-2009, 11:32 PM   #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.
JTEK24 is offline   Reply With Quote
Old 07-04-2009, 11:49 PM   #6
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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.
tabstop is offline   Reply With Quote
Old 07-15-2009, 10:15 PM   #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?
JTEK24 is offline   Reply With Quote
Old 07-16-2009, 09:38 AM   #8
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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.
tabstop is offline   Reply With Quote
Old 07-16-2009, 11:53 AM   #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.
JTEK24 is offline   Reply With Quote
Old 07-16-2009, 03:48 PM   #10
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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.
tabstop is offline   Reply With Quote
Old 07-16-2009, 06:05 PM   #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.
JTEK24 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Please help me to convert a simple C++ program into an object-oriented one. zekesteer C++ Programming 1 12-30-2007 10:08 AM
moving median function supermeew C Programming 0 05-04-2006 02:37 PM
Simple Filter 00Sven C Programming 3 03-14-2006 08:46 PM
Computing Mean, Median and Mode Using Arrays Rodneo C++ Programming 0 05-29-2002 11:40 PM
median frank C++ Programming 4 10-28-2001 04:32 PM


All times are GMT -6. The time now is 09:03 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22