@Test
public void testAsMatrix() throws Exception {

    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(img1);
    assertEquals(4, array1.rank());
    assertEquals(1, array1.size(0));
    assertEquals(1, array1.size(1));
    assertEquals(h1, array1.size(2));
    assertEquals(w1, array1.size(3));
    INDArray array2 = loader1.asMatrix(img2);
    assertEquals(4, array2.rank());
    assertEquals(1, array2.size(0));
    assertEquals(1, array2.size(1));
    assertEquals(h1, array2.size(2));
    assertEquals(w1, array2.size(3));
    int w2 = 111, h2 = 66, ch2 = 3;
    NativeImageLoader loader2 = new NativeImageLoader(h2, w2, ch2);
    INDArray array3 = loader2.asMatrix(img1);
    assertEquals(4, array3.rank());
    assertEquals(1, array3.size(0));
    assertEquals(3, array3.size(1));
    assertEquals(h2, array3.size(2));
    assertEquals(w2, array3.size(3));
    INDArray array4 = loader2.asMatrix(img2);
    assertEquals(4, array4.rank());
    assertEquals(1, array4.size(0));
    assertEquals(3, array4.size(1));
    assertEquals(h2, array4.size(2));
    assertEquals(w2, array4.size(3));
    int w3 = 123, h3 = 77, ch3 = 3;
    NativeImageLoader loader3 = new NativeImageLoader(h3, w3, ch3);
    File f3 = new ClassPathResource("/testimages/class0/2.jpg").getFile();
    ImageWritable iw3 = loader3.asWritable(f3);
    INDArray array5 = loader3.asMatrix(iw3);
    assertEquals(4, array5.rank());
    assertEquals(1, array5.size(0));
    assertEquals(3, array5.size(1));
    assertEquals(h3, array5.size(2));
    assertEquals(w3, array5.size(3));
    Mat mat = loader3.asMat(array5);
    assertEquals(w3, mat.cols());
    assertEquals(h3, mat.rows());
    assertEquals(ch3, mat.channels());
    assertTrue(mat.type() == CV_32FC(ch3) || mat.type() == CV_64FC(ch3));
    Frame frame = loader3.asFrame(array5, Frame.DEPTH_UBYTE);
    assertEquals(w3, frame.imageWidth);
    assertEquals(h3, frame.imageHeight);
    assertEquals(ch3, frame.imageChannels);
    assertEquals(Frame.DEPTH_UBYTE, frame.imageDepth);
    Java2DNativeImageLoader loader4 = new Java2DNativeImageLoader();
    BufferedImage img12 = loader4.asBufferedImage(array1);
    assertEquals(array1, loader4.asMatrix(img12));
}