【JAVA学习】java中读取图片并转换为RGB三维数组

该博客介绍了如何在Java中读取图片并将其转换为适合深度学习输入的四维RGB数组。代码包括读取BufferedImage图像和将图像数据转化为[1, width, height, 3]格式的四维数组。适用于将Python训练的模型在Java环境中部署。
摘要由CSDN通过智能技术生成

【JAVA学习】java中读取图片并转换为RGB三维数组

我们在python代码中通常读取到的图像就是RGB数组了,通常为[width,height,channel],而用于深度学习训练过程中,通常还需要设置图像数量,也就是batch_size,为此,输入网络的数组是:[batch_size,width,height,channel]。本文实现读取图像并返回一个四维数组:[1,width,height,3]。本代码主要供哪些需要将python中训练的模型利用java进行后端部署!

读取图像

    /**
     * 读取指定路径图像为BufferedImage类型图像bf
     * @Authon ChenYirong <1132461715@qq.com>
     * @Date 2021.05.25
     * @param imageFile
     * @return bf
     */
    public static BufferedImage readImage(String imageFile){
        File file = new File(imageFile);
        BufferedImage bf = null;
        try {
            bf = ImageIO.read(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bf;
    }

把图像数组转换为四维数组

    /**
     * 将BufferedImage类型图像bf转换为四维数组rgb4DArray
     * @Authon ChenYirong <1132461715@qq.com>
     * @Date 2021.05.25
     * @param bf
     * @return rgb4DArray
     */
    public static int[][][][] convert2DArrayTO4DArray(BufferedImage bf) {
        // 获取图片宽度和高度
        int width = bf.getWidth();   // 图片宽度
        int height = bf.getHeight();  //图片高度
        int channel = 3; // 3个通道
        int r = 0;
        int g = 1;
        int b = 2;
        int[] data = new int[width*height];
        bf.getRGB(0, 0, width, height, data, 0, width);
        // 将二维数组转换为三维数组
        int[][][][] rgb4DArray = new int[1][width][height][channel]; // 图像数量*宽度*高度*通道数


        for(int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                rgb4DArray[0][j][i][r] = (data[i*width + j] & 0xff0000) >> 16;
                rgb4DArray[0][j][i][g] = (data[i*width + j] & 0xff00) >> 8;
                rgb4DArray[0][j][i][b] = (data[i*width + j] & 0xff);
            }
        }
        return rgb4DArray;
    }

完整实现代码

/**
 * Copyright (C) 2020-2021
 * All rights reserved, Designed By chenyirong
 * 注意:

 * 修改日期:2021.04.14
 * 代码版本:v1.0.0
 */


import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;

public class Main {
    public static void main(String[] args) {
        // 读取图片到BufferedImage
        BufferedImage bf = readImage("E:\\cyr_zhishi.jpg");//这里写你要读取的绝对路径+文件名

        // 将图片转换为4维数组
        int[][][][] rgb4DArray = convert2DArrayTO4DArray(bf);

        // 输出数组
        System.out.println("4维数组:");
        System.out.println("rgb4DArray[0][0][0][0]=" + rgb4DArray[0][0][0][0]);
        int channel = rgb4DArray[0][0][0].length;
        int width = rgb4DArray[0][0].length;
        int height = rgb4DArray[0].length;
        System.out.println("channel:" + channel);
        System.out.println("width:" + width);
        System.out.println("height:" + height);

    }

    /**
     * 读取指定路径图像为BufferedImage类型图像bf
     * @Authon ChenYirong <1132461715@qq.com>
     * @Date 2021.05.25
     * @param imageFile
     * @return bf
     */
    public static BufferedImage readImage(String imageFile){
        File file = new File(imageFile);
        BufferedImage bf = null;
        try {
            bf = ImageIO.read(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bf;
    }


    /**
     * 将BufferedImage类型图像bf转换为四维数组rgb4DArray
     * @Authon ChenYirong <1132461715@qq.com>
     * @Date 2021.05.25
     * @param bf
     * @return rgb4DArray
     */
    public static int[][][][] convert2DArrayTO4DArray(BufferedImage bf) {
        // 获取图片宽度和高度
        int width = bf.getWidth();   // 图片宽度
        int height = bf.getHeight();  //图片高度
        int channel = 3; // 3个通道
        int r = 0;
        int g = 1;
        int b = 2;
        int[] data = new int[width*height];
        bf.getRGB(0, 0, width, height, data, 0, width);
        // 将二维数组转换为三维数组
        int[][][][] rgb4DArray = new int[1][width][height][channel]; // 图像数量*宽度*高度*通道数


        for(int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                rgb4DArray[0][j][i][r] = (data[i*width + j] & 0xff0000) >> 16;
                rgb4DArray[0][j][i][g] = (data[i*width + j] & 0xff00) >> 8;
                rgb4DArray[0][j][i][b] = (data[i*width + j] & 0xff);
            }
        }
        return rgb4DArray;
    }


}

运行效果如下

在这里插入图片描述

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YirongChen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值