public static <I, O> void map(final RandomAccessibleInterval<I> a, final IterableInterval<O> b, final UnaryComputerOp<I, O> op) { final RandomAccess<I> aAccess = a.randomAccess(); final Cursor<O> bCursor = b.localizingCursor(); while (bCursor.hasNext()) { bCursor.fwd(); aAccess.setPosition(bCursor); op.compute(aAccess.get(), bCursor.get()); } }
@Override public void add( final C c ) { sourceAccess.setPosition( 0, d ); c.sourceAccess.setPosition( 0, d ); while ( sourceAccess.getLongPosition( d ) < length ) { sourceAccess.get().add( c.sourceAccess.get() ); sourceAccess.fwd( d ); c.sourceAccess.fwd( d ); } }
@Override public void move( final int distance, final int d ) { assert d < n; position[ d ] += distance; if ( !sourceZero[ d ] ) s.move( distance, sourceComponent[ d ] ); }
@Override public void reset() { index = -1; maxIndexOnLine = dimensions[ 0 ] - 1; randomAccess.setPosition( min ); randomAccess.bck( 0 ); }
/** Gets this DrawingHelper's current drawing position. */ public void getPosition(final long[] position) { for (int i = 0; i < accessor.numDimensions(); i++) position[i] = accessor.getLongPosition(i); }
PairOfPointsNeighborhood( final long[] position, final long[] offset, final RandomAccess< T > sourceRandomAccess ) { super( position ); ra = sourceRandomAccess.copyRandomAccess(); this.offset = offset; this.ndims = sourceRandomAccess.numDimensions(); }
public void copyWithSourceIteration( final Img< IntType > srcImg, final Img< IntType > dstImg ) { final int[] pos = new int[ dimensions.length ]; final Cursor< IntType > src = srcImg.localizingCursor(); final RandomAccess< IntType > dst = dstImg.randomAccess(); while ( src.hasNext() ) { src.fwd(); src.localize( pos ); dst.setPosition( pos ); dst.get().set( src.get() ); } }
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() );
/** * 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; }
public static <I1, I2, O> void map(final IterableInterval<I1> a, final IterableInterval<I2> b, final RandomAccessibleInterval<O> c, final BinaryComputerOp<I1, I2, O> op) { final Cursor<I1> aCursor = a.localizingCursor(); final Cursor<I2> bCursor = b.cursor(); final RandomAccess<O> cAccess = c.randomAccess(); while (aCursor.hasNext()) { aCursor.fwd(); cAccess.setPosition(aCursor); op.compute(aCursor.get(), bCursor.next(), cAccess.get()); } }
/** * 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() ); } }
@Override public void run() { Cursor<FloatType> incrementCursor = increment.localizingCursor(); RandomAccess<T> ra = image.randomAccess(); float val, inc, sum; incrementCursor.reset(); incrementCursor.jumpFwd(chunk.getStartPosition()); for (long j = 0; j < chunk.getLoopSize(); j++) { inc = incrementCursor.next().get(); // FloatType, might be negative ra.setPosition(incrementCursor); val = ra.get().getRealFloat(); // T type, might be 0 // Over/Underflow protection sum = val + inc; if (sum > maxVal) { sum = maxVal; } if (sum < minVal) { sum = minVal; } ra.get().setReal(sum); } } };
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() ); } }
@Test public void testCopy() { ops.run(ConvertIIs.class, out, in, new CopyRealTypes<ShortType, ByteType>()); final Cursor<ShortType> c = in.localizingCursor(); final RandomAccess<ByteType> ra = out.randomAccess(); while (c.hasNext()) { final short value = c.next().get(); ra.setPosition(c); assertEquals(copy(value), ra.get().get()); } }
@Test public void testIntervalPermuteInverseCoordinates() { Img<DoubleType> img = ArrayImgs.doubles(2, 2); Cursor<DoubleType> c = img.cursor(); MersenneTwisterFast r = new MersenneTwisterFast(SEED); while (c.hasNext()) { c.next().set(r.nextDouble()); } IntervalView<DoubleType> expected = Views.permuteCoordinatesInverse(img, new int[]{0, 1}); Cursor<DoubleType> e = expected.cursor(); RandomAccessibleInterval<DoubleType> actual = ops.transform().permuteCoordinatesInverseView(img, new int[]{0, 1}); RandomAccess<DoubleType> actualRA = actual.randomAccess(); while (e.hasNext()) { e.next(); actualRA.setPosition(e); assertEquals(e.get().get(), actualRA.get().get(), 1e-10); } assertTrue(Intervals.equals(expected, actual)); }
@Test public void defaultPermuteCoordinatesTest() { Img<DoubleType> img = new ArrayImgFactory<DoubleType>().create(new int[]{2, 2}, new DoubleType()); Cursor<DoubleType> c = img.cursor(); MersenneTwisterFast r = new MersenneTwisterFast(SEED); while (c.hasNext()) { c.next().set(r.nextDouble()); } Cursor<DoubleType> il2 = Views.permuteCoordinates(img, new int[]{0, 1}).cursor(); RandomAccess<DoubleType> opr = ops.transform().permuteCoordinatesView(img, new int[]{0, 1}).randomAccess(); while (il2.hasNext()) { il2.next(); opr.setPosition(il2); assertEquals(il2.get().get(), opr.get().get(), 1e-10); } }
public static < T extends RealType< T > > double[] minMaxApprox( final RandomAccessibleInterval< T > img, final Random rnd, final int numPixels ) { final RandomAccess< T > ra = img.randomAccess(); // run threads and combine results double min = Double.MAX_VALUE; double max = -Double.MAX_VALUE; for ( int i = 0; i < numPixels; ++i ) { for ( int d = 0; d < img.numDimensions(); ++d ) ra.setPosition( rnd.nextInt( (int)img.dimension( d ) ) + (int)img.min( d ), d ); final double v = ra.get().getRealDouble(); min = Math.min( min, v ); max = Math.max( max, v ); } return new double[]{ min, max }; }
@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 ); } } };