【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;
}
}