Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Manipulating array of arrays...

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Manipulating array of arrays...

    I am trying to work with this a two dimensional array, that represents greyscale of an image... I want to pixelate the image by averaging 2x2 blocks and replacing each value by the average of the 2x2 grid... for example, if the input is the array

      2,  4, 31, 31   
       3,  3, 21, 41
       1,  2, 10, 20
       3,  2, 20, 30
    then you start by looking at the first 2x2 sub-array (from the top-left)

       2,  4
       3,  3
    and find the average of this sub-array (which is (2+4+3+3)/4 = 3). In the returned array, the first 2x2 sub-array will then be

       3,  3
       3,  3
    We then look at the next 2x2 block

       31,  31
       21,  41
    with average 31, The next 2x2 block in the output will be

       31,  31
       31,  31
    I am wondering what approach would best solve this problem. Not too sure how to solve it... Thanks in advance


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,094
    Thanks
    48
    Thanked 2,152 Times in 2,125 Posts

    Default Re: Manipulating array of arrays...

    Try nested loops and walk through the dimensions by 2 vs by 1
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Manipulating array of arrays...

    but how do i make a loop to go through 2x2 portions of my array of array?

  4. #4
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Manipulating array of arrays...

    Quote Originally Posted by choloboy View Post
    ... what approach would best...
    I wouldn't worry about finding the "best" approach. Just think about an approach.

    For example:
    Suppose you have a two-dimension array of ints named pixelArray.
     
    Declare an int named numRows and set it equal to pixelArray.length;
    Declare an int named numCols and set it equal to pixelArray[0].length;
     
    First of all make sure that numRows and numCols are both even numbers.
    (Use numRows%2 and numCols%2 to determine that.)

    Then make nested loops that go through the rows and columns of pixelArray two at a time:

        for (int i = 0; i < numRows; /* What goes here to make it step through the rows two at a time? */) {
            for (int j = 0; j < numCols; /* What goes here to make it step through the columns two at a time? */) {
                //
                // Do the Good Stuff here
                //
            }
        }

    Now, what is the Good Stuff?

    Declare an int variable named sum, or some such thing.
     
    In the innermost loop, calculate it by adding the values of the
    elements of the 2x2 sub array:
       pixelArray[i][j] +
       pixelArray[i][j+1] +
       pixelArray[i+1][j] +
       pixelArray[i+1][j+1]
     
    Declare an int variable named avg or some such thing
    Set the value of avg equal to Math.round(sum/4.0f) to get the
    int value closest to the average value of the four pixels.
     
    Copy this average value back into the pixelArray elements
    that were used to calculate the sum.



    Cheers!


    Z

Similar Threads

  1. Project - Manipulating ArrayList (Union, Intersect, etc)
    By NewStudent709 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 17th, 2012, 01:56 PM
  2. [SOLVED] Need Help Manipulating 2D Array
    By dansofe0r in forum Object Oriented Programming
    Replies: 8
    Last Post: October 9th, 2012, 09:36 PM
  3. Arrays and array list
    By rob17 in forum Collections and Generics
    Replies: 2
    Last Post: February 19th, 2012, 05:10 PM
  4. [SOLVED] manipulating collections as/from parameter/arguments
    By chronoz13 in forum Collections and Generics
    Replies: 12
    Last Post: October 1st, 2011, 09:05 PM
  5. Help reading data from file, manipulating then rewriting out.
    By Nismoz3255 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: May 19th, 2011, 09:13 AM

Tags for this Thread