Java Code Examples for net.imglib2.IterableInterval#localizingCursor()

The following examples show how to use net.imglib2.IterableInterval#localizingCursor() . 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: DefaultCoordinatesEquation.java    From imagej-ops with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void compute(final UnaryFunctionOp<long[], N> op,
	final IterableInterval<T> output)
{

	final Cursor<T> c = output.localizingCursor();
	final long[] pos = new long[output.numDimensions()];

	// loop through all pixels, set the current position and call the op
	while (c.hasNext()) {
		c.fwd();
		c.localize(pos);

		for (int i = 0; i < output.numDimensions(); i++) {

			op.calculate(pos);

			c.get().setReal(op.calculate(pos).floatValue());

		}

	}
}
 
Example 2
Source File: DefaultMoment11.java    From imagej-ops with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void compute(final IterableInterval<I> input, final O output) {

	double moment11 = 0;

	final Cursor<I> cur = input.localizingCursor();
	while (cur.hasNext()) {
		cur.fwd();
		double x = cur.getDoublePosition(0);
		double y = cur.getDoublePosition(1);
		double val = cur.get().getRealDouble();
		moment11 += x * y * val;
	}

	output.setReal(moment11);
}
 
Example 3
Source File: Maps.java    From imagej-ops with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public static <I1, I2, O> void map(final IterableInterval<I1> a,
	final RandomAccessibleInterval<I2> b, final IterableInterval<O> c,
	final BinaryComputerOp<I1, I2, O> op, final long startIndex,
	final long stepSize, final long numSteps)
{
	if (numSteps <= 0) return;
	final Cursor<I1> aCursor = a.localizingCursor();
	final RandomAccess<I2> bAccess = b.randomAccess();
	final Cursor<O> cCursor = c.cursor();

	for (long ctr = 0; ctr < numSteps; ctr++) {
		final long m = ctr == 0 ? startIndex + 1 : stepSize;
		aCursor.jumpFwd(m);
		cCursor.jumpFwd(m);
		bAccess.setPosition(aCursor);
		op.compute(aCursor.get(), bAccess.get(), cCursor.get());
	}
}
 
Example 4
Source File: DefaultCentralMoment03.java    From imagej-ops with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void compute(final IterableInterval<I> input, final O output) {
	final double moment00 = moment00Func.calculate(input).getRealDouble();
	final double moment01 = moment01Func.calculate(input).getRealDouble();
	
	final double centerY = moment01 / moment00;

	double centralmoment03 = 0;

	final Cursor<I> it = input.localizingCursor();
	while (it.hasNext()) {
		it.fwd();
		final double y = it.getDoublePosition(1) - centerY;
		final double val = it.get().getRealDouble();

		centralmoment03 += val * y * y * y;
	}

	output.setReal(centralmoment03);
}
 
Example 5
Source File: Maps.java    From imagej-ops with BSD 2-Clause "Simplified" License 6 votes vote down vote up
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 long startIndex,
	final long stepSize, final long numSteps)
{
	if (numSteps <= 0) return;
	final Cursor<I1> aCursor = a.localizingCursor();
	final Cursor<I2> bCursor = b.cursor();
	final RandomAccess<O> cAccess = c.randomAccess();

	for (long ctr = 0; ctr < numSteps; ctr++) {
		final long m = ctr == 0 ? startIndex + 1 : stepSize;
		aCursor.jumpFwd(m);
		bCursor.jumpFwd(m);
		cAccess.setPosition(aCursor);
		op.compute(aCursor.get(), bCursor.get(), cAccess.get());
	}
}
 
Example 6
Source File: DefaultCentralMoment12.java    From imagej-ops with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void compute(final IterableInterval<I> input, final O output) {
	final double moment00 = moment00Func.calculate(input).getRealDouble();
	final double moment01 = moment01Func.calculate(input).getRealDouble();
	final double moment10 = moment10Func.calculate(input).getRealDouble();
	final double centerX = moment10 / moment00;
	final double centerY = moment01 / moment00;

	double centralmoment12 = 0;

	final Cursor<I> it = input.localizingCursor();
	while (it.hasNext()) {
		it.fwd();
		final double x = it.getDoublePosition(0) - centerX;
		final double y = it.getDoublePosition(1) - centerY;
		final double val = it.get().getRealDouble();

		centralmoment12 += val * x * y * y;
	}

	output.setReal(centralmoment12);
}
 
Example 7
Source File: Maps.java    From imagej-ops with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static <A, I> void inplace(final IterableInterval<A> arg,
	final RandomAccessibleInterval<I> in, final BinaryInplace1Op<A, I, A> op,
	final long startIndex, final long stepSize, final long numSteps)
{
	if (numSteps <= 0) return;
	final Cursor<A> argCursor = arg.localizingCursor();
	final RandomAccess<I> inAccess = in.randomAccess();

	for (long ctr = 0; ctr < numSteps; ctr++) {
		argCursor.jumpFwd(ctr == 0 ? startIndex + 1 : stepSize);
		inAccess.setPosition(argCursor);
		op.mutate1(argCursor.get(), inAccess.get());
	}
}
 
Example 8
Source File: DefaultASCII.java    From imagej-ops with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static <T extends RealType<T>> String ascii(
	final IterableInterval<T> image, final T min, final T max)
{
	final long dim0 = image.dimension(0);
	final long dim1 = image.dimension(1);
	// TODO: Check bounds.
	final int w = (int) (dim0 + 1);
	final int h = (int) dim1;

	// span = max - min
	final T span = max.copy();
	span.sub(min);

	// allocate ASCII character array
	final char[] c = new char[w * h];
	for (int y = 1; y <= h; y++) {
		c[w * y - 1] = '\n'; // end of row
	}

	// loop over all available positions
	final Cursor<T> cursor = image.localizingCursor();
	final int[] pos = new int[image.numDimensions()];
	final T tmp = image.firstElement().copy();
	while (cursor.hasNext()) {
		cursor.fwd();
		cursor.localize(pos);
		final int index = w * pos[1] + pos[0];

		// normalized = (value - min) / (max - min)
		tmp.set(cursor.get());
		tmp.sub(min);
		final double normalized = tmp.getRealDouble() / span.getRealDouble();

		final int charLen = CHARS.length();
		final int charIndex = (int) (charLen * normalized);
		c[index] = CHARS.charAt(charIndex < charLen ? charIndex : charLen - 1);
	}

	return new String(c);
}
 
Example 9
Source File: Maps.java    From imagej-ops with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static <I1, I2, O> void map(final RandomAccessibleInterval<I1> a,
	final IterableInterval<I2> b, final IterableInterval<O> c,
	final BinaryComputerOp<I1, I2, O> op)
{
	final RandomAccess<I1> aAccess = a.randomAccess();
	final Cursor<I2> bCursor = b.localizingCursor();
	final Cursor<O> cCursor = c.cursor();
	while (bCursor.hasNext()) {
		bCursor.fwd();
		aAccess.setPosition(bCursor);
		op.compute(aAccess.get(), bCursor.get(), cCursor.next());
	}
}
 
Example 10
Source File: Maps.java    From imagej-ops with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static <I1, I2, O extends I1> void inplace(
	final IterableInterval<O> arg, final RandomAccessibleInterval<I2> in,
	final BinaryInplace1Op<I1, I2, O> op)
{
	final Cursor<O> argCursor = arg.localizingCursor();
	final RandomAccess<I2> inAccess = in.randomAccess();
	while (argCursor.hasNext()) {
		argCursor.fwd();
		inAccess.setPosition(argCursor);
		op.mutate1(argCursor.get(), inAccess.get());
	}
}
 
Example 11
Source File: Maps.java    From imagej-ops with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static <I, O> void map(final IterableInterval<I> a,
	final RandomAccessibleInterval<O> b, final UnaryComputerOp<I, O> op,
	final long startIndex, final long stepSize, final long numSteps)
{
	if (numSteps <= 0) return;
	final Cursor<I> aCursor = a.localizingCursor();
	final RandomAccess<O> bAccess = b.randomAccess();

	for (long ctr = 0; ctr < numSteps; ctr++) {
		aCursor.jumpFwd(ctr == 0 ? startIndex + 1 : stepSize);
		bAccess.setPosition(aCursor);
		op.compute(aCursor.get(), bAccess.get());
	}
}
 
Example 12
Source File: Maps.java    From imagej-ops with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static <I, O> void map(final IterableInterval<I> a,
	final RandomAccessibleInterval<O> b, final UnaryComputerOp<I, O> op)
{
	final Cursor<I> aCursor = a.localizingCursor();
	final RandomAccess<O> bAccess = b.randomAccess();
	while (aCursor.hasNext()) {
		aCursor.fwd();
		bAccess.setPosition(aCursor);
		op.compute(aCursor.get(), bAccess.get());
	}
}
 
Example 13
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 14
Source File: Maps.java    From imagej-ops with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static <A, I> void inplace(final RandomAccessibleInterval<A> arg,
	final IterableInterval<I> in, final BinaryInplace1Op<A, I, A> op,
	final long startIndex, final long stepSize, final long numSteps)
{
	if (numSteps <= 0) return;
	final RandomAccess<A> argAccess = arg.randomAccess();
	final Cursor<I> inCursor = in.localizingCursor();

	for (long ctr = 0; ctr < numSteps; ctr++) {
		inCursor.jumpFwd(ctr == 0 ? startIndex + 1 : stepSize);
		argAccess.setPosition(inCursor);
		op.mutate1(argAccess.get(), inCursor.get());
	}
}
 
Example 15
Source File: Maps.java    From imagej-ops with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static <I, O> void map(final RandomAccessibleInterval<I> a,
	final IterableInterval<O> b, final UnaryComputerOp<I, O> op,
	final long startIndex, final long stepSize, final long numSteps)
{
	if (numSteps <= 0) return;
	final RandomAccess<I> aAccess = a.randomAccess();
	final Cursor<O> bCursor = b.localizingCursor();

	for (long ctr = 0; ctr < numSteps; ctr++) {
		bCursor.jumpFwd(ctr == 0 ? startIndex + 1 : stepSize);
		aAccess.setPosition(bCursor);
		op.compute(aAccess.get(), bCursor.get());
	}
}
 
Example 16
Source File: DefaultMoment01.java    From imagej-ops with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void compute(final IterableInterval<I> input, final O output) {

	double moment01 = 0;

	final Cursor<I> cur = input.localizingCursor();
	while (cur.hasNext()) {
		cur.fwd();
		double y = cur.getDoublePosition(1);
		double val = cur.get().getRealDouble();
		moment01 += y * val;
	}

	output.setReal(moment01);
}
 
Example 17
Source File: DefaultMoment00.java    From imagej-ops with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@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);
}
 
Example 18
Source File: DefaultMoment10.java    From imagej-ops with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void compute(final IterableInterval<I> input, final O output) {

	double moment10 = 0;

	final Cursor<I> cur = input.localizingCursor();
	while (cur.hasNext()) {
		cur.fwd();
		double x = cur.getDoublePosition(0);
		double val = cur.get().getRealDouble();
		moment10 += x * val;
	}

	output.setReal(moment10);
}
 
Example 19
Source File: ZernikeComputer.java    From imagej-ops with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public ZernikeMoment calculate(final IterableInterval<T> ii) {

	final double width2 = (ii.dimension(0) - 1) / 2.0;
	final double height2 = (ii.dimension(1) - 1) / 2.0;

	final double centerX = width2 + ii.min(0);
	final double centerY = height2 + ii.min(1);

	final double radius = Math.sqrt(width2 * width2 + height2 * height2);

	// Compute pascal's triangle for binomal coefficients: d[x][y] equals (x
	// over y)
	final double[][] d = computePascalsTriangle(order);

	// initialize zernike moment
	final ZernikeMoment moment = initZernikeMoment(order, repetition, d);

	// get the cursor of the iterable interval
	final Cursor<? extends RealType<?>> cur = ii.localizingCursor();

	// run over iterable interval
	while (cur.hasNext()) {
		cur.fwd();

		// get 2d centered coordinates
		final int x = (int) (cur.getIntPosition(0) - ii.min(0));
		final int y = (int) (cur.getIntPosition(1) - ii.min(1));

		final double xm = (x - centerX) / radius;
		final double ym = (y - centerY) / radius;

		final double r = Math.sqrt(xm * xm + ym * ym);
		if (r <= 1 && cur.get().getRealDouble() != 0.0) {
			// calculate theta for this position
			final double theta = Math.atan2(xm, ym);
			moment.getZm().add(multiplyExp(1, moment.getP().evaluate(r), theta, moment.getM()));
		}
	}
	// normalization
	normalize(moment.getZm(), moment.getN(), getNumberOfPixelsInUnitDisk(radius));
	return moment;
}
 
Example 20
Source File: CooccurrenceMatrix3D.java    From imagej-ops with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public double[][] calculate(final IterableInterval<T> input) {

	double[][] matrix = new double[nrGreyLevels][nrGreyLevels];

	final Cursor<T> cursor = input.localizingCursor();
  final Pair<T, T> minMax = minmax.calculate(input);

	double localMin = minMax.getA().getRealDouble();
	double localMax = minMax.getB().getRealDouble();

	final int[][][] pixels = new int[(int) input.dimension(2)][(int) input
			.dimension(1)][(int) input.dimension(0)];

	final int minimumX = (int) input.min(0);
	final int minimumY = (int) input.min(1);
	final int minimumZ = (int) input.min(2);
	
	final double diff = localMax - localMin;
	while (cursor.hasNext()) {
		cursor.fwd();
		pixels[cursor.getIntPosition(2) - minimumZ][cursor
				.getIntPosition(1) - minimumY][cursor
				.getIntPosition(0) - minimumX] = (int) (((cursor
				.get().getRealDouble() - localMin) / diff) * (nrGreyLevels - 1));
	}

	
	final double orientationAtX = orientation.getValueAtDim(0) * distance;
	final double orientationAtY = orientation.getValueAtDim(1) * distance;
	final double orientationAtZ = orientation.getValueAtDim(2) * distance;

	int nrPairs = 0;
	for (int z = 0; z < pixels.length; z++) {
		for (int y = 0; y < pixels[z].length; y++) {
			for (int x = 0; x < pixels[z][y].length; x++) {

				// ignore pixels not in mask
				if (pixels[z][y][x] == Integer.MAX_VALUE) {
					continue;
				}

				// get second pixel
				final int sx = (int) (x + orientationAtX);
				final int sy = (int) (y + orientationAtY);
				final int sz = (int) (z + orientationAtZ);

				// second pixel in interval and mask
				if (sx >= 0 && sy >= 0 && sz >= 0 && sz < pixels.length
						&& sy < pixels[sz].length
						&& sx < pixels[sz][sy].length
						&& pixels[sz][sy][sx] != Integer.MAX_VALUE) {

					matrix[pixels[z][y][x]][pixels[sz][sy][sx]]++;
					nrPairs++;

				}
			}
		}
	}

	// normalize matrix
	if (nrPairs > 0) {
		double divisor = 1.0 / nrPairs;
		for (int row = 0; row < matrix.length; row++) {
			for (int col = 0; col < matrix[row].length; col++) {
				matrix[row][col] *= divisor;
			}
		}
	}

	return matrix;
}