public static Interval scaleBoundingBox( final Interval boundingBox, final double[] factors ) { final int n = boundingBox.numDimensions(); final long[] min = new long[ n ]; final long[] max = new long[ n ]; for ( int d = 0; d < min.length; ++ d ) { min[ d ] = Math.round( boundingBox.min( d ) * factors[d] ); max[ d ] = Math.round( boundingBox.max( d ) * factors[d] ); } return new FinalInterval( min, max ); }
/** * Create a {@link FinalInterval} from a parameter list comprising minimum * and maximum coordinates. For example, to create a 2D interval from (10, * 10) to (20, 40) use createMinMax( 10, 10, 20, 40 ). * * @param minmax * a list of <em>2*n</em> parameters to create a <em>n</em> * -dimensional interval. The first <em>n</em> parameters specify * the minimum of the interval, the next <em>n</em> parameters * specify the maximum of the interval. * @return interval with the specified boundaries */ public static FinalInterval createMinMax( final long... minmax ) { return FinalInterval.createMinMax( minmax ); }
private static <T> RandomAccessibleInterval<T> cropAt( final RandomAccessibleInterval<T> image, final int[] blockSize, final Localizable offset) { final int numDims = image.numDimensions(); final long[] minsize = new long[numDims * 2]; for (int d = 0; d < numDims; d++) { minsize[d] = offset.getLongPosition(d); final long shaveSize = image.dimension(d) % blockSize[d]; minsize[numDims + d] = image.dimension(d) - shaveSize; } return Views.interval(image, FinalInterval.createMinSize(minsize)); } }
min[ d ] = max[ d ] = position[ d ]; min[ dimX ] = target.min( dimX ); min[ dimY ] = target.min( dimY ); max[ dimX ] = target.max( dimX ); max[ dimY ] = target.max( dimY ); final FinalInterval sourceInterval = new FinalInterval( min, max ); final long cr = -target.dimension( dimX ); final RandomAccess< B > targetRandomAccess = target.randomAccess( target ); final RandomAccess< A > sourceRandomAccess = source.randomAccess( sourceInterval ); final long width = target.dimension( dimX ); final long height = target.dimension( dimY ); sourceRandomAccess.setPosition( min ); targetRandomAccess.setPosition( min[ dimX ], dimX ); targetRandomAccess.setPosition( min[ dimY ], dimY ); for ( long y = 0; y < height; ++y )
@Test public void defaultUnshearTest() { Img<DoubleType> img = new ArrayImgFactory<DoubleType>().create(new int[] { 2, 2 }, new DoubleType()); Cursor<DoubleType> imgC = img.cursor(); while (imgC.hasNext()) { imgC.next().set(1); } TransformView<DoubleType> il2 = Views.unshear(Views.shear(Views.extendZero(img), 0, 1), 0, 1); TransformView<DoubleType> opr = ops.transform().unshearView(Views.shear(Views.extendZero(img), 0, 1), 0, 1); Cursor<DoubleType> il2C = Views.interval(il2, new FinalInterval(new long[] { 0, 0 }, new long[] { 3, 3 })) .cursor(); RandomAccess<DoubleType> oprRA = Views .interval(opr, new FinalInterval(new long[] { 0, 0 }, new long[] { 3, 3 })).randomAccess(); while (il2C.hasNext()) { il2C.next(); oprRA.setPosition(il2C); assertEquals(il2C.get().get(), oprRA.get().get(), 1e-10); } }
public DiamondNeighborhoodLocalizableSampler( final RandomAccessible< T > source, final long radius, final DiamondNeighborhoodFactory< T > factory, Interval accessInterval ) { super( source.numDimensions() ); this.source = source; this.radius = radius; this.factory = factory; this.currentPos = new long[ n ]; if ( accessInterval == null && source instanceof Interval ) accessInterval = ( Interval ) source; if ( accessInterval == null ) { sourceInterval = null; } else { final long[] accessMin = new long[ n ]; final long[] accessMax = new long[ n ]; accessInterval.min( accessMin ); accessInterval.max( accessMax ); for ( int d = 0; d < n; ++d ) { accessMin[ d ] = currentPos[ d ] - radius; accessMax[ d ] = currentPos[ d ] + radius; } sourceInterval = new FinalInterval( accessMin, accessMax ); } this.currentNeighborhood = factory.create( currentPos, radius, sourceInterval == null ? source.randomAccess() : source.randomAccess( sourceInterval ) ); }
public static < T extends RealType< T > > void downsample( final RandomAccessible< T > input, final RandomAccessibleInterval< T > output, final int[] factor ) assert input.numDimensions() == output.numDimensions(); assert input.numDimensions() == factor.length; final int n = input.numDimensions(); final RectangleNeighborhoodFactory< T > f = RectangleNeighborhoodUnsafe.< T >factory(); final long[] dim = new long[ n ]; for ( int d = 0; d < n; ++d ) dim[ d ] = factor[ d ]; final Interval spanInterval = new FinalInterval( dim ); output.min( minRequiredInput ); output.max( maxRequiredInput ); for ( int d = 0; d < n; ++d ) maxRequiredInput[ d ] += factor[ d ] - 1; final RandomAccessibleInterval< T > requiredInput = Views.interval( input, new FinalInterval( minRequiredInput, maxRequiredInput ) ); final double scale = 1.0 / size; final Cursor< T > out = Views.iterable( output ).localizingCursor(); while( out.hasNext() )
max[ 1 ] = target.max( 1 ); final IterableInterval< A > srcIterable = Views.iterable( Views.interval( source, new FinalInterval( min, max ) ) ); final Cursor< B > targetCursor = target.localizingCursor(); final RandomAccess< A > sourceRandomAccess = source.randomAccess(); while ( targetCursor.hasNext() ) sourceRandomAccess.setPosition( targetCursor.getLongPosition( 0 ), 0 ); sourceRandomAccess.setPosition( targetCursor.getLongPosition( 1 ), 1 ); converter.convert( sourceRandomAccess.get(), b );
@Override public Void call() final RandomAccess< S > in = source.randomAccess( new FinalInterval( srcmin, srcmax ) ); final RandomAccess< T > out = target.randomAccess( targetInterval ); final Runnable convolver = factory.create( halfkernel, in, out, d, targetInterval.dimension( d ) ); out.setPosition( min ); in.setPosition( srcmin ); out.move( moveToStart ); in.move( moveToStart );
private static final void copy( final long start, final long loopSize, final RandomAccessible< FloatType > source, final RandomAccessibleInterval< FloatType > block, final long[] offset ) { final int numDimensions = source.numDimensions(); final Cursor< FloatType > cursor = Views.iterable( block ).localizingCursor(); // define where we will query the RandomAccess on the source // (we say it is the entire block, although it is just a part of it, // but which part depends on the underlying container) final long[] min = new long[ numDimensions ]; final long[] max = new long[ numDimensions ]; for ( int d = 0; d < numDimensions; ++d ) { min[ d ] = offset[ d ]; max[ d ] = offset[ d ] + block.dimension( d ) - 1; } final RandomAccess< FloatType > randomAccess = source.randomAccess( new FinalInterval( min, max ) ); cursor.jumpFwd( start ); final long[] tmp = new long[ numDimensions ]; for ( long l = 0; l < loopSize; ++l ) { cursor.fwd(); cursor.localize( tmp ); for ( int d = 0; d < numDimensions; ++d ) tmp[ d ] += offset[ d ]; randomAccess.setPosition( tmp ); cursor.get().set( randomAccess.get() ); } }
@SuppressWarnings( "unchecked" ) public MoveAllSlicesRA( final RandomAccessibleInterval< T >[] slices, final Interval interval ) { n = slices[ 0 ].numDimensions() + 1; sd = n - 1; slice = 0; tmpLong = new long[ sd ]; tmpInt = new int[ sd ]; sliceAccesses = new RandomAccess[ slices.length ]; if ( interval == null ) { for ( int i = 0; i < slices.length; ++i ) sliceAccesses[ i ] = slices[ i ].randomAccess(); } else { final long[] smin = new long[ sd ]; final long[] smax = new long[ sd ]; for ( int d = 0; d < sd; ++d ) { smin[ d ] = interval.min( d ); smax[ d ] = interval.max( d ); } final Interval sliceInterval = new FinalInterval( smin, smax ); for ( int i = 0; i < slices.length; ++i ) sliceAccesses[ i ] = slices[ i ].randomAccess( sliceInterval ); } }
@Override protected void process( final RandomAccessible< ? extends T > source, final RandomAccessibleInterval< ? extends T > target, final ExecutorService executorService, final int numThreads ) { final RandomAccessibleInterval< ? extends T > sourceInterval = Views.interval( source, requiredSourceInterval( target ) ); final long[] sourceMin = Intervals.minAsLongArray( sourceInterval ); final long[] targetMin = Intervals.minAsLongArray( target ); final Supplier< Consumer< Localizable > > actionFactory = () -> { final RandomAccess< ? extends T > in = sourceInterval.randomAccess(); final RandomAccess< ? extends T > out = target.randomAccess(); final Runnable convolver = factory.getConvolver( in, out, direction, target.dimension( direction ) ); return position -> { in.setPosition( sourceMin ); out.setPosition( targetMin ); in.move( position ); out.move( position ); convolver.run(); }; }; final long[] dim = Intervals.dimensionsAsLongArray( target ); dim[ direction ] = 1; final int numTasks = numThreads > 1 ? timesFourAvoidOverflow(numThreads) : 1; LineConvolution.forEachIntervalElementInParallel( executorService, numTasks, new FinalInterval( dim ), actionFactory ); }
final FinalInterval interval = new FinalInterval( new long[] { offsetX, offsetY, offsetZ }, new long[] { offsetX + w - 1, offsetY + h - 1, offsetZ + d - 1 } ); final RandomAccess< FloatType > randomAccess = source.randomAccess( interval ); randomAccess.setPosition( tmp ); randomAccess.setPosition( offsetX, 0 ); blockArray[ i++ ] = randomAccess.get().get(); randomAccess.fwd( 0 );
@Override public RandomAccessibleInterval<T> calculate(RandomAccessibleInterval<T> input) { final long[] newDims = Intervals.dimensionsAsLongArray(in()); for (int i = 0; i < Math.min(scaleFactors.length, in().numDimensions()); i++) { newDims[i] = Math.round(in().dimension(i) * scaleFactors[i]); } IntervalView<T> interval = Views.interval(Views.raster(RealViews.affineReal( Views.interpolate(Views.extendMirrorSingle(input), interpolator), new Scale(scaleFactors))), new FinalInterval(newDims)); return interval; }
@Before public void createSourceData() { dimensions = new long[] { 23, 31, 11, 7, 3 }; intervalLine = new FinalInterval( new long[] { 0, 12, 3, 5, 1 }, new long[] { dimensions[ 0 ] - 1, 13, 3, 5, 1 } ); intervalShifted = new FinalInterval( new long[] { 0, 0, 3, 5, 1 }, new long[] { dimensions[ 0 ] - 1, dimensions[ 1 ] - 1, 4, 5, 1 } ); intervalFast = new FinalInterval( new long[] { dimensions[ 0 ], dimensions[ 1 ], 5, 1, 1 } ); intervalFastPart = new FinalInterval( new long[] { dimensions[ 0 ], 2, 3, 1, 1 } ); intervalSinglePlaneShifted = new FinalInterval( new long[] { 0, 0, 3, 5, 1 }, new long[] { dimensions[ 0 ] - 1, dimensions[ 1 ] - 1, 3, 5, 1 } ); intervalSinglePlaneFull = new FinalInterval( new long[] { 0, 0, 1, 1, 1 }, new long[] { dimensions[ 0 ] - 1, dimensions[ 1 ] - 1, 1, 1, 1 } ); // create random data for all dims and fill the planar img numValues = 1; for ( int d = 0; d < dimensions.length; ++d ) numValues *= dimensions[ d ]; intData = new int[ numValues ]; Random random = new Random( 0 ); for ( int i = 0; i < numValues; ++i ) { intData[ i ] = random.nextInt(); } img = new PlanarImgFactory<>( new IntType() ).create( dimensions ); long[] pos = new long[ dimensions.length ]; RandomAccess< IntType > a = img.randomAccess(); for ( int i = 0; i < numValues; ++i ) { IntervalIndexer.indexToPosition( i, dimensions, pos ); a.setPosition( pos ); a.get().set( intData[ i ] ); } }
@Override @SuppressWarnings("unchecked") public O calculate(final I input, final Interval centeredInterval) { int numDimensions = input.numDimensions(); // compute where to place the final Interval for the input so that the // coordinate in the center // of the input is at position (0,0). final long[] min = new long[numDimensions]; final long[] max = new long[numDimensions]; for (int d = 0; d < numDimensions; ++d) { min[d] = input.min(d) + input.dimension(d) / 2; max[d] = min[d] + centeredInterval.dimension(d) - 1; } return (O) new FinalInterval(min, max); } }
/** * Creates a virtual construct that transforms and zero-mins using a user-defined outofbounds & interpolation. * * @param input - the input image * @param transform - the affine transformation * @param boundingBox - the bounding box (after transformation) * @param outOfBoundsFactory - outofboundsfactory * @param interpolatorFactory - any interpolatorfactory * @param <T> type * @return transformed image */ public static < T extends RealType< T > > RandomAccessibleInterval< FloatType > transformView( final RandomAccessibleInterval< T > input, final AffineTransform3D transform, final Interval boundingBox, final OutOfBoundsFactory< T, RandomAccessible< T > > outOfBoundsFactory, final InterpolatorFactory< FloatType, RandomAccessible< FloatType > > interpolatorFactory ) { final long[] offset = new long[ input.numDimensions() ]; final long[] size = new long[ input.numDimensions() ]; for ( int d = 0; d < offset.length; ++d ) { offset[ d ] = boundingBox.min( d ); size[ d ] = boundingBox.dimension( d ); } final TransformedInputGeneralRandomAccessible< T > virtual = new TransformedInputGeneralRandomAccessible< T >( input, transform, outOfBoundsFactory, interpolatorFactory, offset ); return Views.interval( virtual, new FinalInterval( size ) ); }
public RectangleNeighborhoodLocalizableSampler( final RandomAccessibleInterval< T > source, final Interval span, final RectangleNeighborhoodFactory< T > factory ) { super( source ); this.source = source; this.span = span; neighborhoodFactory = factory; currentPos = new long[ n ]; currentMin = new long[ n ]; currentMax = new long[ n ]; final long[] accessMin = new long[ n ]; final long[] accessMax = new long[ n ]; source.min( accessMin ); source.max( accessMax ); for ( int d = 0; d < n; ++d ) { accessMin[ d ] += span.min( d ); accessMax[ d ] += span.max( d ); } currentNeighborhood = neighborhoodFactory.create( currentPos, currentMin, currentMax, span, source.randomAccess( new FinalInterval( accessMin, accessMax ) ) ); }
final static private String printHyperSlice( final RandomAccess< HyperSlice< LongType > > hyperSlicesAccess, final int... axes ) { final long[] max = new long[ axes.length ]; for ( int d = 0; d < axes.length; ++d ) max[ d ] = sizes[ axes[ d ] ] - 1; final RandomAccessibleInterval< LongType > hyperSlice = Views.interval( hyperSlicesAccess.get(), new FinalInterval( new long[ axes.length ], max ) ); long n = 1; for ( final long s : max ) n *= s + 1; final long[] list = new long[ ( int )n ]; int i = 0; for ( final LongType t : Views.iterable( hyperSlice ) ) list[ i++ ] = t.get(); return Arrays.toString( list ); }
/** * * Switch for calling convenience method with pre-defined distances. * */ public static enum DISTANCE_TYPE { /** * Squared Euclidian distance using {@link EuclidianDistanceIsotropic} or * {@link EuclidianDistanceAnisotropic}. */ EUCLIDIAN, /** * L1 distance using special case implementation. */ L1 }