org.datavec.image.data.ImageWritable Java Examples

The following examples show how to use org.datavec.image.data.ImageWritable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ImageWritableToINDArrayTransform.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public INDArray transform(ImageWritable imageWritable) {

    int height = imageWritable.getHeight();
    int width = imageWritable.getWidth();
    int channels = imageWritable.getFrame().imageChannels;

    INDArray out = null;
    try {
        out = loader.asMatrix(imageWritable);
    } catch (IOException e) {
        e.printStackTrace();
    }

    // Convert back to uint8 and reshape to the number of channels in the image
    out = out.reshape(channels, height, width);
    INDArray compressed = out.castTo(DataType.UINT8);
    return compressed;
}
 
Example #2
Source File: ColorConversionTransform.java    From DataVec with Apache License 2.0 6 votes vote down vote up
/**
 * Takes an image and returns a transformed image.
 * Uses the random object in the case of random transformations.
 *
 * @param image  to transform, null == end of stream
 * @param random object to use (or null for deterministic)
 * @return transformed image
 */
@Override
protected ImageWritable doTransform(ImageWritable image, Random random) {
    if (image == null) {
        return null;
    }
    Mat mat = (Mat) converter.convert(image.getFrame());

    Mat result = new Mat();

    try {
        cvtColor(mat, result, conversionCode);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    return new ImageWritable(converter.convert(result));
}
 
Example #3
Source File: PipelineImageTransform.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
/**
 * Takes an image and executes a pipeline of combined transforms.
 *
 * @param image to transform, null == end of stream
 * @param random object to use (or null for deterministic)
 * @return transformed image
 */
@Override
protected ImageWritable doTransform(ImageWritable image, Random random) {
    if (shuffle) {
        Collections.shuffle(imageTransforms);
    }

    currentTransforms.clear();

    // execute each item in the pipeline
    for (Pair<ImageTransform, Double> tuple : imageTransforms) {
        if (tuple.getSecond() == 1.0 || rng.nextDouble() < tuple.getSecond()) { // probability of execution
            currentTransforms.add(tuple.getFirst());
            image = random != null ? tuple.getFirst().transform(image, random)
                    : tuple.getFirst().transform(image);
        }
    }

    return image;
}
 
Example #4
Source File: TestImageTransform.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testCropImageTransform() throws Exception {
    ImageWritable writable = makeRandomImage(0, 0, 1);
    Frame frame = writable.getFrame();
    ImageTransform transform = new CropImageTransform(rng, frame.imageHeight / 2, frame.imageWidth / 2,
                    frame.imageHeight / 2, frame.imageWidth / 2);

    for (int i = 0; i < 100; i++) {
        ImageWritable w = transform.transform(writable);
        Frame f = w.getFrame();
        assertTrue(f.imageHeight <= frame.imageHeight);
        assertTrue(f.imageWidth <= frame.imageWidth);
        assertEquals(f.imageChannels, frame.imageChannels);
    }
    assertEquals(null, transform.transform(null));

    transform = new CropImageTransform(1, 2, 3, 4);
    writable = transform.transform(writable);
    float[] coordinates = {1, 2, 3, 4};
    float[] transformed = transform.query(coordinates);
    assertEquals(1 - 2, transformed[0], 0);
    assertEquals(2 - 1, transformed[1], 0);
    assertEquals(3 - 2, transformed[2], 0);
    assertEquals(4 - 1, transformed[3], 0);
}
 
Example #5
Source File: RotateImageTransform.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
protected ImageWritable doTransform(ImageWritable image, Random random) {
    if (image == null) {
        return null;
    }
    Mat mat = converter.convert(image.getFrame());
    float cy = mat.rows() / 2 + centery * (random != null ? 2 * random.nextFloat() - 1 : 1);
    float cx = mat.cols() / 2 + centerx * (random != null ? 2 * random.nextFloat() - 1 : 1);
    float a = angle * (random != null ? 2 * random.nextFloat() - 1 : 1);
    float s = 1 + scale * (random != null ? 2 * random.nextFloat() - 1 : 1);

    Mat result = new Mat();
    M = getRotationMatrix2D(new Point2f(cx, cy), a, s);
    warpAffine(mat, result, M, mat.size(), interMode, borderMode, borderValue);
    return new ImageWritable(converter.convert(result));
}
 
Example #6
Source File: NativeImageLoader.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
/**
 * Convert a file to a INDArray
 *
 * @param f the image to convert
 * @return INDArray
 * @throws IOException
 */
public ImageWritable asWritable(File f) throws IOException {
    try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f))) {
        Mat mat = streamToMat(bis);
        Mat image = imdecode(mat, IMREAD_ANYDEPTH | IMREAD_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;
    }
}
 
Example #7
Source File: NativeImageLoader.java    From konduit-serving with Apache License 2.0 6 votes vote down vote up
/**
 * Convert a file to a {@link ImageWritable}
 *
 * @param is the {@link InputStream} to read
 * @return {@link ImageWritable} representing the image
 * @throws IOException if an error creating the image occurs
 */
public ImageWritable asWritable(InputStream is) throws IOException {
    try (BufferedInputStream bis = new BufferedInputStream(is)) {
        Mat mat = streamToMat(bis);
        Mat image = imdecode(mat, IMREAD_ANYDEPTH | IMREAD_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;
    }
}
 
Example #8
Source File: TestImageTransform.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testShowImageTransform() throws Exception {
    if (GraphicsEnvironment.isHeadless()) { return; }

    ImageWritable writable = makeRandomImage(0, 0, 3);
    ImageTransform transform = new ShowImageTransform("testShowImageTransform", 100);

    for (int i = 0; i < 10; i++) {
        ImageWritable w = transform.transform(writable);
        assertEquals(w, writable);
    }

    assertEquals(null, transform.transform(null));

    float[] transformed = transform.query(new float[] {33, 44});
    assertEquals(33, transformed[0], 0);
    assertEquals(44, transformed[1], 0);
}
 
Example #9
Source File: TestImageTransform.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testRotateImageTransform() throws Exception {
    ImageWritable writable = makeRandomImage(0, 0, 1);
    Frame frame = writable.getFrame();
    ImageTransform transform =
                    new RotateImageTransform(rng, 180).interMode(INTER_NEAREST).borderMode(BORDER_REFLECT);

    for (int i = 0; i < 100; i++) {
        ImageWritable w = transform.transform(writable);
        Frame f = w.getFrame();
        assertEquals(f.imageHeight, frame.imageHeight);
        assertEquals(f.imageWidth, frame.imageWidth);
        assertEquals(f.imageChannels, frame.imageChannels);
    }
    assertEquals(null, transform.transform(null));

    transform = new RotateImageTransform(0, 0, -90, 0);
    writable = transform.transform(writable);
    float[] coordinates = {frame.imageWidth / 2, frame.imageHeight / 2, 0, 0};
    float[] transformed = transform.query(coordinates);
    assertEquals(frame.imageWidth  / 2, transformed[0], 0);
    assertEquals(frame.imageHeight / 2, transformed[1], 0);
    assertEquals((frame.imageHeight + frame.imageWidth) / 2, transformed[2], 1);
    assertEquals((frame.imageHeight - frame.imageWidth) / 2, transformed[3], 1);
}
 
Example #10
Source File: ResizeImageTransformTest.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testResizeUpscale1() throws Exception {
    ImageWritable srcImg = TestImageTransform.makeRandomImage(32, 32, 3);

    ResizeImageTransform transform = new ResizeImageTransform(200, 200);

    ImageWritable dstImg = transform.transform(srcImg);

    Frame f = dstImg.getFrame();
    assertEquals(f.imageWidth, 200);
    assertEquals(f.imageHeight, 200);

    float[] coordinates = {100, 200};
    float[] transformed = transform.query(coordinates);
    assertEquals(200f * 100 / 32, transformed[0], 0);
    assertEquals(200f * 200 / 32, transformed[1], 0);
}
 
Example #11
Source File: ResizeImageTransformTest.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testResizeDownscale() throws Exception {
    ImageWritable srcImg = TestImageTransform.makeRandomImage(571, 443, 3);

    ResizeImageTransform transform = new ResizeImageTransform(200, 200);

    ImageWritable dstImg = transform.transform(srcImg);

    Frame f = dstImg.getFrame();
    assertEquals(f.imageWidth, 200);
    assertEquals(f.imageHeight, 200);

    float[] coordinates = {300, 400};
    float[] transformed = transform.query(coordinates);
    assertEquals(200f * 300 / 443, transformed[0], 0);
    assertEquals(200f * 400 / 571, transformed[1], 0);
}
 
Example #12
Source File: TestImageTransform.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultiImageTransform() throws Exception {
    ImageWritable writable = makeRandomImage(0, 0, 3);
    Frame frame = writable.getFrame();
    ImageTransform transform = new MultiImageTransform(rng, new CropImageTransform(10),
                    new FlipImageTransform(), new ScaleImageTransform(10), new WarpImageTransform(10));

    for (int i = 0; i < 100; i++) {
        ImageWritable w = transform.transform(writable);
        Frame f = w.getFrame();
        assertTrue(f.imageHeight >= frame.imageHeight - 30);
        assertTrue(f.imageHeight <= frame.imageHeight + 20);
        assertTrue(f.imageWidth >= frame.imageWidth - 30);
        assertTrue(f.imageWidth <= frame.imageWidth + 20);
        assertEquals(f.imageChannels, frame.imageChannels);
    }
    assertEquals(null, transform.transform(null));

    transform = new MultiImageTransform(new ColorConversionTransform(COLOR_BGR2RGB));
    writable = transform.transform(writable);
    float[] transformed = transform.query(new float[] {11, 22});
    assertEquals(11, transformed[0], 0);
    assertEquals(22, transformed[1], 0);
}
 
Example #13
Source File: FlipImageTransform.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
protected ImageWritable doTransform(ImageWritable image, Random random) {
    if (image == null) {
        return null;
    }

    Mat mat = converter.convert(image.getFrame());

    if(mat == null) {
        return null;
    }
    h = mat.rows();
    w = mat.cols();

    mode = random != null ? random.nextInt(4) - 2 : flipMode;

    Mat result = new Mat();
    if (mode < -1) {
        // no flip
        mat.copyTo(result);
    } else {
        flip(mat, result, mode);
    }

    return new ImageWritable(converter.convert(result));
}
 
Example #14
Source File: TestImageTransform.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testRandomCropTransform() throws Exception {
    ImageWritable writable = makeRandomImage(0, 0, 1);
    Frame frame = writable.getFrame();
    ImageTransform transform = new RandomCropTransform(frame.imageHeight / 2, frame.imageWidth / 2);

    for (int i = 0; i < 100; i++) {
        ImageWritable w = transform.transform(writable);
        Frame f = w.getFrame();
        assertTrue(f.imageHeight == frame.imageHeight / 2);
        assertTrue(f.imageWidth == frame.imageWidth / 2);
    }
    assertEquals(null, transform.transform(null));

    transform = new RandomCropTransform(frame.imageHeight, frame.imageWidth);
    writable = transform.transform(writable);
    float[] coordinates = {2, 4, 6, 8};
    float[] transformed = transform.query(coordinates);
    assertEquals(2, transformed[0], 0);
    assertEquals(4, transformed[1], 0);
    assertEquals(6, transformed[2], 0);
    assertEquals(8, transformed[3], 0);
}
 
Example #15
Source File: CropImageTransform.java    From DataVec with Apache License 2.0 6 votes vote down vote up
/**
 * Takes an image and returns a transformed image.
 * Uses the random object in the case of random transformations.
 *
 * @param image  to transform, null == end of stream
 * @param random object to use (or null for deterministic)
 * @return transformed image
 */
@Override
protected ImageWritable doTransform(ImageWritable image, Random random) {
    if (image == null) {
        return null;
    }
    Mat mat = converter.convert(image.getFrame());
    int top = random != null ? random.nextInt(cropTop + 1) : cropTop;
    int left = random != null ? random.nextInt(cropLeft + 1) : cropLeft;
    int bottom = random != null ? random.nextInt(cropBottom + 1) : cropBottom;
    int right = random != null ? random.nextInt(cropRight + 1) : cropRight;

    y = Math.min(top, mat.rows() - 1);
    x = Math.min(left, mat.cols() - 1);
    int h = Math.max(1, mat.rows() - bottom - y);
    int w = Math.max(1, mat.cols() - right - x);
    Mat result = mat.apply(new Rect(x, y, w, h));

    return new ImageWritable(converter.convert(result));
}
 
Example #16
Source File: TestNativeImageLoader.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testAsWritable() throws Exception {
    String f0 = new ClassPathResource("datavec-data-image/testimages/class0/0.jpg").getFile().getAbsolutePath();

    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));
    assertNotEquals(0.0, array1.sum().getDouble(0), 0.0);
}
 
Example #17
Source File: TestNativeImageLoader.java    From DataVec with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example #18
Source File: RotateImageTransform.java    From DataVec with Apache License 2.0 6 votes vote down vote up
@Override
protected ImageWritable doTransform(ImageWritable image, Random random) {
    if (image == null) {
        return null;
    }
    Mat mat = converter.convert(image.getFrame());
    float cy = mat.rows() / 2 + centery * (random != null ? 2 * random.nextFloat() - 1 : 1);
    float cx = mat.cols() / 2 + centerx * (random != null ? 2 * random.nextFloat() - 1 : 1);
    float a = angle * (random != null ? 2 * random.nextFloat() - 1 : 1);
    float s = 1 + scale * (random != null ? 2 * random.nextFloat() - 1 : 1);

    Mat result = new Mat();
    M = getRotationMatrix2D(new Point2f(cx, cy), a, s);
    warpAffine(mat, result, M, mat.size(), interMode, borderMode, borderValue);
    return new ImageWritable(converter.convert(result));
}
 
Example #19
Source File: NativeImageLoader.java    From DataVec with Apache License 2.0 6 votes vote down vote up
/**
 * Convert a file to a INDArray
 *
 * @param f the image to convert
 * @return INDArray
 * @throws IOException
 */
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;
    }
}
 
Example #20
Source File: TestImageTransform.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testScaleImageTransform() throws Exception {
    ImageWritable writable = makeRandomImage(0, 0, 4);
    Frame frame = writable.getFrame();
    ImageTransform transform = new ScaleImageTransform(rng, frame.imageWidth / 2, frame.imageHeight / 2);

    for (int i = 0; i < 100; i++) {
        ImageWritable w = transform.transform(writable);
        Frame f = w.getFrame();
        assertTrue(f.imageHeight >= frame.imageHeight / 2);
        assertTrue(f.imageHeight <= 3 * frame.imageHeight / 2);
        assertTrue(f.imageWidth >= frame.imageWidth / 2);
        assertTrue(f.imageWidth <= 3 * frame.imageWidth / 2);
        assertEquals(f.imageChannels, frame.imageChannels);
    }
    assertEquals(null, transform.transform(null));

    transform = new ScaleImageTransform(frame.imageWidth, 2 * frame.imageHeight);
    writable = transform.transform(writable);
    float[] coordinates = {5, 7, 11, 13};
    float[] transformed = transform.query(coordinates);
    assertEquals(5 * 2,  transformed[0], 0);
    assertEquals(7 * 3,  transformed[1], 0);
    assertEquals(11 * 2, transformed[2], 0);
    assertEquals(13 * 3, transformed[3], 0);
}
 
Example #21
Source File: CifarLoader.java    From DataVec with Apache License 2.0 6 votes vote down vote up
/**
 * Preprocess and store cifar based on successful Torch approach by Sergey Zagoruyko
 * Reference: https://github.com/szagoruyko/cifar.torch
 */
public opencv_core.Mat convertCifar(Mat orgImage) {
    numExamples++;
    Mat resImage = new Mat();
    OpenCVFrameConverter.ToMat converter = new OpenCVFrameConverter.ToMat();
    //        ImageTransform yuvTransform = new ColorConversionTransform(new Random(seed), COLOR_BGR2Luv);
    //        ImageTransform histEqualization = new EqualizeHistTransform(new Random(seed), COLOR_BGR2Luv);
    ImageTransform yuvTransform = new ColorConversionTransform(new Random(seed), COLOR_BGR2YCrCb);
    ImageTransform histEqualization = new EqualizeHistTransform(new Random(seed), COLOR_BGR2YCrCb);

    if (converter != null) {
        ImageWritable writable = new ImageWritable(converter.convert(orgImage));
        // TODO determine if need to normalize y before transform - opencv docs rec but currently doing after
        writable = yuvTransform.transform(writable); // Converts to chrome color to help emphasize image objects
        writable = histEqualization.transform(writable); // Normalizes values to further clarify object of interest
        resImage = converter.convert(writable.getFrame());
    }

    return resImage;
}
 
Example #22
Source File: TestImageTransform.java    From DataVec with Apache License 2.0 6 votes vote down vote up
public static ImageWritable makeRandomImage(int height, int width, int channels) {
    if (height <= 0) {
        height = rng.nextInt() % 100 + 200;
    }
    if (width <= 0) {
        width = rng.nextInt() % 100 + 200;
    }
    Mat img = new Mat(height, width, CV_8UC(channels));
    UByteIndexer idx = img.createIndexer();
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            for (int k = 0; k < channels; k++) {
                idx.put(i, j, k, rng.nextInt());
            }
        }
    }
    Frame frame = converter.convert(img);
    return new ImageWritable(frame);
}
 
Example #23
Source File: ResizeImageTransformTest.java    From DataVec with Apache License 2.0 6 votes vote down vote up
@Test
public void testResizeUpscale1() throws Exception {
    ImageWritable srcImg = TestImageTransform.makeRandomImage(32, 32, 3);

    ResizeImageTransform transform = new ResizeImageTransform(200, 200);

    ImageWritable dstImg = transform.transform(srcImg);

    Frame f = dstImg.getFrame();
    assertEquals(f.imageWidth, 200);
    assertEquals(f.imageHeight, 200);

    float[] coordinates = {100, 200};
    float[] transformed = transform.query(coordinates);
    assertEquals(200f * 100 / 32, transformed[0], 0);
    assertEquals(200f * 200 / 32, transformed[1], 0);
}
 
Example #24
Source File: TestNativeImageLoader.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testDataTypes_1() throws Exception {
    val dtypes = new DataType[]{DataType.FLOAT, DataType.HALF, DataType.SHORT, DataType.INT};

    val dt = Nd4j.dataType();

    for (val dtype: dtypes) {
        Nd4j.setDataType(dtype);
        int w3 = 123, h3 = 77, ch3 = 3;
        val loader = new NativeImageLoader(h3, w3, ch3);
        File f3 = new ClassPathResource("datavec-data-image/testimages/class0/2.jpg").getFile();
        ImageWritable iw3 = loader.asWritable(f3);

        val array = loader.asMatrix(iw3);

        assertEquals(dtype, array.dataType());
    }

    Nd4j.setDataType(dt);
}
 
Example #25
Source File: TestImageTransform.java    From DataVec with Apache License 2.0 6 votes vote down vote up
@Test
public void testCropImageTransform() throws Exception {
    ImageWritable writable = makeRandomImage(0, 0, 1);
    Frame frame = writable.getFrame();
    ImageTransform transform = new CropImageTransform(rng, frame.imageHeight / 2, frame.imageWidth / 2,
                    frame.imageHeight / 2, frame.imageWidth / 2);

    for (int i = 0; i < 100; i++) {
        ImageWritable w = transform.transform(writable);
        Frame f = w.getFrame();
        assertTrue(f.imageHeight <= frame.imageHeight);
        assertTrue(f.imageWidth <= frame.imageWidth);
        assertEquals(f.imageChannels, frame.imageChannels);
    }
    assertEquals(null, transform.transform(null));

    transform = new CropImageTransform(1, 2, 3, 4);
    writable = transform.transform(writable);
    float[] coordinates = {1, 2, 3, 4};
    float[] transformed = transform.query(coordinates);
    assertEquals(1 - 2, transformed[0], 0);
    assertEquals(2 - 1, transformed[1], 0);
    assertEquals(3 - 2, transformed[2], 0);
    assertEquals(4 - 1, transformed[3], 0);
}
 
Example #26
Source File: TestImageTransform.java    From DataVec with Apache License 2.0 6 votes vote down vote up
@Test
public void testShowImageTransform() throws Exception {
    if (GraphicsEnvironment.isHeadless()) { return; }

    ImageWritable writable = makeRandomImage(0, 0, 3);
    ImageTransform transform = new ShowImageTransform("testShowImageTransform", 100);

    for (int i = 0; i < 10; i++) {
        ImageWritable w = transform.transform(writable);
        assertEquals(w, writable);
    }

    assertEquals(null, transform.transform(null));

    float[] transformed = transform.query(new float[] {33, 44});
    assertEquals(33, transformed[0], 0);
    assertEquals(44, transformed[1], 0);
}
 
Example #27
Source File: TestImageTransform.java    From DataVec with Apache License 2.0 6 votes vote down vote up
@Test
public void testScaleImageTransform() throws Exception {
    ImageWritable writable = makeRandomImage(0, 0, 4);
    Frame frame = writable.getFrame();
    ImageTransform transform = new ScaleImageTransform(rng, frame.imageWidth / 2, frame.imageHeight / 2);

    for (int i = 0; i < 100; i++) {
        ImageWritable w = transform.transform(writable);
        Frame f = w.getFrame();
        assertTrue(f.imageHeight >= frame.imageHeight / 2);
        assertTrue(f.imageHeight <= 3 * frame.imageHeight / 2);
        assertTrue(f.imageWidth >= frame.imageWidth / 2);
        assertTrue(f.imageWidth <= 3 * frame.imageWidth / 2);
        assertEquals(f.imageChannels, frame.imageChannels);
    }
    assertEquals(null, transform.transform(null));

    transform = new ScaleImageTransform(frame.imageWidth, 2 * frame.imageHeight);
    writable = transform.transform(writable);
    float[] coordinates = {5, 7, 11, 13};
    float[] transformed = transform.query(coordinates);
    assertEquals(5 * 2,  transformed[0], 0);
    assertEquals(7 * 3,  transformed[1], 0);
    assertEquals(11 * 2, transformed[2], 0);
    assertEquals(13 * 3, transformed[3], 0);
}
 
Example #28
Source File: TestImageTransform.java    From DataVec with Apache License 2.0 6 votes vote down vote up
@Test
public void testRotateImageTransform() throws Exception {
    ImageWritable writable = makeRandomImage(0, 0, 1);
    Frame frame = writable.getFrame();
    ImageTransform transform =
                    new RotateImageTransform(rng, 180).interMode(INTER_NEAREST).borderMode(BORDER_REFLECT);

    for (int i = 0; i < 100; i++) {
        ImageWritable w = transform.transform(writable);
        Frame f = w.getFrame();
        assertEquals(f.imageHeight, frame.imageHeight);
        assertEquals(f.imageWidth, frame.imageWidth);
        assertEquals(f.imageChannels, frame.imageChannels);
    }
    assertEquals(null, transform.transform(null));

    transform = new RotateImageTransform(0, 0, -90, 0);
    writable = transform.transform(writable);
    float[] coordinates = {frame.imageWidth / 2, frame.imageHeight / 2, 0, 0};
    float[] transformed = transform.query(coordinates);
    assertEquals(frame.imageWidth  / 2, transformed[0], 0);
    assertEquals(frame.imageHeight / 2, transformed[1], 0);
    assertEquals((frame.imageHeight + frame.imageWidth) / 2, transformed[2], 1);
    assertEquals((frame.imageHeight - frame.imageWidth) / 2, transformed[3], 1);
}
 
Example #29
Source File: TestImageTransform.java    From DataVec with Apache License 2.0 6 votes vote down vote up
@Ignore
@Test
public void testFilterImageTransform() throws Exception {
    ImageWritable writable = makeRandomImage(0, 0, 4);
    Frame frame = writable.getFrame();
    ImageTransform transform = new FilterImageTransform("noise=alls=20:allf=t+u,format=rgba", frame.imageWidth,
                    frame.imageHeight, frame.imageChannels);

    for (int i = 0; i < 100; i++) {
        ImageWritable w = transform.transform(writable);
        Frame f = w.getFrame();
        assertEquals(f.imageHeight, frame.imageHeight);
        assertEquals(f.imageWidth, frame.imageWidth);
        assertEquals(f.imageChannels, frame.imageChannels);
    }
    assertEquals(null, transform.transform(null));
}
 
Example #30
Source File: TestImageTransform.java    From DataVec with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultiImageTransform() throws Exception {
    ImageWritable writable = makeRandomImage(0, 0, 3);
    Frame frame = writable.getFrame();
    ImageTransform transform = new MultiImageTransform(rng, new CropImageTransform(10),
                    new FlipImageTransform(), new ScaleImageTransform(10), new WarpImageTransform(10));

    for (int i = 0; i < 100; i++) {
        ImageWritable w = transform.transform(writable);
        Frame f = w.getFrame();
        assertTrue(f.imageHeight >= frame.imageHeight - 30);
        assertTrue(f.imageHeight <= frame.imageHeight + 20);
        assertTrue(f.imageWidth >= frame.imageWidth - 30);
        assertTrue(f.imageWidth <= frame.imageWidth + 20);
        assertEquals(f.imageChannels, frame.imageChannels);
    }
    assertEquals(null, transform.transform(null));

    transform = new MultiImageTransform(new ColorConversionTransform(COLOR_BGR2RGB));
    writable = transform.transform(writable);
    float[] transformed = transform.query(new float[] {11, 22});
    assertEquals(11, transformed[0], 0);
    assertEquals(22, transformed[1], 0);
}