private static < T extends RealType< T >, U extends RealType< U > > void transformL1AlongDimension( final RandomAccessible< T > source, final RandomAccessibleInterval< U > target, final int dim, final double weight ) { final int lastDim = target.numDimensions() - 1; final long size = target.dimension( dim ); final RealComposite< DoubleType > tmp = Views.collapseReal( createAppropriateOneDimensionalImage( size, new DoubleType() ) ).randomAccess().get(); // do not permute if we already work on last dimension final Cursor< RealComposite< T > > s = Views.flatIterable( Views.collapseReal( dim == lastDim ? Views.interval( source, target ) : Views.permute( Views.interval( source, target ), dim, lastDim ) ) ).cursor(); final Cursor< RealComposite< U > > t = Views.flatIterable( Views.collapseReal( dim == lastDim ? target : Views.permute( target, dim, lastDim ) ) ).cursor(); while ( s.hasNext() ) { final RealComposite< T > sourceComp = s.next(); final RealComposite< U > targetComp = t.next(); for ( long i = 0; i < size; ++i ) { tmp.get( i ).set( sourceComp.get( i ).getRealDouble() ); } transformL1SingleColumn( tmp, targetComp, weight, size ); } }
public static final < T extends RealType< T >> void addGaussianNoiseToImage( RandomAccessibleInterval< T > img, double sigma_noise ) { IterableInterval< T > iterImg = Views.iterable( img ); Cursor< T > lc = iterImg.localizingCursor(); double val; T var = iterImg.firstElement().createVariable(); while ( lc.hasNext() ) { lc.fwd(); val = Math.max( 0, sigma_noise * ran.nextGaussian() ); var.setReal( val ); lc.get().add( var ); } }
public void setKernel( final RandomAccessibleInterval< R > kernel ) { this.kernel = Views.extendValue( kernel, Util.getTypeFromInterval( kernel ).createVariable() ); this.kernelInterval = kernel; this.fftKernel = null; }
public ImageInterpolation( final Img< T > image, final InterpolatorFactory< T, RandomAccessible< T > > interpolatorFactory, final boolean mirror ) { this.image = image; this.interpolatorFactory = interpolatorFactory; if ( mirror ) this.interpolated = Views.interpolate( Views.extendMirrorSingle( image ), interpolatorFactory ); else this.interpolated = Views.interpolate( Views.extendZero( image ), interpolatorFactory ); }
public ExtendPeriodic(final RandomAccessibleInterval<T> img) { super(Views.interval(Views.extendPeriodic(img), img)); }
private static < T extends RealType< T >, U extends ComplexType< U > > RandomAccessibleInterval< U > calculateEigenValuesImpl( final RandomAccessibleInterval< T > tensor, final RandomAccessibleInterval< U > eigenvalues, final EigenValues< T, U > ev ) { final Cursor< RealComposite< T > > m = Views.iterable( Views.collapseReal( tensor ) ).cursor(); final Cursor< NumericComposite< U > > e = Views.iterable( Views.collapseNumeric( eigenvalues ) ).cursor(); while ( m.hasNext() ) ev.compute( m.next(), e.next() ); return eigenvalues; }
final Img< FloatType > img = factory.create( input, new FloatType() ); final IterableInterval< T > iterableInput = Views.iterable( input ); if ( img.iterationOrder().equals( iterableInput.iterationOrder() ) ) final Cursor< FloatType > out = img.cursor(); final Cursor< T > in = iterableInput.cursor(); while ( out.hasNext() ) out.fwd(); in.fwd(); out.get().set( in.get().getRealFloat() ); final Cursor< FloatType > out = img.localizingCursor(); final RandomAccess< T > in = input.randomAccess(); while ( out.hasNext() ) out.fwd(); in.setPosition( out ); out.get().set( in.get().getRealFloat() );
public static Pair< FloatProcessor, FloatProcessor > getTwoImagesA( final Img< FloatType > imgIn, final int z, final int dist ) { final RandomAccessible< FloatType > r = Views.extendMirrorSingle( imgIn ); final RandomAccessibleInterval< FloatType > img = Views.interval( r, imgIn ); final RandomAccessibleInterval< FloatType > s0 = Views.hyperSlice( img, 2, z ); final RandomAccessibleInterval< FloatType > s1 = Views.hyperSlice( img, 2, z + dist ); final FloatProcessor fp0 = new FloatProcessor( (int)s0.dimension( 0 ), (int)s0.dimension( 1 ) ); final FloatProcessor fp1 = new FloatProcessor( (int)s0.dimension( 0 ), (int)s0.dimension( 1 ) ); final Cursor< FloatType > c0 = Views.iterable( s0 ).localizingCursor(); final Cursor< FloatType > c1 = Views.iterable( s1 ).localizingCursor(); while ( c0.hasNext() ) { c0.fwd(); c1.fwd(); fp0.setf( c0.getIntPosition( 0 ), c0.getIntPosition( 1 ), c0.get().get() ); fp1.setf( c1.getIntPosition( 0 ), c1.getIntPosition( 1 ), c1.get().get() ); } return new ValuePair< FloatProcessor, FloatProcessor >( fp0, fp1 ); }
/** * Runs the operation. This operation fills the whole output image region * with the binary combination of the input values. The output region must be * contained within the extents of each of the input regions. */ @Override public RandomAccessibleInterval<W> compute(RandomAccessibleInterval<U> input1, RandomAccessibleInterval<V> input2, RandomAccessibleInterval<W> output) { long[] position = new long[output.numDimensions()]; Cursor<W> cursor = Views.iterable( output ).localizingCursor(); RandomAccess<U> accessor1 = input1.randomAccess(); RandomAccess<V> accessor2 = input2.randomAccess(); while (cursor.hasNext()) { cursor.fwd(); cursor.localize(position); accessor1.setPosition(position); accessor2.setPosition(position); operation.compute(accessor1.get(), accessor2.get(), cursor.get()); } return output; }
/** * copy src to dest * @param src : source, will not be modified * @param dest : destiantion, will be modified * @param <T> pixel type source * @param <S> pixel type destination */ public static <T extends RealType<T>, S extends RealType<S>> void copy(RandomAccessible< T > src, RandomAccessibleInterval< S > dest) { final Cursor< S > destCursor = Views.iterable( dest ).localizingCursor(); final RandomAccess< T > srcRA = src.randomAccess(); while (destCursor.hasNext()) { destCursor.fwd(); srcRA.setPosition( destCursor ); destCursor.get().setReal( srcRA.get().getRealDouble() ); } }
private void copy(final RandomAccessibleInterval<BitType> source, final RandomAccessibleInterval<BitType> target) { final IterableInterval<BitType> targetIt = Views.iterable(target); final IterableInterval<BitType> sourceIt = Views.iterable(source); if (sourceIt.iterationOrder().equals(targetIt.iterationOrder())) { final Cursor<BitType> targetCursor = targetIt.cursor(); final Cursor<BitType> sourceCursor = sourceIt.cursor(); while (sourceCursor.hasNext()) { targetCursor.fwd(); sourceCursor.fwd(); targetCursor.get().set(sourceCursor.get().get()); } } else { // Fallback to random access final RandomAccess<BitType> targetRA = target.randomAccess(); final Cursor<BitType> sourceCursor = sourceIt.localizingCursor(); while (sourceCursor.hasNext()) { sourceCursor.fwd(); targetRA.setPosition(sourceCursor); targetRA.get().set(sourceCursor.get().get()); } } }
public static <P extends RealType< P >, Q extends RealType< Q >> double getMeanCorrected(RandomAccessibleInterval< P > brightImg, RandomAccessibleInterval< Q > darkImg) { final RealSum sum = new RealSum(); long count = 0; final Cursor< P > brightCursor = Views.iterable( brightImg ).cursor(); final RandomAccess< Q > darkRA = darkImg.randomAccess(); while (brightCursor.hasNext()) { brightCursor.fwd(); darkRA.setPosition( brightCursor ); sum.add( brightCursor.get().getRealDouble() - darkRA.get().getRealDouble()); count++; } if (count == 0) return 0.0; else return sum.getSum() / count; }
public static < T extends RealType< T > > void subtractProjection( final RandomAccessibleInterval< T > img, final RandomAccessibleInterval< T > proj, final int projDim ) { final int n0 = img.numDimensions(); final Cursor< T > cursor = Views.iterable( img ).localizingCursor(); final RandomAccess< T > ra = proj.randomAccess(); while ( cursor.hasNext() ) { final T type = cursor.next(); int dim = 0; for ( int d = 0; d < n0; ++d ) if ( d != projDim ) ra.setPosition( cursor.getLongPosition( d ), dim++ ); type.sub( ra.get() ); } }
public Img<T> AddAndScale(final RandomAccessibleInterval<T> img1, final Img<T> img2, final float a) { Img<T> out = create.calculate(img1); final Cursor<T> cursor1 = Views.iterable(img1).cursor(); final Cursor<T> cursor2 = img2.cursor(); final Cursor<T> cursorOut = out.cursor(); while (cursor1.hasNext()) { cursor1.fwd(); cursor2.fwd(); cursorOut.fwd(); float val1 = cursor1.get().getRealFloat(); float val2 = cursor2.get().getRealFloat(); float val3 = Math.max(val1 + a * val2, 0.0001f); cursorOut.get().setReal(val3); } return out; } }
@Override public void run() { final T tmp = createVariable( target, source ); final Cursor< T > sourceCursor = Views.iterable( source ).localizingCursor(); sourceCursor.jumpFwd( chunk.getStartPosition() ); final RandomAccess< T > targetRandomAccess = target.randomAccess( source ); for ( long step = 0; step < chunk.getLoopSize(); step++ ) { } while ( sourceCursor.hasNext() ) { sourceCursor.fwd(); targetRandomAccess.setPosition( sourceCursor ); tmp.set( targetRandomAccess.get() ); tmp.sub( sourceCursor.get() ); targetRandomAccess.get().set( tmp ); } } };
@Override public Void call() throws Exception { final Cursor< UnsignedShortType > in = Views.iterable( ushortImg ).localizingCursor(); final RandomAccess< FloatType > out = floatImg.randomAccess(); in.jumpFwd( startPosition ); for ( long j = 0; j < loopSize; ++j ) { final UnsignedShortType vin = in.next(); out.setPosition( in ); out.get().set( vin.getRealFloat() ); } return null; } } );
public static < T extends RealType< T > > double[] centerofmass( final RandomAccessibleInterval< T > img, final double threshold ) { final IterableInterval< T > interval = Views.iterable( img ); final double[] center = new double[ img.numDimensions() ]; for ( int d = 0; d < img.numDimensions(); ++d ) { final RealSum s = new RealSum(); final RealSum i = new RealSum(); final Cursor< T > c = interval.localizingCursor(); while ( c.hasNext() ) { final double intensity = c.next().getRealDouble(); if ( intensity >= threshold && c.getIntPosition( 0 ) < 90 ) { s.add( intensity * c.getDoublePosition( d ) ); i.add( intensity ); } } center[ d ] = s.getSum() / i.getSum(); } return center; }
@Test public void defaultSubsampleTest() { Img<DoubleType> img = new ArrayImgFactory<DoubleType>().create(new int[] { 10, 10 }, new DoubleType()); MersenneTwisterFast r = new MersenneTwisterFast(SEED); for (DoubleType d : img) { d.set(r.nextDouble()); } SubsampleView<DoubleType> il2 = Views.subsample((RandomAccessible<DoubleType>) img, 2); SubsampleView<DoubleType> opr = ops.transform().subsampleView(img, 2); Cursor<DoubleType> il2C = Views.interval(il2, new long[] { 0, 0 }, new long[] { 4, 4 }).localizingCursor(); RandomAccess<DoubleType> oprRA = opr.randomAccess(); while (il2C.hasNext()) { il2C.next(); oprRA.setPosition(il2C); assertEquals(il2C.get().get(), oprRA.get().get(), 1e-10); } }
private void assertSum( final RandomAccessibleInterval< IntType > sum ) { final Cursor< IntType > a = Views.iterable( imageA ).cursor(); final Cursor< IntType > b = Views.iterable( imageB ).cursor(); final Cursor< IntType > s = Views.iterable( sum ).cursor(); while ( s.hasNext() ) assertEquals( s.next().get(), a.next().get() + b.next().get() ); }