Announcement

Collapse
No announcement yet.

C++ programing help

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    C++ programing help

    Im doing an engineering project and havent taken C++ since High School and I need some help with some thing because I am stuck and I have been looking at this for 2 weeks.

    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

    PS- If anyone responds with questions, i will be at work and wont get off till 11 tonight(yeah i gotta work on the 4th) so i wont be able to respond until after i get home.
    Last edited by JTEK24; 07-04-2009, 01:21 PM.
    My CB For Sale

    #2
    Originally posted by JTEK24 View Post
    Im doing an engineering project and havent taken C++ since High School and I need some help with some thing because I am stuck and I have been looking at this for 2 weeks.

    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]
    1. I think you just misstated the project requirement. I'm guessing that for any mxn matrix you want to create a matrix that is (m+1)x(n+1) with 0's surrounding the original mxn matrix (Instead of always a 5x5 matrix). Is this correct?

    If I were trying to do this project, I would.....

    Create some sort of function to read in the size of the original matrix, build a matrix of size (m+1)x(n+1) with all 0's

    Then have a second function that would read the values of the original matrix and write them to the (m+1)x(n+1) position in the new matrix

    Finally, return the new built matrix

    I'm not going to tell you exactly how to do it because that's what you're supposed to learn.

    Originally posted by JTEK24 View Post
    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.
    The way you've worded this is a little confusing. You say that the loop will remain in effect until all center values of the mxn matrix are replaced. I'm a little confused as to what the end point of this loop is (until all center values of the mxn matrix are replaced). Perhaps if you could describe this part again and actually write out all the matrices on paper (or in the thread) it would be easier to help you find a good path.

    Every program I've ever written was done completely on paper then put into the computer. Trust me, it makes things 10000000x easier and forces to learn how things work. Plus, you can't just start throwing out BS that in the end isn't going to work but on the surface looks like it will be very easy.


    Originally posted by JTEK24 View Post
    I was told for loops would work but ive had no luck so far.
    Do you have to use for loops? Like, is that part of the project requirements? If not, just do it however you feel is the easiest way or the way you understand the best. If someone's told you a for loop would do well for this then read up and look at a bunch of examples with for loops until you fully understand them then try to apply those examples to your project.



    P.S. C++ can lick my ballz


    Originally posted by Maple50175
    Oh here we go again. Maples other half.

    Comment


      #3
      Thanks for the comment man, I wasnt trying to get the answer, just pointed in the right direction. we dont have to use for loops but it is possible.

      MATLAB is easier than c++ so thats the program we are using. Ill try to write it out and scan it and up it up so you can see it.
      My CB For Sale

      Comment


        #4
        Yeah, I understand man. Everyone needs a little help getting on the right track. MATLAB is a great program. It gets you into the world of programming without all the nusiances of full fledged C++. Really nice interface, GREAT help section, and fairly robust in terms of applications.

        All that said, I haven't worked with it in like 2 years so I'm not sure if I'd be able to help out too much. Still, it wouldn't hurt to put the code up.

        Good luck with the project man.


        Originally posted by Maple50175
        Oh here we go again. Maples other half.

        Comment


          #5
          I went on this programming forum to try to get some help on there and they are trueling being buttholes about it . Its getting a little frustrating now lol/
          My CB For Sale

          Comment


            #6
            Guys on programming sites usually are a little odd.


            Originally posted by Maple50175
            Oh here we go again. Maples other half.

            Comment


              #7
              Ok i think im on to something, this is what i have so far.
              The matrix size is 768x1024 s my for loop starts like this:

              for i=(1:768)
              for j=(1:1024)
              m(i,j)= ???
              end
              end

              Im not sure what equation(s) will give the median just yet...still working on it. I need a clue lol
              My CB For Sale

              Comment


                #8
                Sorry man, I looked over it and realized I've forgotten quite a bit of my programming rules (oops!) and if I tried to offer any real help I'd probably just get you more confused.

                Hopefully a programming specific website would be able to get you beter results.


                Originally posted by Maple50175
                Oh here we go again. Maples other half.

                Comment


                  #9
                  MATLAB is going to be MUCH easier to use than C++ because of all the built in matrix handling stuff.

                  So Tnwagn was on the right track about setting up a matrix of zeros to start with that is M + 2 tall and N + 2 wide. This just makes it easier to process the edge of the picture (this is for image processing yeah?). Matlab has a function to do this very easily. Something like zeroes().

                  Then you're going to want to insert the original values into this new matrix. Remember you're starting at position (2,2). This is where something like that code snippet of nested for loops you posted comes into it.

                  Then you want to run the median filter over this new matrix. So most likely you'll have a function to perform the filter. You'll probably want another set of nested for loops to run the filter over the picture. You'll have to mess with the dimensions of the loops though.

                  If you have a 3x3 window, for every pixel (x,y) you want to extract a 3 x 3 matrix of [(x-1,y-1),(x,y-1),(x+1,Y-1),(x-1,y),(x,y),(x+1,y),(x-1,y+1),(x,y+1),(x+1,y+1)]

                  Then you need to find the median of this and insert it into another new matrix. You don't want to overwrite your original matrix with the new values (I think).

                  Matlab probably has simple ways to extract a 3x3 matrix and probably has a median function. Both of which will make things easier.

                  Hope this helps without giving too much away.

                  edit: With Matlab you can probably run a median function over the entire original picture. You can use the result of this to check if yours is right. Doubt you'll get any marks if you just hand that in though
                  Last edited by Cam; 07-07-2009, 01:05 AM.
                  Members Ride Thread

                  Comment


                    #10
                    How high of a C was it? I thought they didn't go any higher than a C+, you must have been extremely close to a B.

                    Comment


                      #11
                      Thanks Cam, we need all the help we can get. I kinda get what you are talking about. See the thing is I have never used Matlab at all. I just took C++ a few years ago. I am going to read through your post thoroughly to get the idea. I might have questions later. Oh yeah we cant use Matlab's medianfilt function(s). thats why we have to create the median filter.

                      The picture has its own number values for the 768x1024 and we have to run that 3x3 filter over the whole picture (number values in the matrix). Yeah its image processing...you know your stuff lol
                      Last edited by JTEK24; 07-07-2009, 10:40 PM.
                      My CB For Sale

                      Comment


                        #12
                        i used matlab for 2 weeks in class then never used it again.

                        PSN: DJ-dlatino
                        My ride
                        http://www.cb7tuner.com/vbb/showthread.php?t=140370

                        Comment


                          #13
                          I put this code in:

                          x=1
                          y=1


                          kernel=[(x-1),(y-1) x,(y-1) (x+1),(y-1);
                          (x-1),y x,y (x+1),y;
                          (x-1),(y+1) x,(y+1) (x+1),(y+1)]

                          and got this output:


                          x =

                          1

                          y =

                          1


                          kernel =

                          0 0 1 0 2 0
                          0 1 1 1 2 1
                          0 2 1 2 2 2

                          so I think we see how that works but the function...i dont know.
                          Last edited by JTEK24; 07-08-2009, 12:02 AM.
                          My CB For Sale

                          Comment


                            #14
                            Originally posted by tunerman View Post
                            How high of a C was it? I thought they didn't go any higher than a C+, you must have been extremely close to a B.
                            HAHA

                            CrzyTuning now offering port services

                            Comment


                              #15
                              You're on the wrong track a little bit. That bit I put with the x-1,y etc are the matrix indeces that you want to pull the numbers from to form the 3 x 3 window that you will then perform the median function on. So if you had the start of the picture as:

                              0 0 0 0
                              0 1 3 2
                              0 4 5 3
                              0 6 3 2

                              You would run the window over the whole picture starting at index x=2, y=2. So to extract your 3 x 3 window you need to grab the number from indeces originalPicture(x-1,y-1), originalPicture(x-1, y), .....etc like above.

                              That will give you a 3 x 3 of:

                              0 0 0
                              0 1 3
                              0 4 5

                              Then you get the median of this and put it into position (x,y) in a whole new matrix of the same size as the original.

                              Then you do all this again for position (2,3)....extract the window, get the median, put it in the new matrix etc...
                              Members Ride Thread

                              Comment

                              Working...
                              X