/** * Convert a file to a row vector * * @param f the image to convert * @return the flattened image * @throws IOException */ public INDArray asRowVector(File f) throws IOException { return asRowVector(ImageIO.read(f)); // if(channels == 3) { // return toRaveledTensor(f); // } // return NDArrayUtil.toNDArray(flattenedImageFromFile(f)); }
private void writeImage(INDArray array, File file) { BufferedImage image = ImageLoader.toImage(array); try { ImageIO.write(image, "png", file); } catch (IOException e) { e.printStackTrace(); } }
/** * Convert an input stream to a matrix * @param inputStream the input stream to convert * @return the input stream to convert */ public INDArray asMatrix(InputStream inputStream) throws IOException { if (channels == 3) return toBgr(inputStream); try { BufferedImage image = ImageIO.read(inputStream); return asMatrix(image); } catch (IOException e) { throw new IOException("Unable to load image", e); } }
/** * 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 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)); }
BufferedImage grab = fg.getFrame(); if (ravel) record.add(RecordConverter.toRecord(imageLoader.toRaveledTensor(grab))); else record.add(RecordConverter.toRecord(imageLoader.asRowVector(grab))); BufferedImage grab = FrameGrab.getFrame(seekableByteChannel, i); if (ravel) record.add(RecordConverter.toRecord(imageLoader.toRaveledTensor(grab))); else record.add(RecordConverter.toRecord(imageLoader.asRowVector(grab)));
@Override public void setConf(Configuration conf) { super.setConf(conf); imageLoader = new ImageLoader(rows, cols); }
/** * Changes the input stream in to an * bgr based raveled(flattened) vector * @param file the input stream to convert * @return the raveled bgr values for this input stream */ public INDArray toRaveledTensor(File file) { try { BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); INDArray ret = toRaveledTensor(bis); bis.close(); return ret.ravel(); } catch (IOException e) { throw new RuntimeException(e); } }
/** * 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 an input stream to an bgr spectrum image * * @param inputStream the input stream to convert * @return the input stream to convert */ public INDArray toBgr(InputStream inputStream) { try { BufferedImage image = ImageIO.read(inputStream); return toBgr(image); } catch (IOException e) { throw new RuntimeException("Unable to load image", e); } }
protected BufferedImage scalingIfNeed(BufferedImage image, boolean needAlpha) { return scalingIfNeed(image, height, width, needAlpha); }
/** * Convert an image file * in to a matrix * @param f the file to convert * @return a 2d matrix of a rastered version of the image * @throws IOException */ public INDArray asMatrix(File f) throws IOException { return NDArrayUtil.toNDArray(fromFile(f)); }
protected BufferedImage scalingIfNeed(BufferedImage image, int dstHeight, int dstWidth, boolean needAlpha) { if (dstHeight > 0 && dstWidth > 0 && (image.getHeight() != dstHeight || image.getWidth() != dstWidth)) { Image scaled = image.getScaledInstance(dstWidth, dstHeight, Image.SCALE_SMOOTH); if (needAlpha && image.getColorModel().hasAlpha() && channels == BufferedImage.TYPE_4BYTE_ABGR) { return toBufferedImage(scaled, BufferedImage.TYPE_4BYTE_ABGR); } else { if (channels == BufferedImage.TYPE_BYTE_GRAY) return toBufferedImage(scaled, BufferedImage.TYPE_BYTE_GRAY); else return toBufferedImage(scaled, BufferedImage.TYPE_3BYTE_BGR); } } else { if (image.getType() == BufferedImage.TYPE_4BYTE_ABGR || image.getType() == BufferedImage.TYPE_3BYTE_BGR) { return image; } else if (needAlpha && image.getColorModel().hasAlpha() && channels == BufferedImage.TYPE_4BYTE_ABGR) { return toBufferedImage(image, BufferedImage.TYPE_4BYTE_ABGR); } else { if (channels == BufferedImage.TYPE_BYTE_GRAY) return toBufferedImage(image, BufferedImage.TYPE_BYTE_GRAY); else return toBufferedImage(image, BufferedImage.TYPE_3BYTE_BGR); } } }
/** * Slices up an image in to a mini batch. * * @param f the file to load from * @param numMiniBatches the number of images in a mini batch * @param numRowsPerSlice the number of rows for each image * @return a tensor representing one image as a mini batch */ public INDArray asImageMiniBatches(File f, int numMiniBatches, int numRowsPerSlice) { try { INDArray d = asMatrix(f); return Nd4j.create(numMiniBatches, numRowsPerSlice, d.columns()); } catch (Exception e) { throw new RuntimeException(e); } }
@Override public void initialize(Configuration conf, InputSplit split) throws IOException, InterruptedException { this.conf = conf; this.appendLabel = conf.getBoolean(APPEND_LABEL, false); this.height = conf.getInt(HEIGHT, height); this.width = conf.getInt(WIDTH, width); if ("imageio".equals(conf.get(IMAGE_LOADER))) { this.imageLoader = new ImageLoader(height, width); } else { this.imageLoader = new NativeImageLoader(height, width); } initialize(split); }
/** * 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); } }
/** * Convert an input stream to an bgr spectrum image * * @param file the file to convert * @return the input stream to convert */ public INDArray toBgr(File file) { try { BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); INDArray ret = toBgr(bis); bis.close(); return ret; } catch (IOException e) { throw new RuntimeException(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; }
public int[] flattenedImageFromFile(File f) throws IOException { return ArrayUtil.flatten(fromFile(f)); }