Search code examples
javaimage-processingmultidimensional-arrayrgbpixel

Storing the color values of each pixel into a 2D array


Is there a way to store the color of the pixel in the row and the column of an image into a 2D array.

I currently have this

 for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            int c = image1.getRGB(i, j);
     }
    }

But what I want to do is have c be like c[I][j] but getRGB does not work with arrays.


Solution

  • Sure it works.

    int[][] c = new int[w][h]
    
     for (int i = 0; i < w; i++) {
            for (int j = 0; j < h; j++) {
                c[i][j] = image1.getRGB(i, j);
         }
     }
    

    Also, remember to switch height and width. Width should be the outer loop and height should be the inner one.