java - 如何在 Java 中将 HeatMap 的 NA 值表示为空像素(白色)?

标签 java bufferedimage heatmap graphics2d drawimage

我正在使用 Java HeatMap 库 ( http://www.mbeckler.org/heatMap/ ) 为我的数据生成 heatMap。 我正在使用具有 NA 值的数据集。所以,基本上我不希望具有 NA 值的像素有颜色(白色)。但是,不幸的是这个库不支持具有 NA 值的数据,我得到的是具有基色的计划图像 block 。我尝试查看源代码,以便进行一些更改。在代码中,drawData() 方法用于为 bufferedImage 中的每个像素着色(可能!)。有人可以帮助我如何实现对 NA 值的支持并以无颜色的方式显示它们吗?我对 BufferedImage 和 Graphics2D 类几乎没有经验。

这是库源代码中的 drawData() 方法:

/**
     * Creates a BufferedImage of the actual data plot.
     *
     * After doing some profiling, it was discovered that 90% of the drawing
     * time was spend drawing the actual data (not on the axes or tick marks).
     * Since the Graphics2D has a drawImage method that can do scaling, we are
     * using that instead of scaling it ourselves. We only need to draw the
     * data into the bufferedImage on startup, or if the data or gradient
     * changes. This saves us an enormous amount of time. Thanks to
     * Josh Hayes-Sheen (grey@grevian.org) for the suggestion and initial code
     * to use the BufferedImage technique.
     *
     * Since the scaling of the data plot will be handled by the drawImage in
     * paintComponent, we take the easy way out and draw our bufferedImage with
     * 1 pixel per data point. Too bad there isn't a setPixel method in the
     * Graphics2D class, it seems a bit silly to fill a rectangle just to set a
     * single pixel...
     *
     * This function should be called whenever the data or the gradient changes.
     */
    private void drawData()
    {

      //  System.out.println("Column: " + data.length + " row: " + data[0].length);
        bufferedImage = new BufferedImage(data.length,data[0].length, BufferedImage.TYPE_INT_ARGB);
        bufferedGraphics = bufferedImage.createGraphics();

        for (int x = 0; x < data.length; x++)
        {
            for (int y = 0; y < data[0].length; y++)
            {
                bufferedGraphics.setColor(colors[dataColorIndices[x][y]]);
                bufferedGraphics.fillRect(x, y, 1, 1);
            }
        }
    }

样本数据可以在以下位置找到:http://www.filedropper.com/data_13

所以,这就是目前的样子:

来自 Java:

enter image description here

来自 R:

enter image description here

请忽略两个图像的方向

最佳答案

由于 BufferedImage 是作为 ARGB 创建的,因此您不能在那些应该未定义的像素上绘制任何内容,并且它们将保持透明。类似于:

public static int NA = ...;

private void drawData()
{
    bufferedImage = new BufferedImage(data.length,data[0].length, BufferedImage.TYPE_INT_ARGB);
    Graphics2D bufferedGraphics = bufferedImage.createGraphics();
    try {
        for (int x = 0; x < data.length; x++)
        {
            for (int y = 0; y < data[0].length; y++)
            {
                int colorIndex = dataColorIndices[x][y];
                if (colorIndex != NA) {
                    bufferedGraphics.setColor(colors[colorIndex]);
                    bufferedGraphics.fillRect(x, y, 1, 1);
                }
// Alternate flow, if you really want the pixels to be white
//                else {
//                    bufferedGraphics.setColor(Color.WHITE);
//                    bufferedGraphics.fillRect(x, y, 1, 1);
//                }
            }
        }
    }
    finally {
        bufferedGraphics.dispose();
    }
}

我还处理了 Graphics2D 实例,以避免资源泄漏。

关于java - 如何在 Java 中将 HeatMap 的 NA 值表示为空像素(白色)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32813099/

相关文章:

java - 截图

python - 如何从图像上具有不同值的不同像素的坐标数据框创建热图

python - 如何将 pandas 数据框转换为热图兼容数据框?

java - 按字母顺序对 Java 对象排序为 JSON 对象

java - 我应该用什么来爬取许多新闻文章?

java - GRPC Java 将数据从服务器拦截器传递给 rpc 服务调用

java - 为什么需要抽象方法?

java - 如何将缓冲图像中的 IndexColorModel 设置为具有某些颜色和一种透明?

java - 如何将缓冲图像转换为图像,反之亦然?

r - 创建格状(多面)薄板样条响应曲面