Java Code Examples for net.imglib2.RandomAccessible#randomAccess()

The following examples show how to use net.imglib2.RandomAccessible#randomAccess() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LiICQ.java    From Colocalisation_Analysis with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void execute(DataContainer<T> container)
		throws MissingPreconditionException {
	double mean1 = container.getMeanCh1();
	double mean2 = container.getMeanCh2();

	// get the 2 images for the calculation of Li's ICQ
	RandomAccessible<T> img1 = container.getSourceImage1();
	RandomAccessible<T> img2 = container.getSourceImage2();
	RandomAccessibleInterval<BitType> mask = container.getMask();

	TwinCursor<T> cursor = new TwinCursor<T>(img1.randomAccess(),
			img2.randomAccess(), Views.iterable(mask).localizingCursor());
	// calculate ICQ value
	icqValue = calculateLisICQ(cursor, mean1, mean2);
}
 
Example 2
Source File: AccessBoxRandomAccessible.java    From paintera with GNU General Public License v2.0 5 votes vote down vote up
public AccessBoxRandomAccessible(final RandomAccessible<T> source)
{
	this.source = source;
	min = new long[source.numDimensions()];
	max = new long[source.numDimensions()];
	sourceAccess = source.randomAccess();
}
 
Example 3
Source File: AccessBoxRandomAccessibleOnGet.java    From paintera with GNU General Public License v2.0 5 votes vote down vote up
public AccessBoxRandomAccessibleOnGet(final RandomAccessible<T> source)
{
	this.source = source;
	min = new long[source.numDimensions()];
	max = new long[source.numDimensions()];
	sourceAccess = source.randomAccess();
}
 
Example 4
Source File: AbstractOpTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 5 votes vote down vote up
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;
}
 
Example 5
Source File: Block.java    From SPIM_Registration with GNU General Public License v2.0 5 votes vote down vote up
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() );
	}
}
 
Example 6
Source File: Block.java    From SPIM_Registration with GNU General Public License v2.0 5 votes vote down vote up
private static final void copy3dArray( final int threadIdx, final int numThreads, final RandomAccessible< FloatType > source, final ArrayImg< FloatType, ? > block, final long[] offset )
{
	final int w = (int)block.dimension( 0 );
	final int h = (int)block.dimension( 1 );
	final int d = (int)block.dimension( 2 );

	final long offsetX = offset[ 0 ];
	final long offsetY = offset[ 1 ];
	final long offsetZ = offset[ 2 ];
	final float[] blockArray = ((FloatArray)block.update( null ) ).getCurrentStorageArray();

	// define where we will query the RandomAccess on the source
	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 );

	final long[] tmp = new long[]{ offsetX, offsetY, 0 };

	for ( int z = threadIdx; z < d; z += numThreads )
	{
		tmp[ 2 ] = z + offsetZ;
		randomAccess.setPosition( tmp );

		int i = z * h * w;

		for ( int y = 0; y < h; ++y )
		{
			randomAccess.setPosition( offsetX, 0 );

			for ( int x = 0; x < w; ++x )
			{
				blockArray[ i++ ] = randomAccess.get().get();
				randomAccess.fwd( 0 );
			}

			randomAccess.move( -w, 0 );
			randomAccess.fwd( 1 );
		}
	}
}
 
Example 7
Source File: KendallTauRankCorrelation.java    From Colocalisation_Analysis with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void execute(DataContainer<T> container)
	throws MissingPreconditionException
{
	RandomAccessible<T> img1 = container.getSourceImage1();
	RandomAccessible<T> img2 = container.getSourceImage2();
	RandomAccessibleInterval<BitType> mask = container.getMask();

	TwinCursor<T> cursor = new TwinCursor<T>(img1.randomAccess(),
			img2.randomAccess(), Views.iterable(mask).localizingCursor());

	tau = calculateMergeSort(cursor);
}
 
Example 8
Source File: MandersColocalization.java    From Colocalisation_Analysis with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void execute(DataContainer<T> container)
		throws MissingPreconditionException {
	// get the two images for the calculation of Manders' split coefficients
	RandomAccessible<T> img1 = container.getSourceImage1();
	RandomAccessible<T> img2 = container.getSourceImage2();
	RandomAccessibleInterval<BitType> mask = container.getMask();

	TwinCursor<T> cursor = new TwinCursor<T>(img1.randomAccess(),
			img2.randomAccess(), Views.iterable(mask).localizingCursor());

	// calculate Manders' split coefficients without threshold, M1 and M2.
	MandersResults results = calculateMandersCorrelation(cursor,
			img1.randomAccess().get().createVariable());

	// save the results
	mandersM1 = results.m1;
	mandersM2 = results.m2;

	// calculate the thresholded Manders' split coefficients, tM1 and tM2, if possible
	AutoThresholdRegression<T> autoThreshold = container.getAutoThreshold();
	if (autoThreshold != null ) {
		// thresholded Manders' split coefficients, tM1 and tM2
		cursor.reset();
		results = calculateMandersCorrelation(cursor, autoThreshold.getCh1MaxThreshold(),
				autoThreshold.getCh2MaxThreshold(), ThresholdMode.Above);

		// save the results
		mandersThresholdedM1 = results.m1;
		mandersThresholdedM2 = results.m2;
	}
}
 
Example 9
Source File: RestrictPainting.java    From paintera with GNU General Public License v2.0 4 votes vote down vote up
private static <T, U> void restrictTo(
		final RandomAccessible<Pair<T, U>> source,
		final RandomAccessible<UnsignedLongType> mask,
		final Localizable seed,
		final Shape shape,
		final Predicate<T> backgroundFilter,
		final Predicate<U> canvasFilter)
{
	final int n = source.numDimensions();

	final RandomAccessible<Pair<Pair<T, U>, UnsignedLongType>> paired = Views.pair(source, mask);

	final TLongList[] coordinates = new TLongList[n];
	for (int d = 0; d < n; ++d)
	{
		coordinates[d] = new TLongArrayList();
		coordinates[d].add(seed.getLongPosition(d));
	}

	final RandomAccessible<Neighborhood<Pair<Pair<T, U>, UnsignedLongType>>> neighborhood       = shape
			.neighborhoodsRandomAccessible(
			paired);
	final RandomAccess<Neighborhood<Pair<Pair<T, U>, UnsignedLongType>>>     neighborhoodAccess = neighborhood
			.randomAccess();

	final RandomAccess<UnsignedLongType> targetAccess = mask.randomAccess();
	targetAccess.setPosition(seed);
	targetAccess.get().set(1);

	final UnsignedLongType zero = new UnsignedLongType(0);
	final UnsignedLongType one  = new UnsignedLongType(1);
	final UnsignedLongType two  = new UnsignedLongType(2);

	for (int i = 0; i < coordinates[0].size(); ++i)
	{
		for (int d = 0; d < n; ++d)
		{
			neighborhoodAccess.setPosition(coordinates[d].get(i), d);
		}

		final Cursor<Pair<Pair<T, U>, UnsignedLongType>> neighborhoodCursor = neighborhoodAccess.get().cursor();

		while (neighborhoodCursor.hasNext())
		{
			final Pair<Pair<T, U>, UnsignedLongType> p                   = neighborhoodCursor.next();
			final UnsignedLongType                   m                   = p.getB();
			final Pair<T, U>                         backgroundAndCanvas = p.getA();
			if (m.valueEquals(zero) && canvasFilter.test(backgroundAndCanvas.getB()))
			{
				// If background is same as at seed, mark mask with two
				// (==not active), else with one (==active).
				m.set(backgroundFilter.test(backgroundAndCanvas.getA()) ? two : one);
				for (int d = 0; d < n; ++d)
				{
					coordinates[d].add(neighborhoodCursor.getLongPosition(d));
				}
			}

		}

		if (i > CLEANUP_THRESHOLD)
		{
			for (int d = 0; d < coordinates.length; ++d)
			{
				final TLongList c = coordinates[d];
				coordinates[d] = c.subList(i, c.size());
			}
			i = 0;
		}

	}
}
 
Example 10
Source File: FourNeighborhoodExtrema.java    From BigStitcher with GNU General Public License v2.0 4 votes vote down vote up
public static < T extends RealType< T > > ArrayList< Pair< Localizable, Double > > findMax( final RandomAccessible< T > img, final Interval region, final int maxN )
	{
		final Cursor< T > c = Views.iterable( Views.interval( img, region ) ).localizingCursor();
		final RandomAccess< T > r = img.randomAccess();
		final int n = img.numDimensions();

		final ArrayList< Pair< Localizable, Double > > list = new ArrayList< Pair< Localizable, Double > >();

		for ( int i = 0; i < maxN; ++i )
			list.add( new ValuePair< Localizable, Double >( null, -Double.MAX_VALUE ) );

A:		while ( c.hasNext() )
		{
			final double type = c.next().getRealDouble();
			r.setPosition( c );

			for ( int d = 0; d < n; ++d )
			{
				r.fwd( d );
				if ( type < r.get().getRealDouble() )
					continue A;
	
				r.bck( d );
				r.bck( d );
				
				if ( type < r.get().getRealDouble() )
					continue A;

				r.fwd( d );
			}

			
			for ( int i = maxN - 1; i >= 0; --i )
			{
				if ( type < list.get( i ).getB() )
				{
					if ( i == maxN - 1 )
					{
						continue A;
					}
					else
					{
						list.add( i + 1, new ValuePair< Localizable, Double >( new Point( c ), type ) );
						list.remove( maxN );
						continue A;
					}
				}
			}

			list.add( 0, new ValuePair< Localizable, Double >( new Point( c ), type ) );
			list.remove( maxN );
		}

		// remove all null elements
		for ( int i = maxN -1; i >= 0; --i )
			if ( list.get( i ).getA() == null )
				list.remove(  i );

		return list;
	}
 
Example 11
Source File: ConvolveNaiveC.java    From imagej-ops with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public void compute(final RandomAccessible<I> input,
	final RandomAccessibleInterval<O> output)
{
	// TODO: try a decomposition of the kernel into n 1-dim kernels

	final long[] min = new long[input.numDimensions()];
	final long[] max = new long[input.numDimensions()];

	for (int d = 0; d < kernel.numDimensions(); d++) {
		min[d] = -kernel.dimension(d);
		max[d] = kernel.dimension(d) + output.dimension(d);
	}

	final RandomAccess<I> inRA =
		input.randomAccess(new FinalInterval(min, max));

	final Cursor<K> kernelC = Views.iterable(kernel).localizingCursor();

	final Cursor<O> outC = Views.iterable(output).localizingCursor();

	final long[] pos = new long[input.numDimensions()];
	final long[] kernelRadius = new long[kernel.numDimensions()];
	for (int i = 0; i < kernelRadius.length; i++) {
		kernelRadius[i] = kernel.dimension(i) / 2;
	}

	float val;

	while (outC.hasNext()) {
		// image
		outC.fwd();
		outC.localize(pos);

		// kernel inlined version of the method convolve
		val = 0;
		inRA.setPosition(pos);

		kernelC.reset();
		while (kernelC.hasNext()) {
			kernelC.fwd();

			for (int i = 0; i < kernelRadius.length; i++) {
				// dimension can have zero extension e.g. vertical 1d kernel
				if (kernelRadius[i] > 0) {
					inRA.setPosition(pos[i] + kernelC.getLongPosition(i) -
						kernelRadius[i], i);
				}
			}

			val += inRA.get().getRealDouble() * kernelC.get().getRealDouble();
		}

		outC.get().setReal(val);
	}
}