I am trying to group 8x8 blocks of pixel data that I retrieve from BufferedImage.
jpegPixels
is a 3D int array with height, width, and an RGB size (0=Red, 1=Green, 2=Blue).pixelMatrix
is a 2D int array with 8x8 block sizeSo far the code works because I get a resultant image back out. Below is a partial sample of the code that groups pixels into 8x8 (it only shows 2 rows because code is too long).
int [][][] jpegPixels = new int[h][w][3]; //h = height size, w = width size
int [][] pixelMatrix = new int [8][8];
for(int i=6; i<jpegPixels.length-7; i++)
{
for(int j=6; j<jpegPixels[0].length-7; j++)
{
for(int x=0; x<2; x++)
{
//************ ROW 1 ******************
//********** Pixel 1 ********************
pixelMatrix[0][0] = jpegPixels[i-1][j-1][x];
//********** Pixel 2 ********************
pixelMatrix[0][1] = jpegPixels[i-1][j][x];
//********** Pixel 3 ********************
pixelMatrix[0][2] = jpegPixels[i-1][j+1][x];
//********** Pixel 4 ********************
pixelMatrix[0][3] = jpegPixels[i-1][j+2][x];
//********** Pixel 5 ********************
pixelMatrix[0][4] = jpegPixels[i-1][j+3][x];
//********** Pixel 6 ********************
pixelMatrix[0][5] = jpegPixels[i-1][j+4][x];
//********** Pixel 7 ********************
pixelMatrix[0][6] = jpegPixels[i-1][j+5][x];
//********** Pixel 8 ********************
pixelMatrix[0][7] = jpegPixels[i-1][j+6][x];
//************ ROW 2 ******************
//********** Pixel 9 ********************
pixelMatrix[1][0] = jpegPixels[i][j-1][x];
//********** Pixel 10 ********************
pixelMatrix[1][1] = jpegPixels[i][j][x];
//********** Pixel 11 ********************
pixelMatrix[1][2] = jpegPixels[i][j+1][x];
//********** Pixel 12 ********************
pixelMatrix[1][3] = jpegPixels[i][j+2][x];
//********** Pixel 13 ********************
pixelMatrix[1][4] = jpegPixels[i][j+3][x];
//********** Pixel 14 ********************
pixelMatrix[1][5] = jpegPixels[i][j+4][x];
//********** Pixel 15 ********************
pixelMatrix[1][6] = jpegPixels[i][j+5][x];
//********** Pixel 16 ********************
pixelMatrix[1][7] = jpegPixels[i][j+6][x];
//********** Pixel 17 - 64 ********************
// ******* Too long to print the rest ********
}
}
}
Can someone identify is this the correct way of grouping values into 8x8 blocks of a 3D array of h x w size? The reason is, is that when I test print out values of the original pixels and resultant pixels, I am getting values from the resultant pixels from a higher ofset?
For e.g.: Original pixels - 2234445556788, Resultant pixels - 0000000002234445556788
Hope this is clear. Thanks
I'm not sure how your pixels are being represented in your example. So I chose to use the way that a pixel is represented by the BufferedImage class as per this tutorial. If you need to change the way the pixels are being represented as ints you can edit the getIntFromPixel method.
private int getIntFromPixel(int[] pixel)
{
int r = pixel[0];
int g = pixel[1];
int b = pixel[2];
return (r << 16) | (g << 8) | b;
}
// Returns the sub array with upper-left corner at [start_i][start_j] with
// width sub_w and height sub_h.
public int[][] getSubArray(int[][][] pixelArray, int start_i, int start_j
, int sub_w, int sub_h)
{
int[][] subArray = new int[sub_w][sub_h];
for (int i = 0; i < sub_w; i++) {
for (int j = 0; j < sub_h ; j++) {
subArray[i][j] = getIntFromPixel(pixelArray[start_i + i][start_j + j]);
}
}
return subArray;
}
Now for example given a 3x3 array
pixelArray = {
{{1,1,1}, {2,2,2}, {3,3,3}},
{{4,4,4}, {5,5,5}, {6,6,6}},
{{7,7,7}, {8,8,8}, {9,9,9}}
}
If we want to get the 2x2 pixel sub-array at [0][0]:
{{{1,1,1}, {2,2,2}}, {{4,4,4}, {5,5,5}}}
you can call
getSubArray(pixel, 0, 0, 2, 2);
Which returns the array
{{65793, 131586}, {263172, 328965}}
In your loop in the inner loop the pixelMatrix is being over written each time the loop iterates meaning that the contents of pixelMatrix will be the b value of the pixel.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments