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 ); } }
@Override public void compute(final IterableInterval<I> input, final O output) { double moment00 = 0; final Cursor<I> cur = input.localizingCursor(); while (cur.hasNext()) { cur.fwd(); double val = cur.get().getRealDouble(); moment00 += val; } output.setReal(moment00); } }
@Override public void compute(final Iterable<T> input, final T output) { // Re-use output to compare against output.setReal(output.getMinValue()); for (final T in : input) if (output.compareTo(in) < 0) output.set(in); }
protected ClampingNLinearInterpolatorRealType( final RandomAccessible< T > randomAccessible, final T type ) { super( randomAccessible, type ); clampMin = type.getMinValue(); clampMax = type.getMaxValue(); }
@Override public void compute( final A input1, final B input2, final C output ) { output.setReal( ( input1.getRealDouble() - input2.getRealDouble() ) * normalizationFactor ); }
@Override protected V getVMinValue( final V var ) { var.setReal( var.getMinValue() ); return var; }
private RandomAccessibleInterval<T> project( final RandomAccessibleInterval<T> image) if (image.numDimensions() < 2) { throw new IllegalArgumentException("Dimensionality too small: " + // image.numDimensions()); final IterableInterval<T> input = Views.iterable(image); final T type = input.firstElement(); // e.g. unsigned 8-bit final long xLen = image.dimension(0); final long yLen = image.dimension(1); final Img<T> output = new ArrayImgFactory<T>().create(outputDims, type); for (final T sample : output) { sample.setReal(type.getMinValue()); final Cursor<T> inPos = input.localizingCursor(); final RandomAccess<T> outPos = output.randomAccess(); while (inPos.hasNext()) { final T inPix = inPos.next(); final long xPos = inPos.getLongPosition(0); final long yPos = inPos.getLongPosition(1); outPos.setPosition(xPos, 0); outPos.setPosition(yPos, 1); final T outPix = outPos.get(); if (outPix.compareTo(inPix) < 0) { outPix.set(inPix);
private void copyDataValues(final Img<? extends RealType<?>> input, final Img<? extends RealType<?>> output) { final long[] position = new long[output.numDimensions()]; final Cursor<? extends RealType<?>> outputCursor = output.localizingCursor(); final RandomAccess<? extends RealType<?>> inputAccessor = input.randomAccess(); while (outputCursor.hasNext()) { outputCursor.next(); outputCursor.localize(position); inputAccessor.setPosition(position); final double value = inputAccessor.get().getRealDouble(); outputCursor.get().setReal(value); } }
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 final <T extends RealType<T>> void addGaussianNoiseToImage(Img<T> img, double sigma_noise) { Cursor<T> lc = img.localizingCursor(); double val; T var = img.firstElement().createVariable(); while (lc.hasNext()) { lc.fwd(); val = Math.max(0, sigma_noise * ran.nextGaussian()); var.setReal(val); lc.get().add(var); } }
/** * 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); } } };
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; }
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 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; } }
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 }; }
public <T extends RealType<T>> boolean areCongruent( final IterableInterval<T> in, final RandomAccessible<T> out, final double epsilon) { Cursor<T> cin = in.localizingCursor(); RandomAccess<T> raOut = out.randomAccess(); while (cin.hasNext()) { cin.fwd(); raOut.setPosition(cin); if (Math.abs(cin.get().getRealDouble() - raOut.get() .getRealDouble()) > epsilon) return false; } return true; }
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; }
private void copy(ShuffledView<T> shuffled, Img<T> buffer) { Cursor<T> cursor = buffer.localizingCursor(); RandomAccess<T> ra = shuffled.randomAccess(); while (cursor.hasNext()) { T v = cursor.next(); ra.setPosition(cursor); v.set(ra.get()); } }