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 18 of 18

Thread: Split a sheet of images into an array

  1. #1
    Member
    Join Date
    Jun 2011
    Posts
    30
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Split a sheet of images into an array

    ...


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Split a sheet of images into an array

    to split an image into an array of images
    Can you describe how the images that are to go in the array are created from the original image?
    What are the pixel boundaries for each image?
    Can you write a loop that changes the x and/or y pixel values for each image that is to be created?
    The code sample shows you how to create a buffered image and put it in an array and get a Graphics object for drawing on it.
    Then use the drawImage() method to draw that part of the source image into the next image you want to save in the array.

  3. #3
    Member
    Join Date
    Jun 2011
    Posts
    30
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Split a sheet of images into an array

    ...

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Split a sheet of images into an array

    Are you saying your problem is moving elements from a two dim array to a one dim array?

    Take a piece of paper, draw the 2 dim array as you have above. Mark each element in the two dim array with its row, col position: 0,0 thru 2,5. Then draw the one dim array and mark on it the row, col values for its souce in the 2 dim array. For example a = 0,0 b = 0,1 ... g = 1,0 etc
    Looking at those numbers should allow you to copy the contents from the 2 dim to the one dim

    The trick could be to use nested loops going over the rows and columns for the input and by having another index for the output array that you manually increment.

  5. #5
    Member
    Join Date
    Jun 2011
    Posts
    30
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Split a sheet of images into an array

    ...

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Split a sheet of images into an array

    Make a nested loop for the two dimensions, i for first and j for second.
    Have another index for the target array:
    .... here the nested for loops
    targetArray[idx++] = sourceArray[i][j]; // Copy element and increment the index to one dim
    }
    }

  7. #7
    Member
    Join Date
    Jun 2011
    Posts
    30
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Split a sheet of images into an array

    ...

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Split a sheet of images into an array

    I still can't get it to work.
    Please post your code for copying the 2 dim array to a 1 dim array.

  9. #9
    Member
    Join Date
    Jun 2011
    Posts
    30
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Split a sheet of images into an array

    ...

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Split a sheet of images into an array

    all my attempts resulted in an error
    When you get errors, please copy and paste the full text here.

  11. #11
    Member
    Join Date
    Jun 2011
    Posts
    30
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Split a sheet of images into an array

    ...
    Last edited by pajfilms; August 8th, 2011 at 05:08 PM.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Split a sheet of images into an array

    have you worked out the x,y positions for the output and compared it to the desired?
    a starts at 0,0 and goes to 0
    e starts at 1,0 and goes to 1 this should be 4
    i starts at 2,0 and goes to 2 this should be 8
    b starts at 0,1 and goes to 3 this should be 1

    There is a relationship above between the x,y and the z. You need to find the expression that generates the z value (1 dim index) from the x,y 2 dim indexes.

  13. #13
    Member
    Join Date
    Jun 2011
    Posts
    30
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Split a sheet of images into an array

    ...

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Split a sheet of images into an array

    I did reverse the indexes
    No that was wrong. I deleted it just after I posted it.
    Are you sure the elements are in the order you show? And not like this?
    a d g
    b e h
    c f i

    Here is code for 2 dim to 1 dim:
       int[][] twoDim = {{1,2,3},{4,5,6}, {7,8,9}};  // Input 2 dim array
       int[] oneDim = new int[9];                          // Output to 1 dim array
       int ix = 0;   // index to the one dim array
       for (int i=0; i < twoDim.length; i++) {
          for (int j=0; j < twoDim[i].length; j++) {
            oneDim[ix++] = twoDim[i][j];               // copy from 2 dim to 1 dim
          }
        }
        System.out.println("oneDim=" + Arrays.toString(oneDim)); // oneDim=[1, 2, 3, 4, 5, 6, 7, 8, 9]
    Last edited by Norm; August 8th, 2011 at 05:25 PM.

  15. #15
    Member
    Join Date
    Jun 2011
    Posts
    30
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Split a sheet of images into an array

    ...

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Split a sheet of images into an array

    Is it working?
    If you take your image and draw a grid on it and label the images with letters, where is the image b relative to image a, on the same row or in the same column?


    Here I can get output similar to when you get it in the wrong order
          int[][] twoDim = {{1,2,3},{4,5,6}, {7,8,9}};
          int[] oneDim = new int[9];
          for (int i=0; i < twoDim.length; i++) {
            for (int j=0; j < twoDim[i].length; j++) {
              oneDim[j*3+i] = twoDim[i][j];    // NB: hardcoded 3 here
            }
          }
          System.out.println("oneDim=" + Arrays.toString(oneDim)); 
          // oneDim=[1, 2, 3, 4, 5, 6, 7, 8, 9]    using oneDim[i*3+j] = twoDim[i][j];
          // oneDim=[1, 4, 7, 2, 5, 8, 3, 6, 9]    using oneDim[j*3+i] = twoDim[i][j];
    Last edited by Norm; August 8th, 2011 at 05:42 PM.

  17. #17
    Member
    Join Date
    Jun 2011
    Posts
    30
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Split a sheet of images into an array

    ...
    Last edited by pajfilms; December 25th, 2012 at 10:23 AM. Reason: I wrote "letters" instead of "errors"

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Split a sheet of images into an array

    Look at the end of my last post. One version of the code ordered the output the same as you show.
    By column vs by row.

Similar Threads

  1. Java split problem..
    By arch in forum Java SE APIs
    Replies: 5
    Last Post: August 11th, 2011, 07:48 AM
  2. Help me split and then join a file
    By babbupandey in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 13th, 2010, 12:20 PM
  3. Thread Hangs at split method.
    By kailasvilaskore in forum Threads
    Replies: 1
    Last Post: November 26th, 2010, 12:13 PM
  4. MVC - Split the Java servlet(help needed)
    By kamweshi in forum Java Servlet
    Replies: 1
    Last Post: October 18th, 2010, 09:02 AM
  5. Alphabet from sprite sheet
    By Asido in forum Java Theory & Questions
    Replies: 2
    Last Post: September 18th, 2010, 11:49 AM