protected BufferedImage scalingIfNeed(BufferedImage image, boolean needAlpha) { return scalingIfNeed(image, height, width, needAlpha); }
/** * Convert an BufferedImage to an bgr spectrum image * * @param image the BufferedImage to convert * @return the input stream to convert */ public INDArray toBgr(BufferedImage image) { if (image == null) throw new IllegalStateException("Unable to load image"); image = scalingIfNeed(image, false); return toINDArrayBGR(image); }
/** * Load a rastered image from file * @param file the file to load * @return the rastered image * @throws IOException */ public int[][] fromFile(File file) throws IOException { BufferedImage image = ImageIO.read(file); image = scalingIfNeed(image, true); return toIntArrayArray(image); }
/** * Convert an image in to a raveled tensor of * the bgr values of the image * @param image the image to parse * @return the raveled tensor of bgr values */ public INDArray toRaveledTensor(BufferedImage image) { try { image = scalingIfNeed(image, false); return toINDArrayBGR(image).ravel(); } catch (Exception e) { throw new RuntimeException("Unable to load image", e); } }
/** * Load a rastered image from file * @param file the file to load * @return the rastered image * @throws IOException */ public int[][][] fromFileMultipleChannels(File file) throws IOException { BufferedImage image = ImageIO.read(file); image = scalingIfNeed(image, channels > 3); int w = image.getWidth(), h = image.getHeight(); int bands = image.getSampleModel().getNumBands(); int[][][] ret = new int[channels][h][w]; byte[] pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { for (int k = 0; k < channels; k++) { if (k >= bands) break; ret[k][i][j] = pixels[channels * w * i + channels * j + k]; } } } return ret; }
/** * Convert an BufferedImage to a matrix * @param image the BufferedImage to convert * @return the input stream to convert */ public INDArray asMatrix(BufferedImage image) { if (channels == 3) { return toBgr(image); } else { image = scalingIfNeed(image, true); int w = image.getWidth(); int h = image.getHeight(); INDArray ret = Nd4j.create(h, w); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { ret.putScalar(new int[] {i, j}, image.getRGB(j, i)); } } return ret; } }
/** * Convert the given image to an rgb image * @param arr the array to use * @param image the image to set */ public void toBufferedImageRGB(INDArray arr, BufferedImage image) { if (arr.rank() < 3) throw new IllegalArgumentException("Arr must be 3d"); image = scalingIfNeed(image, arr.size(-2), arr.size(-1), true); for (int i = 0; i < image.getWidth(); i++) { for (int j = 0; j < image.getHeight(); j++) { int r = arr.slice(0).getInt(i, j); int g = arr.slice(1).getInt(i, j); int b = arr.slice(2).getInt(i, j); int a = 1; int col = (a << 24) | (r << 16) | (g << 8) | b; image.setRGB(i, j, col); } } }
/** * Convert an image in to a row vector * @param image the image to convert * @return the row vector based on a rastered * representation of the image */ public INDArray asRowVector(BufferedImage image) { if (centerCropIfNeeded) { image = centerCropIfNeeded(image); } image = scalingIfNeed(image, true); if (channels == 3) { return toINDArrayBGR(image).ravel(); } int[][] ret = toIntArrayArray(image); return NDArrayUtil.toNDArray(ArrayUtil.flatten(ret)); }