gpt4 book ai didi

java 图像转换为矩阵

转载 作者:行者123 更新时间:2023-11-30 07:14:43 25 4
gpt4 key购买 nike

有一个非常简单的 .jpg 图像,我想将其转换为矩阵。但是使用 getRGB(i,j) 指向像素会给出 ArrayIndexOutOfBounds 的运行时异常。以下代码对图像大小有限制吗?它只是显示整个图像中获得的第一个颜色代码。

BufferedImage img=ImageIO.read(new File("stars.jpg"));
int pix[][]= new int[img.getHeight()][img.getWidth()];

然后我应用了一个嵌套的 for 循环并使用了

pix[i][j]= img.getRGB(i,j);

最佳答案

这个函数完全符合你提到的功能,并且对我来说效果很好。我相信您可能在嵌套 for 循环中做错了什么。

 public int[][] getMatrixOfImage(BufferedImage bufferedImage) {
int width = bufferedImage.getWidth(null);
int height = bufferedImage.getHeight(null);
int[][] pixels = new int[width][height];
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
pixels[i][j] = bufferedImage.getRGB(i, j);
}
}

return pixels;
}

关于java 图像转换为矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38627006/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com