Java Code Examples for org.datavec.image.data.ImageWritable#getFrame()
The following examples show how to use
org.datavec.image.data.ImageWritable#getFrame() .
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: ResizeImageTransformTest.java From DataVec with Apache License 2.0 | 6 votes |
@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 2
Source File: TestImageTransform.java From deeplearning4j with Apache License 2.0 | 6 votes |
@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 3
Source File: TestImageTransform.java From deeplearning4j with Apache License 2.0 | 6 votes |
@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 4
Source File: ResizeImageTransformTest.java From deeplearning4j with Apache License 2.0 | 6 votes |
@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 5
Source File: ImageWritableToINDArrayTransform.java From deeplearning4j with Apache License 2.0 | 6 votes |
@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 6
Source File: TestImageTransform.java From deeplearning4j with Apache License 2.0 | 6 votes |
@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 7
Source File: TestImageTransform.java From deeplearning4j with Apache License 2.0 | 6 votes |
@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 8
Source File: TestImageTransform.java From DataVec with Apache License 2.0 | 6 votes |
@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 9
Source File: TestImageTransform.java From DataVec with Apache License 2.0 | 6 votes |
@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: TestImageTransform.java From DataVec with Apache License 2.0 | 6 votes |
@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 11
Source File: TestImageTransform.java From deeplearning4j with Apache License 2.0 | 6 votes |
@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 12
Source File: TestImageTransform.java From DataVec with Apache License 2.0 | 6 votes |
@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 13
Source File: ResizeImageTransformTest.java From DataVec with Apache License 2.0 | 6 votes |
@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 14
Source File: TestImageTransform.java From DataVec with Apache License 2.0 | 5 votes |
@Test public void testWarpImageTransform() throws Exception { ImageWritable writable = makeRandomImage(0, 0, 1); Frame frame = writable.getFrame(); ImageTransform transform = new WarpImageTransform(rng, frame.imageWidth / 10).interMode(INTER_CUBIC) .borderMode(BORDER_REPLICATE); 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 WarpImageTransform(1, 2, 3, 4, 5, 6, 7, 8); writable = transform.transform(writable); float[] coordinates = { 0, 0, frame.imageWidth, 0, frame.imageWidth, frame.imageHeight, 0, frame.imageHeight}; float[] transformed = transform.query(coordinates); assertEquals(1, transformed[0], 0); assertEquals(2, transformed[1], 0); assertEquals(3 + frame.imageWidth, transformed[2], 0); assertEquals(4, transformed[3], 0); assertEquals(5 + frame.imageWidth, transformed[4], 0); assertEquals(6 + frame.imageHeight, transformed[5], 0); assertEquals(7, transformed[6], 0); assertEquals(8 + frame.imageHeight, transformed[7], 0); }
Example 15
Source File: TestImageTransform.java From DataVec with Apache License 2.0 | 5 votes |
@Test public void testProbabilisticPipelineTransform() throws Exception { ImageWritable writable = makeRandomImage(0, 0, 3); Frame frame = writable.getFrame(); ImageTransform randCrop = new RandomCropTransform(frame.imageHeight / 2, frame.imageWidth / 2); ImageTransform flip = new FlipImageTransform(); List<Pair<ImageTransform, Double>> pipeline = new LinkedList<>(); pipeline.add(new Pair<>(randCrop, 1.0)); pipeline.add(new Pair<>(flip, 0.5)); ImageTransform transform = new PipelineImageTransform(pipeline, true); 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(f.imageChannels, frame.imageChannels); } assertEquals(null, transform.transform(null)); transform = new PipelineImageTransform(new EqualizeHistTransform()); writable = transform.transform(writable); float[] transformed = transform.query(new float[] {88, 99}); assertEquals(88, transformed[0], 0); assertEquals(99, transformed[1], 0); }
Example 16
Source File: RandomCropTransform.java From deeplearning4j with Apache License 2.0 | 5 votes |
/** * Takes an image and returns a randomly cropped image. * * @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; } // ensure that transform is valid if (image.getFrame().imageHeight < outputHeight || image.getFrame().imageWidth < outputWidth) throw new UnsupportedOperationException( "Output height/width cannot be more than the input image. Requested: " + outputHeight + "+x" + outputWidth + ", got " + image.getFrame().imageHeight + "+x" + image.getFrame().imageWidth); // determine boundary to place random offset int cropTop = image.getFrame().imageHeight - outputHeight; int cropLeft = image.getFrame().imageWidth - outputWidth; Mat mat = converter.convert(image.getFrame()); int top = rng.nextInt(cropTop + 1); int left = rng.nextInt(cropLeft + 1); y = Math.min(top, mat.rows() - 1); x = Math.min(left, mat.cols() - 1); Mat result = mat.apply(new Rect(x, y, outputWidth, outputHeight)); return new ImageWritable(converter.convert(result)); }
Example 17
Source File: TestImageTransform.java From DataVec with Apache License 2.0 | 5 votes |
@Test public void testBoxImageTransform() throws Exception { ImageTransform transform = new BoxImageTransform(rng, 237, 242).borderValue(Scalar.GRAY); for (int i = 0; i < 100; i++) { ImageWritable writable = makeRandomImage(0, 0, i % 4 + 1); Frame frame = writable.getFrame(); ImageWritable w = transform.transform(writable); Frame f = w.getFrame(); assertEquals(237, f.imageWidth); assertEquals(242, f.imageHeight); assertEquals(frame.imageChannels, f.imageChannels); float[] coordinates = {1, 2, 3, 4, 0, 0}; float[] transformed = transform.query(coordinates); int x = (frame.imageWidth - f.imageWidth) / 2; int y = (frame.imageHeight - f.imageHeight) / 2; assertEquals(1 - x, transformed[0], 0); assertEquals(2 - y, transformed[1], 0); assertEquals(3 - x, transformed[2], 0); assertEquals(4 - y, transformed[3], 0); assertEquals( - x, transformed[4], 0); assertEquals( - y, transformed[5], 0); } assertEquals(null, transform.transform(null)); }
Example 18
Source File: TestImageTransform.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test public void testBoxImageTransform() throws Exception { ImageTransform transform = new BoxImageTransform(rng, 237, 242).borderValue(Scalar.GRAY); for (int i = 0; i < 100; i++) { ImageWritable writable = makeRandomImage(0, 0, i % 4 + 1); Frame frame = writable.getFrame(); ImageWritable w = transform.transform(writable); Frame f = w.getFrame(); assertEquals(237, f.imageWidth); assertEquals(242, f.imageHeight); assertEquals(frame.imageChannels, f.imageChannels); float[] coordinates = {1, 2, 3, 4, 0, 0}; float[] transformed = transform.query(coordinates); int x = (frame.imageWidth - f.imageWidth) / 2; int y = (frame.imageHeight - f.imageHeight) / 2; assertEquals(1 - x, transformed[0], 0); assertEquals(2 - y, transformed[1], 0); assertEquals(3 - x, transformed[2], 0); assertEquals(4 - y, transformed[3], 0); assertEquals( - x, transformed[4], 0); assertEquals( - y, transformed[5], 0); } assertEquals(null, transform.transform(null)); }
Example 19
Source File: RandomCropTransform.java From DataVec with Apache License 2.0 | 5 votes |
/** * Takes an image and returns a randomly cropped image. * * @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; } // ensure that transform is valid if (image.getFrame().imageHeight < outputHeight || image.getFrame().imageWidth < outputWidth) throw new UnsupportedOperationException( "Output height/width cannot be more than the input image. Requested: " + outputHeight + "+x" + outputWidth + ", got " + image.getFrame().imageHeight + "+x" + image.getFrame().imageWidth); // determine boundary to place random offset int cropTop = image.getFrame().imageHeight - outputHeight; int cropLeft = image.getFrame().imageWidth - outputWidth; Mat mat = converter.convert(image.getFrame()); int top = rng.nextInt(cropTop + 1); int left = rng.nextInt(cropLeft + 1); y = Math.min(top, mat.rows() - 1); x = Math.min(left, mat.cols() - 1); Mat result = mat.apply(new Rect(x, y, outputWidth, outputHeight)); return new ImageWritable(converter.convert(result)); }
Example 20
Source File: TestImageTransform.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test public void testWarpImageTransform() throws Exception { ImageWritable writable = makeRandomImage(0, 0, 1); Frame frame = writable.getFrame(); ImageTransform transform = new WarpImageTransform(rng, frame.imageWidth / 10).interMode(INTER_CUBIC) .borderMode(BORDER_REPLICATE); 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 WarpImageTransform(1, 2, 3, 4, 5, 6, 7, 8); writable = transform.transform(writable); float[] coordinates = { 0, 0, frame.imageWidth, 0, frame.imageWidth, frame.imageHeight, 0, frame.imageHeight}; float[] transformed = transform.query(coordinates); assertEquals(1, transformed[0], 0); assertEquals(2, transformed[1], 0); assertEquals(3 + frame.imageWidth, transformed[2], 0); assertEquals(4, transformed[3], 0); assertEquals(5 + frame.imageWidth, transformed[4], 0); assertEquals(6 + frame.imageHeight, transformed[5], 0); assertEquals(7, transformed[6], 0); assertEquals(8 + frame.imageHeight, transformed[7], 0); }