NativeImageLoader extends BaseImageLoader { public ImageWritable asWritable(File f) throws IOException { try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f))) { Mat mat = streamToMat(bis); Mat image = imdecode(mat, CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR); if (image == null || image.empty()) { PIX pix = pixReadMem(mat.data(), mat.cols()); if (pix == null) { throw new IOException("Could not decode image from input stream"); } image = convert(pix); pixDestroy(pix); } ImageWritable writable = new ImageWritable(converter.convert(image)); return writable; } } NativeImageLoader();  NativeImageLoader(int height, int width);  NativeImageLoader(int height, int width, int channels);  NativeImageLoader(int height, int width, int channels, boolean centerCropIfNeeded);  NativeImageLoader(int height, int width, int channels, ImageTransform imageTransform);  NativeImageLoader(int height, int width, int channels, MultiPageMode mode); protected  NativeImageLoader(NativeImageLoader other); @Override String[] getAllowedFormats(); @Override INDArray asRowVector(File f); @Override INDArray asRowVector(InputStream is); INDArray asRowVector(Object image); INDArray asRowVector(Frame image); INDArray asRowVector(Mat image); @Override INDArray asMatrix(File f); @Override INDArray asMatrix(InputStream is); @Override Image asImageMatrix(File f); @Override Image asImageMatrix(InputStream is); INDArray asMatrix(Object image); void asMatrixView(InputStream is, INDArray view); void asMatrixView(File f, INDArray view); void asMatrixView(Mat image, INDArray view); INDArray asMatrix(Frame image); INDArray asMatrix(Mat image); ImageWritable asWritable(File f); INDArray asMatrix(ImageWritable writable); Frame asFrame(INDArray array); Frame asFrame(INDArray array, int dataType); Mat asMat(INDArray array); Mat asMat(INDArray array, int dataType); static final String[] ALLOWED_FORMATS; }
@Test public void testAsWritable() throws Exception { File f0 = new ClassPathResource("/testimages/class0/0.jpg").getFile(); NativeImageLoader imageLoader = new NativeImageLoader(); ImageWritable img = imageLoader.asWritable(f0); assertEquals(32, img.getFrame().imageHeight); assertEquals(32, img.getFrame().imageWidth); assertEquals(3, img.getFrame().imageChannels); BufferedImage img1 = makeRandomBufferedImage(0, 0, 3); Mat img2 = makeRandomImage(0, 0, 4); int w1 = 33, h1 = 77, ch1 = 1; NativeImageLoader loader1 = new NativeImageLoader(h1, w1, ch1); INDArray array1 = loader1.asMatrix(f0); assertEquals(4, array1.rank()); assertEquals(1, array1.size(0)); assertEquals(1, array1.size(1)); assertEquals(h1, array1.size(2)); assertEquals(w1, array1.size(3)); }