[英]Java - Get a matrix of pixel values from an image
Good evening,晚上好,
I'm loading an image in Java using BufferedImage.我正在使用 BufferedImage 在 Java 中加载图像。 I'd to convert this Object in a simple 2D matrix where every cell is an 8 bit pixel value (from 0 to 255).我想将这个 Object 转换为一个简单的二维矩阵,其中每个单元格都是一个 8 位像素值(从 0 到 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).这需要处理灰度图像,也需要处理 RGB 图像(在这种情况下,output 需要 3 个矩阵,每个通道一个,或单个 3D 矩阵)。 This is similar to how MatLab handles images.这类似于 MatLab 处理图像的方式。
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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.