【问题标题】:Convert 2D binary matrix to black/white image in java在java中将二维二进制矩阵转换为黑白图像
【发布时间】:2013-12-17 18:23:18
【问题描述】:

我是 Java 新手。我现在只有 1 和 0 的 2D 二进制矩阵。我想将其保存为具有相同宽度和高度的 jpg 图像(黑白)。我怎么能意识到这一点?我尝试了下面的代码但失败了,说“java.lang.IllegalArgumentException:image == null!”请帮助我或给我更好的解决方案。非常感谢。

public static void main(String[] args) throws IOException {

    //result is double[25][33] binary matrix with only 1s and 0s;
    int height=result.length;
    int width=result[0].length;;
    byte[] data = new byte[height*width];
    int k=0;
    for(int i = 0;i < height;i++){
        for(int j = 0; j < width; j++){
            data[k]=(byte)result[i][j];
            k++;
        }
        System.out.print("\n");
    }
    InputStream input = new ByteArrayInputStream(data);
    BufferedImage output = ImageIO.read(input);
    ImageIO.write(ouput, "jpg", new File("c:/result.jpg"));

}

【问题讨论】:

  • “我怎么能意识到这一点?” 我怀疑一种方法是将每个像素绘制到BufferedImage.TYPE_BYTE_BINARY 并保存。但不是作为 JPG,它不能准确地保留颜色。除了怀疑之外,请发布SSCCE(它只需要一些导入,并将其包装在class中)。

标签: java image matrix binary


【解决方案1】:

这是一个创建 30x30 方格框的简单示例:

public static void main(String... args) throws IOException {
    int w = 30, h = 30;

    // create the binary mapping
    byte BLACK = (byte)0, WHITE = (byte)255;
    byte[] map = {BLACK, WHITE};
    IndexColorModel icm = new IndexColorModel(1, map.length, map, map, map);

    // create checkered data
    int[] data = new int[w*h];
    for(int i=0; i<w; i++)
        for(int j=0; j<h; j++)
            data[i*h + j] = i%4<2 && j%4<2 || i%4>=2 && j%4>=2 ? BLACK:WHITE;

    // create image from color model and data
    WritableRaster raster = icm.createCompatibleWritableRaster(w, h);
    raster.setPixels(0, 0, w, h, data);
    BufferedImage bi = new BufferedImage(icm, raster, false, null);

    // output to a file
    ImageIO.write(bi, "jpg", new File("C:\\Users\\user\\Desktop\\test.jpg"));
}

编辑:

您实际上不需要创建自己的 ImageColorModel,您可以使用内置类型:BufferedImage.TYPE_BYTE_GRAY 或 TYPE_BYTE_BINARY。这是一个更好的例子,展示了如何使用灰度来获得方格:

public static void main(String... args) throws IOException {
    int w = 40, h = 40, divs = 5;

    BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY);
    WritableRaster raster = bi.getRaster();

    for(int i=0; i<w; i++)
        for(int j=0; j<h; j++)
            raster.setSample(i,j,0,128+(int)(127*Math.sin(Math.PI*i/w*divs)*Math.sin(Math.PI*j/h*divs)));

    ImageIO.write(bi, "jpg", new File("C:\\Users\\user\\Desktop\\test.jpg"));
}

【讨论】:

  • 感谢您的 cmets,但是如果我想将矩阵输出到 0 到 255 的图像范围内怎么办?是这样写的吗: byte[] map = new byte[256]; for(int i=0;i
【解决方案2】:

JHeatChart 也可以完成这项工作,而无需您创建自定义图像库。

http://www.javaheatmap.com/

// Create some dummy data.
double[][] data = new double[][]{{3,2,3,4,5,6},
                                 {2,3,4,5,6,7},
                                 {3,4,5,6,7,6},
                                 {4,5,6,7,6,5}};

// Step 1: Create our heat map chart using our data.
HeatChart map = new HeatChart(data);

// Step 2: Customise the chart.
map.setTitle("This is my heat chart title");
map.setXAxisLabel("X Axis");
map.setYAxisLabel("Y Axis");

// Step 3: Output the chart to a file.
map.saveToFile(new File("java-heat-chart.png"));

您实际上要做的是制作热图。而不是从 0 到任何值的范围,而是 0 和 1 的范围。

double[][] data = new double[][](//etc); 替换为您的布尔数组。

【讨论】:

    猜你喜欢
    • 2011-03-17
    • 2019-05-22
    • 2017-04-16
    • 1970-01-01
    • 2019-05-02
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 2015-06-01
    相关资源
    最近更新 更多