Split a sheet of images into an array

• August 8th, 2011, 10:58 AM
pajfilms
Split a sheet of images into an array
...
• August 8th, 2011, 11:53 AM
Norm
Re: Split a sheet of images into an array
Quote:

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.
• August 8th, 2011, 12:42 PM
pajfilms
Re: Split a sheet of images into an array
...
• August 8th, 2011, 12:49 PM
Norm
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.
• August 8th, 2011, 01:51 PM
pajfilms
Re: Split a sheet of images into an array
...
• August 8th, 2011, 01:56 PM
Norm
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
}
}
• August 8th, 2011, 04:41 PM
pajfilms
Re: Split a sheet of images into an array
...
• August 8th, 2011, 04:46 PM
Norm
Re: Split a sheet of images into an array
Quote:

I still can't get it to work.
Please post your code for copying the 2 dim array to a 1 dim array.
• August 8th, 2011, 04:49 PM
pajfilms
Re: Split a sheet of images into an array
...
• August 8th, 2011, 04:52 PM
Norm
Re: Split a sheet of images into an array
Quote:

all my attempts resulted in an error
When you get errors, please copy and paste the full text here.
• August 8th, 2011, 05:04 PM
pajfilms
Re: Split a sheet of images into an array
...
• August 8th, 2011, 05:10 PM
Norm
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.
• August 8th, 2011, 05:19 PM
pajfilms
Re: Split a sheet of images into an array
...
• August 8th, 2011, 05:23 PM
Norm
Re: Split a sheet of images into an array
Quote:

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:
Code :

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]

• August 8th, 2011, 05:33 PM
pajfilms
Re: Split a sheet of images into an array
...
• August 8th, 2011, 05:38 PM
Norm
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
Code :

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];

• August 8th, 2011, 05:46 PM
pajfilms
Re: Split a sheet of images into an array
...
• August 8th, 2011, 05:55 PM
Norm
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.