DeepLearning4j - INDArray 转为图像

三通通道彩色图

代码引自:https://github.com/sjsdfg/dl4j-tutorials/blob/master/src/main/java/styletransfer/NeuralStyleTransfer.java

 /**
 * Takes an INDArray containing an image loaded using the native image loader
 * libraries associated with DL4J, and converts it into a BufferedImage.
 * The INDArray contains the color values split up across three channels (RGB)
 * and in the integer range 0-255.
 *
 * @param array INDArray containing an image
 * @return BufferedImage
 */
private BufferedImage imageFromINDArray(INDArray array) {
    long[] shape = array.shape();

    long height = shape[2];
    long width = shape[3];
    BufferedImage image = new BufferedImage((int)width, (int)height, BufferedImage.TYPE_INT_RGB);
    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            int red = array.getInt(0, 2, y, x);
            int green = array.getInt(0, 1, y, x);
            int blue = array.getInt(0, 0, y, x);

            //handle out of bounds pixel values
            red = Math.min(red, 255);
            green = Math.min(green, 255);
            blue = Math.min(blue, 255);

            red = Math.max(red, 0);
            green = Math.max(green, 0);
            blue = Math.max(blue, 0);
            image.setRGB(x, y, new Color(red, green, blue).getRGB());
        }
    }
    return image;
}

单通道灰度图

代码引自:https://github.com/sjsdfg/dl4j-tutorials/blob/master/src/main/java/lesson6/UsingModelToPredict.java

/**
 * 将单通道的 INDArray 保存为灰度图
 *
 * There's also NativeImageLoader.asMat(INDArray) and we can then use OpenCV to save it as an image file.
 *
 * @param array 输入
 * @return 灰度图转化
 */
private static BufferedImage imageFromINDArray(INDArray array) {
    long[] shape = array.shape();

    int height = (int)shape[2];
    int width = (int)shape[3];
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            int gray = array.getInt(0, 0, y, x);

            // handle out of bounds pixel values
            gray = Math.min(gray, 255);
            gray = Math.max(gray, 0);

            image.getRaster().setSample(x, y, 0, gray);
        }
    }
    return image;
}

Java2DNativeImageLoader

文档地址:https://deeplearning4j.org/api/latest/org/datavec/image/loader/Java2DNativeImageLoader.html

new Java2DNativeImageLoader().asBufferedImage(array);

保存图片到本地

private void saveImage(INDArray combination, int iteration) throws IOException {
    IMAGE_PRE_PROCESSOR.revertFeatures(combination);

    BufferedImage output = imageFromINDArray(combination);
    URL resource = getClass().getResource(OUTPUT_PATH);
    File file = new File(resource.getPath() + "/iteration" + iteration + ".jpg");
    ImageIO.write(output, "jpg", file);
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • jHipster - 微服务搭建 CC_简书[//www.greatytc.com/u/be0d56c4...
    quanjj阅读 843评论 0 2
  • 今日践行: 1、听读古诗《忆江南 2、读绘本《汉娜的惊喜》 3、点读英语1本 晚上,让孩子挑他喜欢的事情做,他...
    智慧拼搏阅读 194评论 0 0
  • 我有一个同桌,她的名字很亲切,很好听。她的名字叫韩雨,他的变化,跟班级的变化很像,很像。 我第...
    王明亮_fcc0阅读 334评论 5 1
  • 湘江北去 她漠然落笔,像是一尘了如故,丝毫不关乎。那割破的脉搏,像是流云行霜,止不住的,是鲜血。 一月添香,二月夜...
    时无一阅读 1,844评论 30 22