gpt4 book ai didi

java - 将二维 double 组转换为 BufferedImage

转载 作者:行者123 更新时间:2023-12-02 02:31:12 24 4
gpt4 key购买 nike

我有一个二维 double 组,它是图像的过滤值。我想将此数组转换回 BufferedImage。如何将 double[][] 转换为 BufferedImage

BufferedImage b = new BufferedImage(arr.length, arr[0].length, 3); 
Graphics c = b.getGraphics();


PrintWriter writer = new PrintWriter("the-file-name.txt", "UTF-8");
for(int i=0; i< arr.length; i++){
for (int j=0; j<arr[0].length; j++){
c.drawString(String.valueOf(arr[i][j]), i, j);
writer.print(arr[i][j]+" \t");
}
writer.println();
}

ImageIO.write(b, "jpg", new File("CustomImage.jpg"));
System.out.println("end");

当我使用 imshow 在 matlab 中绘制 file-name.txt 时,我可以看到过滤后的图像。但是 CustomImage.jpg 仅包含一种颜色。知道为什么吗?

c.drawString(String.valueOf(arr[i][j]), i, j) 的结果: enter image description here

c.drawString(String.valueOf(arr[i][j]), 0+(i*10), 0+(j*10)): enter image description here

Matlab plor arr 的 double ,首先是数组的 double ,其次是初始灰度图像: enter image description here

最佳答案

你的代码

BufferedImage b = new BufferedImage(arr.length, arr[0].length, 3); 
Graphics c = b.getGraphics();

for(int i = 0; i<arr.length; i++) {
for(int j = 0; j<arr[0].length; j++) {
c.drawString(String.valueOf(arr[i][j]), 0+(i*10), 0+(i*10));
}
}
ImageIO.write(b, "Doublearray", new File("Doublearray.jpg"));
System.out.println("end");

重构后

int xLenght = arr.length;
int yLength = arr[0].length;
BufferedImage b = new BufferedImage(xLenght, yLength, 3);

for(int x = 0; x < xLenght; x++) {
for(int y = 0; y < yLength; y++) {
int rgb = (int)arr[x][y]<<16 | (int)arr[x][y] << 8 | (int)arr[x][y]
b.setRGB(x, y, rgb);
}
}
ImageIO.write(b, "Doublearray", new File("Doublearray.jpg"));
System.out.println("end");

关于java - 将二维 double 组转换为 BufferedImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21576096/

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