Java Code Examples for net.imglib2.img.Img#cursor()

The following examples show how to use net.imglib2.img.Img#cursor() . 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: GenerateSpimData.java    From BigStitcher with GNU General Public License v2.0 6 votes vote down vote up
public static Img< FloatType > copyChannel( final ImagePlus imp, final int channel )
{
	final int w, h, d;

	Img< FloatType > img = ArrayImgs.floats( w = imp.getWidth(), h = imp.getHeight(), d = imp.getNSlices() );
	
	final Cursor< FloatType > c = img.cursor();

	for ( int z = 0; z < d; ++z )
	{
		final int[] pixels = (int[])imp.getStack().getProcessor( z + 1 ).getPixels();
		
		for ( int i = 0; i < w*h; ++i )
		{
			if ( channel == 0 )
				c.next().set( ( pixels[ i ] & 0xff0000) >> 16 );
			else if ( channel == 1 )
				c.next().set( ( pixels[ i ] & 0xff00 ) >> 8 );
			else
				c.next().set( pixels[ i ] & 0xff );
		}
	}
	
	return img;
}
 
Example 2
Source File: PermuteViewTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
public void testIntervalPermuteDimensionCoordinates() {
	Img<DoubleType> img = ArrayImgs.doubles(2, 2);
	Cursor<DoubleType> c = img.cursor();
	MersenneTwisterFast r = new MersenneTwisterFast(SEED);
	while (c.hasNext()) {
		c.next().set(r.nextDouble());
	}
	IntervalView<DoubleType> expected = Views.permuteCoordinates(img, new int[]{0, 1}, 1);
	Cursor<DoubleType> e = expected.cursor();
	RandomAccessibleInterval<DoubleType> actual = ops.transform().permuteCoordinatesView(img, new int[]{0, 1}, 1);
	RandomAccess<DoubleType> actualRA = actual.randomAccess();
	
	while (e.hasNext()) {
		e.next();
		actualRA.setPosition(e);
		assertEquals(e.get().get(), actualRA.get().get(), 1e-10);
	}
	
	assertTrue(Intervals.equals(expected, actual));
	
}
 
Example 3
Source File: PermuteViewTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
public void testIntervalPermuteCoordinates() {
	Img<DoubleType> img = ArrayImgs.doubles(2, 2);
	Cursor<DoubleType> c = img.cursor();
	MersenneTwisterFast r = new MersenneTwisterFast(SEED);
	while (c.hasNext()) {
		c.next().set(r.nextDouble());
	}
	IntervalView<DoubleType> expected = Views.permuteCoordinates(img, new int[]{0, 1});
	Cursor<DoubleType> e = expected.cursor();
	RandomAccessibleInterval<DoubleType> actual = ops.transform().permuteCoordinatesView(img, new int[]{0, 1});
	RandomAccess<DoubleType> actualRA = actual.randomAccess();
	
	while (e.hasNext()) {
		e.next();
		actualRA.setPosition(e);
		assertEquals(e.get().get(), actualRA.get().get(), 1e-10);
	}
	
	assertTrue(Intervals.equals(expected, actual));
	
}
 
Example 4
Source File: FFTTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * a utility to assert that two images are equal
 * 
 * @param img1
 * @param img2
 * @param delta
 */
protected void assertImagesEqual(final Img<FloatType> img1,
	final Img<FloatType> img2, final float delta)
{
	final Cursor<FloatType> c1 = img1.cursor();
	final Cursor<FloatType> c2 = img2.cursor();

	while (c1.hasNext()) {
		c1.fwd();
		c2.fwd();

		// assert that the inverse = the input within the error delta
		assertEquals(c1.get().getRealFloat(), c2.get().getRealFloat(), delta);
	}

}
 
Example 5
Source File: CopyImgTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
public void copyImgWithOutputTest() {
	final Img<DoubleType> inputCopy = input.factory().create(input, input
		.firstElement());
	copy(input, inputCopy);

	final Img<DoubleType> output = input.factory().create(input, input
		.firstElement());

	ops.run(CopyImg.class, output, input);

	final Cursor<DoubleType> inc = input.cursor();
	final Cursor<DoubleType> inCopyc = inputCopy.cursor();
	final Cursor<DoubleType> outc = output.cursor();

	while (inc.hasNext()) {
		assertEquals(inc.next().get(), outc.next().get(), 0.0);
		assertEquals(inc.get().get(), inCopyc.next().get(), 0.0);
	}
}
 
Example 6
Source File: NormalizeTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
public void testNormalize() {

	Img<ByteType> in = generateByteArrayTestImg(true, 5, 5);
	Img<ByteType> out = in.factory().create(in, new ByteType());

	ops.run(NormalizeIIComputer.class, out, in);

	final Pair<ByteType, ByteType> minMax2 = ops.stats().minMax(out);

	assertEquals(minMax2.getA().get(), Byte.MIN_VALUE);
	assertEquals(minMax2.getB().get(), Byte.MAX_VALUE);

	final IterableInterval<ByteType> lazyOut = ops.image().normalize(in);
	final IterableInterval<ByteType> notLazyOut = ops.image().normalize(in,
		null, null, null, null, false);

	final Cursor<ByteType> outCursor = out.cursor();
	final Cursor<ByteType> lazyCursor = lazyOut.cursor();
	final Cursor<ByteType> notLazyCursor = notLazyOut.cursor();
	while (outCursor.hasNext()) {
		assertEquals(outCursor.next().get(), lazyCursor.next().get());
		assertEquals(outCursor.get().get(), notLazyCursor.next().get());
	}
}
 
Example 7
Source File: UnshearViewTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
public void defaultUnshearTest() {
	Img<DoubleType> img = new ArrayImgFactory<DoubleType>().create(new int[] { 2, 2 }, new DoubleType());
	Cursor<DoubleType> imgC = img.cursor();
	while (imgC.hasNext()) {
		imgC.next().set(1);
	}

	TransformView<DoubleType> il2 = Views.unshear(Views.shear(Views.extendZero(img), 0, 1), 0, 1);
	TransformView<DoubleType> opr = ops.transform().unshearView(Views.shear(Views.extendZero(img), 0, 1), 0, 1);
	Cursor<DoubleType> il2C = Views.interval(il2, new FinalInterval(new long[] { 0, 0 }, new long[] { 3, 3 }))
			.cursor();
	RandomAccess<DoubleType> oprRA = Views
			.interval(opr, new FinalInterval(new long[] { 0, 0 }, new long[] { 3, 3 })).randomAccess();

	while (il2C.hasNext()) {
		il2C.next();
		oprRA.setPosition(il2C);
		assertEquals(il2C.get().get(), oprRA.get().get(), 1e-10);
	}
}
 
Example 8
Source File: DefaultBilateralTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
public void testBigImage() {
	final byte[] data = { 7, 8, 9, 1, 2, 3, 7, 9, 8, 1, 3, 2, 8, 7, 9, 2, 1, 3, 8, 9, 7, 2, 3, 1, 9, 7, 8, 3, 1, 2,
			9, 8, 7, 3, 2, 1 };
	final Img<ByteType> in = ArrayImgs.bytes(data, 6, 6);
	final Img<ByteType> out = generateByteArrayTestImg(false, 6, 6);

	ops.run(DefaultBilateral.class, out, in, 15, 5, 2);

	final byte[] expected = { 8, 7, 6, 4, 3, 2, 8, 7, 6, 4, 3, 2, 8, 7, 6, 4, 3, 2, 8, 7, 6, 4, 3, 2, 8, 7, 6, 4, 3,
			2, 8, 7, 6, 4, 3, 2 };

	Cursor<ByteType> cout = out.cursor();
	for (int i = 0; i < expected.length; i++) {
		assertEquals(cout.next().get(), expected[i]);
	}
}
 
Example 9
Source File: VectorAccelerator.java    From imagej-ops with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public double DotProduct(final Img<T> image1, final Img<T> image2) {
	final Cursor<T> cursorImage1 = image1.cursor();
	final Cursor<T> cursorImage2 = image2.cursor();

	double dotProduct = 0.0d;

	while (cursorImage1.hasNext()) {
		cursorImage1.fwd();
		cursorImage2.fwd();

		float val1 = cursorImage1.get().getRealFloat();
		float val2 = cursorImage2.get().getRealFloat();

		dotProduct += val1 * val2;
	}

	return dotProduct;
}
 
Example 10
Source File: DoGTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
public void dogRAITest() {

	final double[] sigmas1 = new double[] { 1, 1 };
	final double[] sigmas2 = new double[] { 2, 2 };
	final long[] dims = new long[] { 10, 10 };

	final Img<ByteType> in = generateByteArrayTestImg(true, dims);
	final Img<ByteType> out1 = generateByteArrayTestImg(false, dims);
	final Img<ByteType> out2 = generateByteArrayTestImg(false, dims);

	ops.run(DoGVaryingSigmas.class, out1, in, sigmas1, sigmas2);

	// test against native imglib2 implementation
	DifferenceOfGaussian.DoG(sigmas1, sigmas2, Views.extendMirrorSingle(in),
		out2, Executors.newFixedThreadPool(10));

	final Cursor<ByteType> out1Cursor = out1.cursor();
	final Cursor<ByteType> out2Cursor = out2.cursor();

	while (out1Cursor.hasNext()) {
		org.junit.Assert.assertEquals(out1Cursor.next().getRealDouble(),
			out2Cursor.next().getRealDouble(), 0);
	}
}
 
Example 11
Source File: ExtractPSF.java    From SPIM_Registration with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Compute average PSF in local image coordinates, all images are supposed to have the same dimensions
 * 
 * @return
 */
public Img< T > computeAveragePSF()
{
	Img< T > someImg = originalPSFs.values().iterator().next();
	final Img< T > avgOriginalPSF = someImg.factory().create( someImg, someImg.firstElement() );

	try
	{
		for ( final ViewId viewId : getViewIdsForPSFs() )
		{
			final Img< T > psf = originalPSFs.get( viewId );

			final Cursor< T > cursor = psf.cursor();

			for ( final T t : avgOriginalPSF )
				t.add( cursor.next() );
		}
	}
	catch (Exception e) 
	{
		IOFunctions.println( "Input PSFs were most likely of different size ... not computing average image in original scale." );
		e.printStackTrace();
	}
	
	return avgOriginalPSF;
}
 
Example 12
Source File: ContentBased.java    From SPIM_Registration with GNU General Public License v2.0 5 votes vote down vote up
protected Img< FloatType > approximateEntropy(
		final RandomAccessibleInterval< FloatType > input,
		final ImgFactory< ComplexFloatType > imgFactory,
		final double[] sigma1,
		final double[] sigma2 )

{
	// the result
	ImgFactory<FloatType> f;
	try { f = imgFactory.imgFactory( new FloatType() ); } catch (IncompatibleTypeException e) { f = new ArrayImgFactory< FloatType >(); }
	
	final Img< FloatType > conv = f.create( input, new FloatType() );
	
	// compute I*sigma1
	FFTConvolution< FloatType > fftConv = new FFTConvolution<FloatType>( input, createGaussianKernel( sigma1 ), conv, imgFactory );
	fftConv.convolve();
	
	// compute ( I - I*sigma1 )^2
	final Cursor< FloatType > c = conv.cursor();
	final RandomAccess< FloatType > r = input.randomAccess();
	
	while ( c.hasNext() )
	{
		c.fwd();
		r.setPosition( c );
		
		final float diff = c.get().get() - r.get().get();
		c.get().set( diff * diff );
	}
	
	// compute ( ( I - I*sigma1 )^2 ) * sigma2
	fftConv = new FFTConvolution<FloatType>( conv, createGaussianKernel( sigma2 ), imgFactory );
	fftConv.convolve();

	// normalize to [0...1]
	FusionHelper.normalizeImage( conv );

	return conv;
}
 
Example 13
Source File: ConvertMapTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Test
public void testLossy() {

	final float[] inArray =
		{ 12.7f, -13089.3208f, 78.023f, 0.04f, 12.01f, -1208.90f, 109432.109f,
			1204.88f, 87.6f };
	final Img<FloatType> in = generateFloatImg(inArray);

	final byte[] outArray = { 4, 123, 18, 64, 90, 120, 12, 17, 73 };
	final Img<UnsignedByteType> out = generateUnsignedByteImg(outArray);

	ops.run(Ops.Map.class, out, in, new ComplexToUint8<FloatType>());

	final Cursor<FloatType> inC = in.cursor();
	final Cursor<UnsignedByteType> outC = out.cursor();

	while (inC.hasNext()) {

		final double inV = inC.next().getRealDouble();
		final double outV = outC.next().getRealDouble();

		// values won't be equal because the conversion is lossy
		assertNotEquals(inV, outV, 0);

		// uint8 masks float values to be 8 bits
		assertEquals(Types.uint8(inV), outV, 0);

	}
}
 
Example 14
Source File: DilationTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Test
public void testSingleDilate() {
	final Shape shape = new DiamondShape(1);
	@SuppressWarnings("unchecked")
	final Img<ByteType> out1 = (Img<ByteType>) ops.run(DefaultDilate.class,
		Img.class, in, shape, false);
	final Img<ByteType> out2 = Dilation.dilate(in, shape, 1);
	final Cursor<ByteType> c1 = out1.cursor();
	final Cursor<ByteType> c2 = out2.cursor();
	while (c1.hasNext())
		assertEquals(c1.next().get(), c2.next().get());
}
 
Example 15
Source File: ColocImgLibGadgets.java    From Colocalisation_Analysis with GNU General Public License v3.0 5 votes vote down vote up
protected double getImageMean(Img<T> img) {
 double sum = 0;
 Cursor<T> cursor = img.cursor();
 while (cursor.hasNext()) {
  cursor.fwd();
  T type = cursor.get();
  sum += type.getRealDouble();
 }
 return sum / ImageStatistics.getNumPixels(img);
}
 
Example 16
Source File: CopyImgTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void copy(final Img<DoubleType> from, final Img<DoubleType> to) {
	final Cursor<DoubleType> fromc = from.cursor();
	final Cursor<DoubleType> toc = to.cursor();
	while (fromc.hasNext()) {
		toc.next().set(fromc.next().get());
	}
}
 
Example 17
Source File: ConvertMapTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Test
public void testLossless() {

	final byte[] inArray = { 12, 122, 9, -6, 56, 34, 108, 1, 73 };
	final Img<UnsignedByteType> in = generateUnsignedByteImg(inArray);

	final float[] outArray =
		{ 134.7f, -13089.3208f, 209.3f, 0.6f, 84.0f, -543.1f, 0f, 34.908f,
			592087.0957f };
	final Img<FloatType> out = generateFloatImg(outArray);

	final Cursor<UnsignedByteType> inC1 = in.cursor();
	final Cursor<FloatType> outC1 = out.cursor();

	while (inC1.hasNext()) {
		assertNotEquals(inC1.next().getRealDouble(),
			outC1.next().getRealDouble(), 0d);
	}

	ops.run(Ops.Map.class, out, in, new ComplexToFloat32<UnsignedByteType>());

	final Cursor<UnsignedByteType> inC2 = in.cursor();
	final Cursor<FloatType> outC2 = out.cursor();

	while (inC2.hasNext()) {
		assertEquals(inC2.next().getRealDouble(), outC2.next().getRealDouble(), 0);
	}
}
 
Example 18
Source File: MorphologyOpsTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Before
public void loadImages() {
	if (initialized) {
		return;
	}

	// create two bittypes images
	Img<FloatType> inputWithoutHoles = openFloatImg("img_without_holes.png");
	Img<FloatType> inputWithHoles = openFloatImg("img_with_holes.png");
	Img<FloatType> invertedInputWithFilledHoles = openFloatImg("inverted_img_with_filled_holes.png");

	Cursor<FloatType> inputWithoutHolesCursor = inputWithoutHoles.cursor();
	Cursor<FloatType> inputWithHolesCursor = inputWithHoles.cursor();
	Cursor<FloatType> invertedInputWithFilledHolesCursor = invertedInputWithFilledHoles.cursor();

	imgWithoutHoles = ops.create().img(inputWithoutHoles, new BitType());
	imgWithHoles = ops.create().img(inputWithHoles, new BitType());
	invertedImgWithFilledHoles = ops.create().img(invertedInputWithFilledHoles, new BitType());

	Cursor<BitType> imgWithoutHolesCursor = imgWithoutHoles.cursor();
	Cursor<BitType> imgWithHolesCursor = imgWithHoles.cursor();
	Cursor<BitType> invertedImgWithFilledHolesCursor = invertedImgWithFilledHoles.cursor();

	while (inputWithoutHolesCursor.hasNext()) {
		imgWithoutHolesCursor.next().set((inputWithoutHolesCursor.next().get() > 0) ? true : false);
	}

	while (inputWithHolesCursor.hasNext()) {
		imgWithHolesCursor.next().set((inputWithHolesCursor.next().get() > 0) ? true : false);
	}

	while (invertedInputWithFilledHolesCursor.hasNext()) {
		invertedImgWithFilledHolesCursor.next()
				.set((invertedInputWithFilledHolesCursor.next().get() > 0) ? true : false);
	}

	initialized = true;
}
 
Example 19
Source File: MapTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private static void assertImgSubEquals(final Img<ByteType> in1,
	final Img<ByteType> in2, final Img<ByteType> out)
{
	final Cursor<ByteType> in1Cursor = in1.cursor();
	final Cursor<ByteType> in2Cursor = in2.cursor();
	final Cursor<ByteType> outCursor = out.cursor();

	while (in1Cursor.hasNext()) {
		assertEquals((byte) (in1Cursor.next().get() - in2Cursor.next().get()),
			outCursor.next().get());
	}
}
 
Example 20
Source File: SobelFilterTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Test
public void test() {

	Img<FloatType> img = generateFloatArrayTestImg(false, new long[] { 20, 20 });

	Cursor<FloatType> cursorImg = img.cursor();
	int counterX = 0;
	int counterY = 0;
	while (cursorImg.hasNext()) {
		if (counterX > 8 && counterX < 12 || counterY > 8 && counterY < 12) {
			cursorImg.next().setOne();
		} else {
			cursorImg.next().setZero();
		}
		counterX++;
		if (counterX % 20 == 0) {
			counterY++;
		}
		if (counterX == 20) {
			counterX = 0;
		}
		if (counterY == 20) {
			counterY = 0;
		}
	}
	RandomAccessibleInterval<FloatType> out = ops.filter().sobel(img);

	RandomAccess<FloatType> outRA = out.randomAccess();
	outRA.setPosition(new int[] { 0, 8 });
	FloatType type = Util.getTypeFromInterval(out).createVariable();
	type.set(4.0f);
	assertEquals(type, outRA.get());
	type.setZero();
	outRA.setPosition(new int[] { 0, 10 });
	assertEquals(type, outRA.get());
	outRA.setPosition(new int[] { 10, 8 });
	assertEquals(type, outRA.get());
	outRA.setPosition(new int[] { 10, 10 });
	assertEquals(type, outRA.get());

	outRA.setPosition(new int[] { 10, 12 });
	type.set(0.0f);
	assertEquals(type, outRA.get());
	outRA.setPosition(new int[] { 12, 10 });
	assertEquals(type, outRA.get());
}