简体   繁体   中英

Java - Get a matrix of pixel values from an image

Good evening,

I'm loading an image in Java using BufferedImage. I'd to convert this Object in a simple 2D matrix where every cell is an 8 bit pixel value (from 0 to 255). This needs to work with grayscale images but also with RGB images (in this case the output needs to be 3 matrixes, one for each channel, or a single 3D matrix). This is similar to how MatLab handles images.

Anyone that can help me?

Thank you

You could try something like this:

BufferedImage bf = //Assuming you have a buffered image
int[][] R = new int[bf.getWidth()][bf.getHeight()];
int[][] G = //Same as for R
int[][] B = //Same as for R

for(int r = 0; r < bf.getWidth(); r++)
{
     for(int c = 0; c < bf.getHeight() c++)
     {
           //Uses the Java color class to do the conversion from int to RGB
           Color temp = new Color(bf.getRGB(r, c));
           R[r][c] = temp.getRed();
           G[r][c] = temp.getGreen();
           B[r][c] = temp.getBlue();
     }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM