I have a 6*6 2d array called pixels
int [][] pixels = { {1,2,7,9,4,11},
{3,4,6,6,12,12},
{4,9,15,14,9,9},
{10,10,20,18,8,8},
{4,3,17,16,1,4},
{4,5,18,18,5,6}
};
and I want to store its data into 9 2d arrays of size 2*2 like this
int [][] array1 = {{1,2},
{3,4}
};
int [][] array2 = {{7,9},
{6,6}
};
int [][] array3 = {{4,11},
{12,12}
};
and like this, I have another 2d array of size 320 * 320 which I want to store its data in 6400 2d arrays of size 44 and I don't know what is the dynamic to do. my for loops aren't right. Can anyone help? edit: I tried this code and it gives me the right result regrading the first array of the 2d array but I'm not sure about the second one. vectorHeight is 4. the big 2d array size is 320 320 and the smaller ones size is 4 * 4
for(int j=0; j < 320 ; j++){
for(int i=0; i< 320 ; i++){
int[][] array = new int[][]{
{pixels2[j * vectorHeight][i * vectorHeight], pixels2[j * vectorHeight][(i * vectorHeight) + 1],pixels2[j * vectorHeight][(i * vectorHeight) + 2],pixels2[j * vectorHeight][(i * vectorHeight) + 3]},
{pixels2[(j * vectorHeight) + 1][i * vectorHeight], pixels2[(j * vectorHeight) + 1][(i * vectorHeight) + 1],pixels2[(j * vectorHeight) + 2][(i * vectorHeight) + 2],pixels2[(j * vectorHeight) + 3][(i * vectorHeight) + 3]},
};
System.out.println(Arrays.deepToString(array));
}
}
edit2: the 320 * 320 2d array is a result of a function that takes an image and return the pixels vector so this is the code for reading an image:
public static int[][] readImage(String filePath) {
File file = new File(filePath);
BufferedImage image = null;
try {
image = ImageIO.read(file);
} catch (IOException e) {
e.printStackTrace();
return null;
}
int width = image.getWidth();
int height = image.getHeight();
int[][] pixels = new int[height][width];
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
int p = image.getRGB(x, y);
int a = (p >> 24) & 0xff;
int r = (p >> 16) & 0xff;
int g = (p >> 8) & 0xff;
int b = p & 0xff;
pixels[y][x] = r;
p = (a << 24) | (r << 16) | (g << 8) | b;
image.setRGB(x, y, p);
}
}
return pixels;
}
and the image that I am currently working on is this one https://www.researchgate.net/profile/Ayman-Al-Dmour/publication/304496637/figure/fig1/AS:377628822392833@1467045135613/Test-images_Q320.jpg the rest of the code is a function that takes an image, a vector size - height and weight- and eventually its suppose to compress it using vector quantization
int [][] pixels = readImage(filePath);
int numofVectors1 = (pixels.length * pixels.length) / (vectorHeight * vectorWidth);
ArrayList<int[][]> vectors = new ArrayList<>(numofVectors1);
for (int j = 0; j < pixels.length; j++) {
for (int i = 0; i < pixels.length; i++) {
int[][] array = new int[][]{
{pixels[j * vectorHeight][i * vectorHeight], pixels[j * vectorHeight][(i * vectorHeight) + 1], pixels[j * vectorHeight][(i * vectorHeight) + 2], pixels[j * vectorHeight][(i * vectorHeight) + 3]},
{pixels[(j * vectorHeight) + 1][i * vectorHeight], pixels[(j * vectorHeight) + 1][(i * vectorHeight) + 1], pixels[(j * vectorHeight) + 1][(i * vectorHeight) + 2], pixels[(j * vectorHeight) + 1][(i * vectorHeight) + 3]},
};
vectors.add(array);
}
}