@Override public IntervalView<T> calculate(final RandomAccessibleInterval<T> input) { return Views.translate(input, translation); }
@Override public MixedTransformView<T> calculate(RandomAccessible<T> input) { return Views.translate(input, translation); }
/** * Translate the source view by the given translation vector. Pixel * <em>x</em> in the source view has coordinates <em>(x + translation)</em> * in the resulting view. * * @param interval * the source * @param translation * translation vector of the source view. The pixel at <em>x</em> * in the source view becomes <em>(x + translation)</em> in the * resulting view. */ public static < T > IntervalView< T > translate( final RandomAccessibleInterval< T > interval, final long... translation ) { return Views.interval( Views.translate( ( RandomAccessible< T > ) interval, translation ), Intervals.translate( interval, translation ) ); }
/** * Translate the source view by the given translation vector. Pixel * <em>x</em> in the source view has coordinates <em>(x + translation)</em> * in the resulting view. * * @param interval * the source * @param translation * translation vector of the source view. The pixel at <em>x</em> * in the source view becomes <em>(x + translation)</em> in the * resulting view. */ public static < T > IntervalView< T > translate( final RandomAccessibleInterval< T > interval, final long... translation ) { return Views.interval( Views.translate( ( RandomAccessible< T > ) interval, translation ), Intervals.translate( interval, translation ) ); }
/** * Creates an image with randomized content. * @param type Pixel type * @param interval Interval */ public < T extends NativeType< T > > RandomAccessibleInterval< T > nextImage( final T type, Interval interval ) { long[] sizes = Intervals.dimensionsAsLongArray( interval ); long[] min = Intervals.minAsLongArray( interval ); return Views.translate( nextImage( type, sizes ), min ); }
@Override public void compute(final RandomAccessibleInterval<T> in1, final Shape in2, final IterableInterval<T> output) { final RandomAccessibleInterval<T> shifted; if (isFull) { final long[] offset = MorphologyUtils .computeTargetImageDimensionsAndOffset(in1, in2)[1]; shifted = Views.translate(in1, offset); } else { shifted = in1; } final ExtendedRandomAccessibleInterval<T, RandomAccessibleInterval<T>> extended = Views.extend(shifted, f); Dilation.dilate(extended, output, in2, minVal, Runtime.getRuntime() .availableProcessors()); } }
@Override public void compute(final RandomAccessibleInterval<T> in1, final Shape in2, final IterableInterval<T> output) { final RandomAccessibleInterval<T> shifted; if (isFull) { final long[] offset = MorphologyUtils .computeTargetImageDimensionsAndOffset(in1, in2)[1]; shifted = Views.translate(in1, offset); } else { shifted = in1; } final ExtendedRandomAccessibleInterval<T, RandomAccessibleInterval<T>> extended = Views.extend(shifted, f); Erosion.erode(extended, output, in2, maxVal, Runtime.getRuntime() .availableProcessors()); } }
/** * Creates an image with randomized content. * @param type Pixel type * @param interval Interval */ public < T extends NativeType< T > > RandomAccessibleInterval< T > nextImage( final T type, Interval interval ) { long[] sizes = Intervals.dimensionsAsLongArray( interval ); long[] min = Intervals.minAsLongArray( interval ); return Views.translate( nextImage( type, sizes ), min ); }
public static < T > RandomAccessibleInterval< T > translateIfNecessary( final Interval original, final RandomAccessibleInterval< T > copy ) { if ( Views.isZeroMin( original ) ) { return copy; } else { final long[] min = new long[ original.numDimensions() ]; original.min( min ); return Views.translate( copy, min ); } }
private static < T extends NativeType< T > > RandomAccessibleInterval< T > createImage( final T targetType, final Interval targetInterval ) { final long[] dimensions = Intervals.dimensionsAsLongArray( targetInterval ); final Img< T > ts = Util.getArrayOrCellImgFactory( targetInterval, targetType ).create( dimensions ); return Views.translate( ts, Intervals.minAsLongArray( targetInterval ) ); }
private RandomAccessibleInterval< IntType > randomImage( final int randomSeed ) { final Img< IntType > result = ArrayImgs.ints( 3, 2, 5 ); final Random random = new Random( randomSeed ); result.forEach( x -> x.set( random.nextInt() ) ); return Views.translate( result, random.nextInt(), random.nextInt(), random.nextInt() ); }
@Override public void compute(final RandomAccessibleInterval<T> in1, final List<Shape> in2, final IterableInterval<T> out) { final long[][] minSize = Morphologies.computeMinSize(in1, in2); final Interval interval = new FinalInterval(minSize[1]); Img<T> upstream = imgCreator.calculate(interval); Img<T> downstream = imgCreator.calculate(interval); Img<T> tmp; dilateComputer.compute(in1, in2.get(0), Views.translate(downstream, minSize[0])); for (int i = 1; i < in2.size(); i++) { // Ping-ponging intermediate results between upstream and downstream to // avoid repetitively creating new Imgs. tmp = downstream; downstream = upstream; upstream = tmp; dilateComputer.compute(upstream, in2.get(i), downstream); } if (isFull) copyImg.compute(downstream, out); else copyImg.compute(Views.interval(Views.translate(downstream, minSize[0]), out), out); } }
@Override public void compute(final RandomAccessibleInterval<T> in1, final List<Shape> in2, final IterableInterval<T> out) { final long[][] minSize = Morphologies.computeMinSize(in1, in2); final Interval interval = new FinalInterval(minSize[1]); Img<T> upstream = imgCreator.calculate(interval); Img<T> downstream = imgCreator.calculate(interval); Img<T> tmp; erodeComputer.compute(in1, in2.get(0), Views.translate(downstream, minSize[0])); for (int i = 1; i < in2.size(); i++) { // Ping-ponging intermediate results between upstream and downstream to // avoid repetitively creating new Imgs. tmp = downstream; downstream = upstream; upstream = tmp; erodeComputer.compute(Views.interval(Views.extendValue(upstream, maxVal), interval), in2.get(i), downstream); } if (isFull) copyImg.compute(downstream, out); else copyImg.compute(Views.interval(Views.translate(downstream, minSize[0]), out), out); } }
/** * Adjusts the given {@link Img} to match the bounds of the specified * {@link Interval}. * * @param img An image whose min/max bounds might need adjustment. * @param minMax An {@link Interval} whose min/max bounds to use when * adjusting the image. If the provided {@code minMax} object is not * an {@link Interval}, no adjustment is performed. * @return A wrapped version of the input {@link Img} with bounds adjusted to * match the provided {@link Interval}, if any; or the input image * itself if no adjustment was needed/possible. */ public static <T extends Type<T>> Img<T> adjustMinMax(final Img<T> img, final Object minMax) { if (!(minMax instanceof Interval)) return img; final Interval interval = (Interval) minMax; final long[] min = new long[interval.numDimensions()]; interval.min(min); for (int d = 0; d < min.length; d++) { if (min[d] != 0) { return ImgView.wrap(Views.translate(img, min), img.factory()); } } return img; }
@Override public void compute(final RandomAccessibleInterval<T> input, final RandomAccessibleInterval<T> output) { // input may potentially be translated final long[] translation = new long[input.numDimensions()]; input.min(translation); final IntervalView<T> tmpInterval = Views.interval(Views.translate( (RandomAccessible<T>) tmpCreator.calculate(input), translation), output); gauss1.compute(input, tmpInterval); gauss2.compute(input, output); // TODO: Match the Subtract Op in initialize() once we have BinaryOp ops().run(Ops.Math.Subtract.class, output, output, tmpInterval); }
@Override public ImageRoi convert(final RealMaskRealInterval mask) { // Wrap mask as RRARI final RealRandomAccessibleRealInterval<BoolType> rrari = Masks .toRealRandomAccessibleRealInterval(mask); // Convert the RRARI to a RAI whose min is (0, 0), this will ensure it // displays properly final RandomAccessible<BoolType> raster = Views.raster(rrari); final RandomAccessible<BoolType> translate = Views.translate(raster, new long[] { (long) -mask.realMin(0), (long) -mask.realMin(1) }); final RandomAccessibleInterval<BoolType> rai = Views.interval(translate, new long[] { 0, 0 }, new long[] { (long) (mask.realMax(0) - mask.realMin( 0)), (long) (mask.realMax(1) - mask.realMin(1)) }); // Convert RAI to ImagePlus final Dataset d = datasetService.create(rai); final ImagePlus ip = convertService.convert(d, ImagePlus.class); return new ImageRoi((int) mask.realMin(0), (int) mask.realMin(1), ip .getBufferedImage()); }
@Test public void defaultZeroMinTest() { Img<DoubleType> img = new ArrayImgFactory<DoubleType>().create(new int[] { 10, 10 }, new DoubleType()); IntervalView<DoubleType> imgTranslated = Views.interval( Views.translate((RandomAccessible<DoubleType>) img, 2, 5), new long[] { 2, 5 }, new long[] { 12, 15 }); IntervalView<DoubleType> il2 = Views.zeroMin(imgTranslated); IntervalView<DoubleType> opr = ops.transform().zeroMinView(imgTranslated); assertTrue(Views.isZeroMin(il2) == Views.isZeroMin(opr)); } }
@Test public void defaultTranslateTest() { Img<DoubleType> img = new ArrayImgFactory<DoubleType>().create(new int[] { 10, 10 }, new DoubleType()); MixedTransformView<DoubleType> il2 = Views.translate( deinterval(img), 2, 5); MixedTransformView<DoubleType> opr = ops.transform().translateView( deinterval(img), 2, 5); for (int i = 0; i < il2.getTransformToSource().getMatrix().length; i++) { for (int j = 0; j < il2.getTransformToSource().getMatrix()[i].length; j++) { assertEquals(il2.getTransformToSource().getMatrix()[i][j], opr.getTransformToSource().getMatrix()[i][j], 1e-10); } } }
@Test public void testIntervalTranslate() { Img<DoubleType> img = ArrayImgs.doubles(10,10); IntervalView<DoubleType> expected = Views.translate(img, 2, 5); IntervalView<DoubleType> actual = ops.transform().translateView(img, 2, 5); for (int i = 0; i < ((MixedTransformView<DoubleType>) expected.getSource()).getTransformToSource().getMatrix().length; i++) { for (int j = 0; j < ((MixedTransformView<DoubleType>) expected.getSource()).getTransformToSource().getMatrix()[i].length; j++) { assertEquals(((MixedTransformView<DoubleType>) expected.getSource()).getTransformToSource().getMatrix()[i][j], ((MixedTransformView<DoubleType>) actual.getSource()).getTransformToSource().getMatrix()[i][j], 1e-10); } } assertTrue(Intervals.equals(expected, actual)); } }
protected void testCursorIteration( Cursor< IntType > cursor, Interval i ) { long[] position = new long[ cursor.numDimensions() ]; long[] min = new long[ cursor.numDimensions() ]; i.min( min ); cursor.fwd(); cursor.localize( position ); cursor.reset(); int ctr = 0; long sum = 0; final RandomAccess< BitType > check = Views.translate( new ArrayImgFactory<>( new BitType() ).create( i ), min ).randomAccess(); while ( cursor.hasNext() ) { cursor.fwd(); cursor.localize( position ); check.setPosition( position ); assertFalse( check.get().get() ); check.get().set( true ); sum += cursor.get().get(); ctr++; } assertEquals( "wrong number of elements accessed.", getIntervalSize( i ), ctr ); assertEquals( "sum of elements incorrect.", sum, getSum( i ) ); }