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

The following examples show how to use net.imglib2.Cursor#fwd() . 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: CentroidII.java    From imagej-ops with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public RealLocalizable calculate(final IterableInterval<?> input) {
	int numDimensions = input.numDimensions();
	double[] output = new double[numDimensions];
	Cursor<?> c = input.localizingCursor();
	double[] pos = new double[numDimensions];
	while (c.hasNext()) {
		c.fwd();
		c.localize(pos);
		for (int i = 0; i < output.length; i++) {
			output[i] += pos[i];
		}
	}

	for (int i = 0; i < output.length; i++) {
		output[i] = output[i] / input.size();
	}

	return new RealPoint(output);
}
 
Example 2
Source File: DefaultCentralMoment02.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 centralmoment02 = 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();

		centralmoment02 += val * y * y;
	}

	output.setReal(centralmoment02);
}
 
Example 3
Source File: DefaultCentralMoment30.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 moment10 = moment10Func.calculate(input).getRealDouble();

	final double centerX = moment10 / moment00;

	double centralmoment30 = 0;

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

		centralmoment30 += val * x * x * x;
	}

	output.setReal(centralmoment30);
}
 
Example 4
Source File: CreateKernel2D.java    From imagej-ops with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public RandomAccessibleInterval<T> calculate(double[][] input) {
	final long[] dims = { input.length, input[0].length };
	final RandomAccessibleInterval<T> rai = createOp.calculate(new FinalInterval(
		dims));

	final Cursor<T> cursor = Views.iterable(rai).cursor();
	for (int j = 0; j < input.length; j++) {
		for (int k = 0; k < input[j].length; k++) {
			cursor.fwd();

			cursor.get().setReal(input[j][k]);
		}
	}

	return rai;
}
 
Example 5
Source File: BlendingRealRandomAccess.java    From SPIM_Registration with GNU General Public License v2.0 6 votes vote down vote up
public static void main( String[] args )
{
	new ImageJ();
	
	Img< FloatType > img = ArrayImgs.floats( 500, 500 );
	BlendingRealRandomAccess blend = new BlendingRealRandomAccess(
			img,
			new float[]{ 100, 0 },
			new float[]{ 12, 150 } );
	
	Cursor< FloatType > c = img.localizingCursor();
	
	while ( c.hasNext() )
	{
		c.fwd();
		blend.setPosition( c );
		c.get().setReal( blend.get().getRealFloat() );
	}
	
	ImageJFunctions.show( img );
}
 
Example 6
Source File: InvertTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private <T extends IntegerType<T>> void
	assertIntegerInvert(final Img<T> in, final Img<T> out)
{

	final Op op = ops.op(Ops.Image.Invert.class, out, in);
	assertSame(InvertIIInteger.class, op.getClass());
	op.run();
	
	Cursor<T> inCursor = in.localizingCursor();
	Cursor<T> outCursor = out.localizingCursor();
	
	while(inCursor.hasNext()) {
		inCursor.fwd();
		outCursor.fwd();
	}
	
	integerCompare(in, out, null, null);
}
 
Example 7
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 8
Source File: ColocImgLibGadgets.java    From Colocalisation_Analysis with GNU General Public License v3.0 6 votes vote down vote up
protected double calculatePearson(Cursor<T> cursor1, double mean1, Cursor<T> cursor2, double mean2) {
double pearsonDenominator = 0;
double ch1diffSquaredSum = 0;
double ch2diffSquaredSum = 0;
while (cursor1.hasNext() && cursor2.hasNext()) {
	cursor1.fwd();
	cursor2.fwd();
	T type1 = cursor1.get();
	double ch1diff = type1.getRealDouble() - mean1;
	T type2 = cursor2.get();
	double ch2diff = type2.getRealDouble() - mean2;
	pearsonDenominator += ch1diff*ch2diff;
	ch1diffSquaredSum += (ch1diff*ch1diff);
	ch2diffSquaredSum += (ch2diff*ch2diff);
}
double pearsonNumerator = Math.sqrt(ch1diffSquaredSum * ch2diffSquaredSum);
return pearsonDenominator / pearsonNumerator;
 }
 
Example 9
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 10
Source File: LegacySlideBook6ImgLoader.java    From SPIM_Registration with GNU General Public License v2.0 5 votes vote down vote up
public static final < T extends RealType< T > > void readUnsignedInts( final byte[] b, final Cursor< T > cursor, final int width, final boolean isLittleEndian )
{
	while( cursor.hasNext() )
	{
		cursor.fwd();
		cursor.get().setReal( LegacyStackImgLoaderLOCI.getIntValue( b, ( cursor.getIntPosition( 0 ) + cursor.getIntPosition( 1 ) * width ) * 4, isLittleEndian ) );
	}
}
 
Example 11
Source File: DefaultInertiaTensor3D.java    From imagej-ops with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public RealMatrix calculate(final IterableRegion<B> input) {
	final BlockRealMatrix output = new BlockRealMatrix(3, 3);
	Cursor<Void> c = input.localizingCursor();
	double[] pos = new double[3];
	double[] computedCentroid = new double[3];
	centroid.calculate(input).localize(computedCentroid);
	final double mX = computedCentroid[0];
	final double mY = computedCentroid[1];
	final double mZ = computedCentroid[2];
	while (c.hasNext()) {
		c.fwd();
		c.localize(pos);
		output.setEntry(0, 0, output.getEntry(0, 0) + (pos[0] - mX) * (pos[0] - mX));
		output.setEntry(1, 1, output.getEntry(1, 1) + (pos[1] - mX) * (pos[1] - mY));
		output.setEntry(2, 2, output.getEntry(2, 2) + (pos[2] - mX) * (pos[2] - mZ));
		output.setEntry(0, 1, output.getEntry(0, 1) + (pos[0] - mY) * (pos[1] - mY));
		output.setEntry(1, 0, output.getEntry(0, 1));
		output.setEntry(0, 2, output.getEntry(0, 2) + (pos[0] - mY) * (pos[2] - mZ));
		output.setEntry(2, 0, output.getEntry(0, 2));
		output.setEntry(1, 2, output.getEntry(1, 2) + (pos[1] - mZ) * (pos[2] - mZ));
		output.setEntry(2, 1, output.getEntry(1, 2));
	}

	final double size = input.size();
	output.setEntry(0, 0, output.getEntry(0, 0) / size);
	output.setEntry(0, 1, output.getEntry(0, 1) / size);
	output.setEntry(0, 2, output.getEntry(0, 2) / size);
	output.setEntry(1, 0, output.getEntry(1, 0) / size);
	output.setEntry(1, 1, output.getEntry(1, 1) / size);
	output.setEntry(1, 2, output.getEntry(1, 2) / size);
	output.setEntry(2, 0, output.getEntry(2, 0) / size);
	output.setEntry(2, 1, output.getEntry(2, 1) / size);
	output.setEntry(2, 2, output.getEntry(2, 2) / size);

	return output;
}
 
Example 12
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 13
Source File: IntegralCursorTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testIntegralCursor() {
	Shape rectangleShape = new RectangleShape(1, false);
	IterableInterval<Neighborhood<ByteType>> ii = rectangleShape
		.neighborhoodsSafe(img);
	Cursor<Neighborhood<ByteType>> cursor = ii.cursor();

	// Manually select the neighborhood that is centered on the image
	cursor.fwd();
	cursor.fwd();
	cursor.fwd();
	cursor.fwd();
	cursor.fwd();

	IntegralCursor<ByteType> integralCursor = new IntegralCursor<>(
		(RectangleNeighborhood) cursor.get());

	assertEquals(integralCursor.next(), new ByteType((byte) 1));
	assertEquals(integralCursor.next(), new ByteType((byte) 2));
	assertEquals(integralCursor.next(), new ByteType((byte) 5));
	assertEquals(integralCursor.next(), new ByteType((byte) 4));
	assertFalse(integralCursor.hasNext());

	integralCursor.reset();

	assertEquals(integralCursor.next(), new ByteType((byte) 1));
	assertEquals(integralCursor.next(), new ByteType((byte) 2));
	assertEquals(integralCursor.next(), new ByteType((byte) 5));
	assertEquals(integralCursor.next(), new ByteType((byte) 4));
	assertFalse(integralCursor.hasNext());
}
 
Example 14
Source File: RichardsonLucyTVUpdate.java    From imagej-ops with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * performs update step of the Richardson Lucy with Total Variation Algorithm
 */
@Override
public void compute(I correction, I estimate) {

	if (variation == null) {
		Type<T> type = Util.getTypeFromInterval(correction);

		variation = ops().create().img(correction, type.createVariable());
	}

	divUnitGradFastThread(estimate);

	final Cursor<T> cursorCorrection = Views.iterable(correction).cursor();

	final Cursor<T> cursorVariation = Views.iterable(variation).cursor();

	final Cursor<T> cursorEstimate = Views.iterable(estimate).cursor();

	while (cursorEstimate.hasNext()) {
		cursorCorrection.fwd();
		cursorVariation.fwd();
		cursorEstimate.fwd();

		cursorEstimate.get().mul(cursorCorrection.get());
		cursorEstimate.get().mul(1f / (1f - regularizationFactor * cursorVariation
			.get().getRealFloat()));
	}

}
 
Example 15
Source File: MaskFactory.java    From Colocalisation_Analysis with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Create a new mask image with a defined size and preset content.
 * @throws MissingPreconditionException
 */
public static RandomAccessibleInterval<BitType> createMask(long[] dim, long[] roiOffset, long[] roiDim)
		throws MissingPreconditionException {
	if (dim.length != roiOffset.length || dim.length != roiDim.length) {
		throw new MissingPreconditionException("The dimensions of the mask as well as the ROIs and his offset must be the same.");
	}

	final RandomAccessibleInterval<BitType> mask = createMask(dim);
	final int dims = mask.numDimensions();
	final long[] pos = new long[dims];
	

	// create an array with the max corner of the ROI
	final long[] roiOffsetMax = new long[dims];
	for (int i=0; i<dims; ++i)
		roiOffsetMax[i] = roiOffset[i] + roiDim[i];
	// go through the mask and mask points as valid that are in the ROI
	Cursor<BitType> cursor = Views.iterable(mask).localizingCursor();
	while ( cursor.hasNext() ) {
		cursor.fwd();
		cursor.localize(pos);
		boolean valid = true;
		// test if the current position is contained in the ROI
		for(int i=0; i<dims; ++i)
			valid &= pos[i] >= roiOffset[i] && pos[i] < roiOffsetMax[i];
		cursor.get().set(valid);
	}

	return mask;
}
 
Example 16
Source File: DefaultDerivativeGauss.java    From imagej-ops with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Convolves the rows of the image
 *
 * @param input - The input image.
 * @param output - The output image.
 * @param mask - The mask needed for the convolution, determined beforehand.
 */
private <T extends RealType<T>> void convolve_n(
	final RandomAccessibleInterval<T> input,
	final RandomAccessibleInterval<DoubleType> output, final double[] mask,
	final int n)
{
	double sum;
	final Cursor<T> cursor = Views.iterable(input).localizingCursor();
	final OutOfBoundsMirrorFactory<T, RandomAccessibleInterval<T>> osmf =
		new OutOfBoundsMirrorFactory<>(Boundary.SINGLE);
	final RandomAccess<T> inputRA = osmf.create(input);
	final RandomAccess<DoubleType> outputRA = output.randomAccess();

	while (cursor.hasNext()) {
		cursor.fwd();
		inputRA.setPosition(cursor);
		outputRA.setPosition(cursor);
		sum = 0;
		// loop from the bottom of the image to the top
		final int halfWidth = mask.length / 2;
		for (int i = -halfWidth; i <= halfWidth; i++) {
			for (int dim = 0; dim < input.numDimensions(); dim++) {
				long position = cursor.getLongPosition(dim);
				if (dim == n) position += i;
				inputRA.setPosition(position, dim);
			}
			sum += inputRA.get().getRealDouble() * mask[i + halfWidth];
		}
		outputRA.get().setReal(sum);
	}
}
 
Example 17
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 RandomAccessibleInterval<O> c,
	final BinaryComputerOp<I1, I2, O> op)
{
	final RandomAccess<I1> aAccess = a.randomAccess();
	final Cursor<I2> bCursor = b.localizingCursor();
	final RandomAccess<O> cAccess = c.randomAccess();
	while (bCursor.hasNext()) {
		bCursor.fwd();
		aAccess.setPosition(bCursor);
		cAccess.setPosition(bCursor);
		op.compute(aAccess.get(), bCursor.get(), cAccess.get());
	}
}
 
Example 18
Source File: IterableCentralMoment00.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 19
Source File: TestImageAccessor.java    From Colocalisation_Analysis with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Generates a Perlin noise image. It is based on Ken Perlin's
 * reference implementation (ImprovedNoise class) and a small
 * bit of Kas Thomas' sample code (http://asserttrue.blogspot.com/).
 */
public static <T extends RealType<T> & NativeType<T>> RandomAccessibleInterval<T> producePerlinNoiseImage(T type, int width,
		int height, double z, double scale) {
	// create the new image
	ImgFactory<T> imgFactory = new ArrayImgFactory<T>();
	RandomAccessibleInterval<T> noiseImage = imgFactory.create( new int[] {width, height}, type);
	Cursor<T> noiseCursor = Views.iterable(noiseImage).localizingCursor();

	double xOffset = Math.random() * (width*width);
	double yOffset = Math.random() * (height*height);

	while (noiseCursor.hasNext()) {
		noiseCursor.fwd();
		double x = (noiseCursor.getDoublePosition(0) + xOffset) * scale;
		double y = (noiseCursor.getDoublePosition(1) + yOffset) * scale;

		float t = (float)ImprovedNoise.noise( x, y, z);

		// ImprovedNoise.noise returns a float in the range [-1..1],
		// whereas we want a float in the range [0..1], so:
                       t = (1 + t) * 0.5f;

                       noiseCursor.get().setReal(t);
	}

	//return gaussianSmooth(noiseImage, imgFactory, smoothingSigma);
	return noiseImage;
}
 
Example 20
Source File: WatershedSeededTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void assertResults(final RandomAccessibleInterval<FloatType> in, final ImgLabeling<Integer, IntType> out,
		final ImgLabeling<Integer, IntType> seeds, final RandomAccessibleInterval<BitType> mask,
		final boolean withWatersheds, final boolean smallMask) {

	final Cursor<LabelingType<Integer>> curOut = out.cursor();
	final RandomAccess<BitType> raMask = mask.randomAccess();
	while (curOut.hasNext()) {
		curOut.fwd();
		raMask.setPosition(curOut);
		if (raMask.get().get()) {
			assertEquals(1, curOut.get().size());
		} else {
			assertEquals(true, curOut.get().isEmpty());
		}
	}
	// Sample the output image based on the mask
	IterableRegion<BitType> regions = Regions.iterable(mask);

	// count labels
	Set<Integer> labelSet = new HashSet<>();
	for (LabelingType<Integer> pixel : Regions.sample(regions, out)) {
		labelSet.addAll(pixel);
	}

	// assert equals
	assertEquals(in.numDimensions(), out.numDimensions());
	assertEquals(in.dimension(0), out.dimension(0));
	assertEquals(in.dimension(1), out.dimension(1));
	assertEquals(3 + (withWatersheds ? 1 : 0), labelSet.size() + (smallMask ? 1 : 0));
}