Java Code Examples for net.imglib2.Cursor#reset()

The following examples show how to use net.imglib2.Cursor#reset() . 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: DefaultFillHoles.java    From imagej-ops with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void compute(final RandomAccessibleInterval<T> op,
	final RandomAccessibleInterval<T> r)
{
	final IterableInterval<T> iterOp = Views.flatIterable(op);
	final IterableInterval<T> iterR = Views.flatIterable(r);

	long[] dim = new long[r.numDimensions()];
	r.dimensions(dim);
	Cursor<T> rc = iterR.cursor();
	Cursor<T> opc = iterOp.localizingCursor();
	// Fill with non background marker
	while (rc.hasNext()) {
		rc.next().setOne();
	}

	rc.reset();
	boolean border;
	// Flood fill from every background border voxel
	while (rc.hasNext()) {
		rc.next();
		opc.next();
		if (rc.get().get() && !opc.get().get()) {
			border = false;
			for (int i = 0; i < r.numDimensions(); i++) {
				if (rc.getLongPosition(i) == 0 || rc.getLongPosition(i) == dim[i] -
					1)
				{
					border = true;
					break;
				}
			}
			if (border) {
				floodFillComp.compute(op, rc, r);
			}
		}
	}
}
 
Example 2
Source File: AbstractThin.java    From imagej-ops with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public void compute(final RandomAccessibleInterval<BitType> input,
	final RandomAccessibleInterval<BitType> output)
{
	// Create a new image as a buffer to store the thinning image in each
	// iteration.
	// This image and output are swapped each iteration since we need to work on
	// the image
	// without changing it.

	final Img<BitType> buffer = ops().create().img(input, new BitType());

	final IterableInterval<BitType> it1 = Views.iterable(buffer);
	final IterableInterval<BitType> it2 = Views.iterable(output);

	// Extend the buffer in order to be able to iterate care-free later.
	final RandomAccessible<BitType> ra1 = Views.extendBorder(buffer);
	final RandomAccessible<BitType> ra2 = Views.extendBorder(output);

	// Used only in first iteration.
	RandomAccessible<BitType> currRa = Views.extendBorder(input);

	// Create cursors.
	final Cursor<BitType> firstCursor = it1.localizingCursor();
	Cursor<BitType> currentCursor = Views.iterable(input).localizingCursor();
	final Cursor<BitType> secondCursor = it2.localizingCursor();

	// Create pointers to the current and next cursor and set them to Buffer and
	// output respectively.
	Cursor<BitType> nextCursor;
	nextCursor = secondCursor;

	// The main loop.
	boolean changes = true;
	int i = 0;
	// Until no more changes, do:
	final long[] coordinates = new long[currentCursor.numDimensions()];
	while (changes) {
		changes = false;
		// This For-Loop makes sure, that iterations only end on full cycles (as
		// defined by the strategies).
		for (int j = 0; j < m_strategy.getIterationsPerCycle(); ++j) {
			// For each pixel in the image.
			while (currentCursor.hasNext()) {
				// Move both cursors
				currentCursor.fwd();
				nextCursor.fwd();
				// Get the position of the current cursor.
				currentCursor.localize(coordinates);

				// Copy the value of the image currently operated upon.
				final boolean curr = currentCursor.get().get();
				nextCursor.get().set(curr);

				// Only foreground pixels may be thinned
				if (curr) {

					// Ask the strategy whether to flip the foreground pixel or not.
					final boolean flip = m_strategy.removePixel(coordinates, currRa, j);

					// If yes - change and keep track of the change.
					if (flip) {
						nextCursor.get().set(false);
						changes = true;
					}
				}
			}
			// One step of the cycle is finished, notify the strategy.
			m_strategy.afterCycle();

			// Reset the cursors to the beginning and assign pointers for the next
			// iteration.
			currentCursor.reset();
			nextCursor.reset();

			// Keep track of the most recent image. Needed for output.
			if (currRa == ra2) {
				currRa = ra1;
				currentCursor = firstCursor;
				nextCursor = secondCursor;
			}
			else {
				currRa = ra2;
				currentCursor = secondCursor;
				nextCursor = firstCursor;
			}

			// Keep track of iterations.
			++i;
		}
	}

	// Depending on the iteration count, the final image is either in ra1 or
	// ra2. Copy it to output.
	if (i % 2 == 0) {
		// Ra1 points to img1, ra2 points to output.
		copy(buffer, output);
	}
}
 
Example 3
Source File: CursorBasedChunk.java    From imagej-ops with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public static void setToStart(final Cursor<?> c, long startIndex) {
	c.reset();
	c.jumpFwd(startIndex + 1);
}
 
Example 4
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);
	}
}