如何将图像转换为 java 中的 int[][]?
How to convert an image to int[][] in java?
我加载了一个二进制图像,我想将它转换为二维数组,特别是 int[][]
:
public int[][] ImageToArray(String pathImage) throws IOException {
File file = new File(pathImage);
BufferedImage bufferedImage = ImageIO.read(file);
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
int[][] imageArray = new int[width][height];
return imageArray;}
但是当我 运行 我的源代码时出现异常:
Caused by: javax.imageio.IIOException: Can't read input file!
你能帮帮我吗?
如果你想得到所有的像素点作为二维数组(矩阵),你可以使用:
File file = new File(pathImage); // Be sure to read input file, you have error reading it.
BufferedImage bufferedImage = ImageIO.read(file);
WritableRaster wr = bufferedImage.getRaster();
那么该矩阵的用法就很简单了:
for (int i = 0; i < wr.getWidth(); i++) {
for (int j = 0; j < wr.getHeight(); j++) {
int pixel = wr.getSample(i, j, 0); // the sample in the specified band for the pixel at the specified coordinate.
}
}
还有其他获取和设置像素的方法,请务必阅读文档。
希望这有帮助。
我加载了一个二进制图像,我想将它转换为二维数组,特别是 int[][]
:
public int[][] ImageToArray(String pathImage) throws IOException {
File file = new File(pathImage);
BufferedImage bufferedImage = ImageIO.read(file);
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
int[][] imageArray = new int[width][height];
return imageArray;}
但是当我 运行 我的源代码时出现异常:
Caused by: javax.imageio.IIOException: Can't read input file!
你能帮帮我吗?
如果你想得到所有的像素点作为二维数组(矩阵),你可以使用:
File file = new File(pathImage); // Be sure to read input file, you have error reading it.
BufferedImage bufferedImage = ImageIO.read(file);
WritableRaster wr = bufferedImage.getRaster();
那么该矩阵的用法就很简单了:
for (int i = 0; i < wr.getWidth(); i++) {
for (int j = 0; j < wr.getHeight(); j++) {
int pixel = wr.getSample(i, j, 0); // the sample in the specified band for the pixel at the specified coordinate.
}
}
还有其他获取和设置像素的方法,请务必阅读文档。 希望这有帮助。