Java Code Examples for net.imglib2.RandomAccess#move()

The following examples show how to use net.imglib2.RandomAccess#move() . 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: AccessedBlocksRandomAccessible.java    From paintera with GNU General Public License v2.0 5 votes vote down vote up
public static void main(final String[] args)
{
	final long[] dimensions = {10, 7};
	final int[]  blockSize  = {5, 3};

	final ArrayImg<LongType, LongArray> dummy = ArrayImgs.longs(dimensions);

	final AccessedBlocksRandomAccessible<LongType> tracker = new AccessedBlocksRandomAccessible<>(
			dummy,
			new CellGrid(dimensions, blockSize)
	);

	System.out.println(Arrays.toString(tracker.listBlocks()));
	final RandomAccess<LongType> ra = tracker.randomAccess();
	System.out.println(Arrays.toString(tracker.listBlocks()));
	ra.get();
	System.out.println(Arrays.toString(tracker.listBlocks()));
	ra.move(4, 0);
	ra.get();
	System.out.println(Arrays.toString(tracker.listBlocks()));
	ra.fwd(0);
	ra.get();
	System.out.println(Arrays.toString(tracker.listBlocks()));
	ra.move(6, 1);
	ra.get();
	System.out.println(Arrays.toString(tracker.listBlocks()));

}
 
Example 2
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 3
Source File: SimpleInterruptibleProjectorPreMultiply.java    From paintera with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Render the 2D target image by copying values from the source. Source can have more dimensions than the target.
 * Target coordinate <em>(x,y)</em> is copied from source coordinate <em>(x,y,0,...,0)</em>.
 *
 * @return true if rendering was completed (all target pixels written). false if rendering was interrupted.
 */
@Override
public boolean map()
{
	interrupted.set(false);

	final StopWatch stopWatch = new StopWatch();
	stopWatch.start();

	min[0] = target.min(0);
	min[1] = target.min(1);
	max[0] = target.max(0);
	max[1] = target.max(1);

	final long cr = -target.dimension(0);

	final int width  = (int) target.dimension(0);
	final int height = (int) target.dimension(1);

	final boolean         createExecutor = executorService == null;
	final ExecutorService ex             = createExecutor
	                                       ? Executors.newFixedThreadPool(numThreads)
	                                       : executorService;
	final int             numTasks;
	if (numThreads > 1)
	{
		numTasks = Math.min(numThreads * 10, height);
	}
	else
		numTasks = 1;
	final double                    taskHeight = (double) height / numTasks;
	final ArrayList<Callable<Void>> tasks      = new ArrayList<>(numTasks);
	for (int taskNum = 0; taskNum < numTasks; ++taskNum)
	{
		final long myMinY   = min[1] + (int) (taskNum * taskHeight);
		final long myHeight = (taskNum == numTasks - 1
		                       ? height
		                       : (int) ((taskNum + 1) * taskHeight)) - myMinY - min[1];

		final Callable<Void> r = () -> {
			if (interrupted.get())
				return null;

			System.out.println("WTF!");
			final RandomAccess<A>        sourceRandomAccess = source.randomAccess(
					SimpleInterruptibleProjectorPreMultiply.this);
			final RandomAccess<ARGBType> targetRandomAccess = target.randomAccess(target);

			sourceRandomAccess.setPosition(min);
			sourceRandomAccess.setPosition(myMinY, 1);
			targetRandomAccess.setPosition(min[0], 0);
			targetRandomAccess.setPosition(myMinY, 1);
			for (int y = 0; y < myHeight; ++y)
			{
				if (interrupted.get())
					return null;
				for (int x = 0; x < width; ++x)
				{
					final ARGBType argb = targetRandomAccess.get();
					converter.convert(sourceRandomAccess.get(), argb);
					final int nonpre = argb.get();
					argb.set(PixelUtils.NonPretoPre(nonpre));
					sourceRandomAccess.fwd(0);
					targetRandomAccess.fwd(0);
				}
				sourceRandomAccess.move(cr, 0);
				targetRandomAccess.move(cr, 0);
				sourceRandomAccess.fwd(1);
				targetRandomAccess.fwd(1);
			}
			return null;
		};
		tasks.add(r);
	}
	try
	{
		ex.invokeAll(tasks);
	} catch (final InterruptedException e)
	{
		Thread.currentThread().interrupt();
	}
	if (createExecutor)
		ex.shutdown();

	lastFrameRenderNanoTime = stopWatch.nanoTime();

	return !interrupted.get();
}
 
Example 4
Source File: AveragedRandomAccessible.java    From BigStitcher with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void move(int distance, int d)
{
	for (final RandomAccess< T > ra : RAs)
		ra.move( distance, d );
}
 
Example 5
Source File: AveragedRandomAccessible.java    From BigStitcher with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void move(long distance, int d)
{
	for (final RandomAccess< T > ra : RAs)
		ra.move( distance, d );
}
 
Example 6
Source File: AveragedRandomAccessible.java    From BigStitcher with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void move(Localizable localizable)
{
	for (final RandomAccess< T > ra : RAs)
		ra.move( localizable);
}
 
Example 7
Source File: AveragedRandomAccessible.java    From BigStitcher with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void move(int[] distance)
{
	for (final RandomAccess< T > ra : RAs)
		ra.move( distance);
}
 
Example 8
Source File: AveragedRandomAccessible.java    From BigStitcher with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void move(long[] distance)
{
	for (final RandomAccess< T > ra : RAs)
		ra.move( distance );
}
 
Example 9
Source File: HilditchThinningStrategy.java    From imagej-ops with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public boolean removePixel(final long[] position,
	final RandomAccessible<BitType> accessible, final int iteration)
{
	final RandomAccess<BitType> access = randomAccess(accessible);
	access.setPosition(position);

	final boolean[] vals = getNeighbourhood(access);

	// First condition is to ensure there are at least 2 and at most 6
	// neighbouring foreground pixels.
	int numForeground = 0;
	for (int i = 1; i < vals.length; ++i) {
		if (vals[i] == m_foreground) {
			++numForeground;
		}
	}

	if (!(2 <= numForeground && numForeground <= 6)) {
		return false;
	}

	// Second condition checks for transitions between foreground and
	// background. Exactly 1 such transition
	// is required.
	final int numPatterns = findPatternSwitches(vals);
	if (!(numPatterns == 1)) {
		return false;
	}

	// The third and fourth conditions require neighbourhoods of adjacent
	// pixels.

	// Access has to be reset to current image-position before moving it, since
	// the getNeighbourhood() method moves it to the top-left of the initial
	// pixel.
	access.setPosition(position);
	access.move(-1, 1);
	final int p2Patterns = findPatternSwitches((getNeighbourhood(access)));
	if (!((vals[1] == m_background || vals[3] == m_background ||
		vals[7] == m_background) || p2Patterns != 1))
	{
		return false;
	}

	access.setPosition(position);
	access.move(1, 0);
	final int p4Patterns = findPatternSwitches((getNeighbourhood(access)));

	if (!((vals[1] == m_background || vals[3] == m_background ||
		vals[5] == m_background) || p4Patterns != 1))
	{
		return false;
	}

	// If all conditions are met, we can safely remove the pixel.
	return true;
}
 
Example 10
Source File: DefaultContour.java    From imagej-ops with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public Polygon2D calculate(final RandomAccessibleInterval<B> input) {
	List<RealPoint> p = new ArrayList<>();

	final B var = Util.getTypeFromInterval(input).createVariable();

	final RandomAccess<B> raInput = Views.extendValue(input, var)
		.randomAccess();
	final Cursor<B> cInput = Views.flatIterable(input).cursor();
	final ClockwiseMooreNeighborhoodIterator<B> cNeigh =
		new ClockwiseMooreNeighborhoodIterator<>(raInput);

	double[] position = new double[2];
	double[] startPos = new double[2];

	// find first true pixel
	while (cInput.hasNext()) {
		// we are looking for a true pixel
		if (cInput.next().get()) {
			raInput.setPosition(cInput);
			raInput.localize(startPos);

			// add to polygon
			p.add(new RealPoint(startPos[0], startPos[1]));

			// backtrack:
			raInput.move(-1, 0);

			cNeigh.reset();

			while (cNeigh.hasNext()) {
				if (cNeigh.next().get()) {

					boolean specialBacktrack = false;

					raInput.localize(position);
					if (startPos[0] == position[0] && startPos[1] == position[1]) {
						// startPoint was found.
						if (useJacobs) {
							// Jacobs stopping criteria
							final int index = cNeigh.getIndex();
							if (index == 1 || index == 0) {
								// Jonathans refinement to
								// non-terminating jacobs criteria
								specialBacktrack = true;
							}
							else if (index == 2 || index == 3) {
								// if index is 2 or 3, we entered pixel
								// by moving {1, 0}, refore in same
								// way.
								break;
							} // else criteria not fulfilled, continue.
						}
						else {
							break;
						}
					}
					// add found point to polygon
					p.add(new RealPoint(position[0], position[1]));

					if (specialBacktrack) {
						cNeigh.backtrackSpecial();
					}
					else {
						cNeigh.backtrack();
					}
				}
			}

			break; // we only need to extract one contour.
		}
	}

	return new DefaultWritablePolygon2D(p);
}
 
Example 11
Source File: Abstract3x3NeighbourhoodThinning.java    From imagej-ops with BSD 2-Clause "Simplified" License 3 votes vote down vote up
/**
 * Returns all booleans in a 3x3 neighbourhood of the pixel the RandomAccess
 * points to. These booleans are stored in an Array in the following order:
 * <br>
 * 8 1 2 <br>
 * 7 0 3 <br>
 * 6 5 4 <br>
 *
 * @param access A RandomAccess pointing to a pixel of the image
 * @return A boolean Array holding the values of the neighbourhood in
 *         clockwise order.
 */
protected boolean[] getNeighbourhood(final RandomAccess<BitType> access) {
	final boolean[] vals = new boolean[9];

	vals[0] = access.get().get();

	access.move(-1, 1);
	vals[1] = access.get().get();

	access.move(1, 0);
	vals[2] = access.get().get();

	access.move(1, 1);
	vals[3] = access.get().get();

	access.move(1, 1);
	vals[4] = access.get().get();

	access.move(-1, 0);
	vals[5] = access.get().get();

	access.move(-1, 0);
	vals[6] = access.get().get();

	access.move(-1, 1);
	vals[7] = access.get().get();

	access.move(-1, 1);
	vals[8] = access.get().get();

	return vals;

}