Search in sources :

Example 36 with Cursor

use of net.imglib2.Cursor in project imagej-ops by imagej.

the class SubsampleViewTest method defaultSubsampleStepsTest.

@Test
public void defaultSubsampleStepsTest() {
    Img<DoubleType> img = new ArrayImgFactory<DoubleType>().create(new int[] { 10, 10 }, new DoubleType());
    Random r = new Random();
    for (DoubleType d : img) {
        d.set(r.nextDouble());
    }
    SubsampleView<DoubleType> il2 = Views.subsample((RandomAccessible<DoubleType>) img, 2, 1);
    SubsampleView<DoubleType> opr = ops.transform().subsampleView(img, 2, 1);
    Cursor<DoubleType> il2C = Views.interval(il2, new long[] { 0, 0 }, new long[] { 4, 9 }).localizingCursor();
    RandomAccess<DoubleType> oprRA = opr.randomAccess();
    while (il2C.hasNext()) {
        il2C.next();
        oprRA.setPosition(il2C);
        assertEquals(il2C.get().get(), oprRA.get().get(), 1e-10);
    }
}
Also used : Random(java.util.Random) DoubleType(net.imglib2.type.numeric.real.DoubleType) AbstractOpTest(net.imagej.ops.AbstractOpTest) Test(org.junit.Test)

Example 37 with Cursor

use of net.imglib2.Cursor in project imagej-ops by imagej.

the class UnshearViewTest method defaultUnshearTest.

@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);
    }
}
Also used : DoubleType(net.imglib2.type.numeric.real.DoubleType) FinalInterval(net.imglib2.FinalInterval) AbstractOpTest(net.imagej.ops.AbstractOpTest) Test(org.junit.Test)

Example 38 with Cursor

use of net.imglib2.Cursor in project imagej-ops by imagej.

the class UnshearViewTest method UnshearIntervalTest.

@Test
public void UnshearIntervalTest() {
    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);
    }
    Cursor<DoubleType> il2 = Views.unshear(Views.shear(Views.extendZero(img), 0, 1), new FinalInterval(new long[] { 0, 0 }, new long[] { 3, 3 }), 0, 1).cursor();
    RandomAccess<DoubleType> opr = ops.transform().unshearView(Views.shear(Views.extendZero(img), 0, 1), new FinalInterval(new long[] { 0, 0 }, new long[] { 3, 3 }), 0, 1).randomAccess();
    while (il2.hasNext()) {
        il2.next();
        opr.setPosition(il2);
        assertEquals(il2.get().get(), opr.get().get(), 1e-10);
    }
}
Also used : DoubleType(net.imglib2.type.numeric.real.DoubleType) FinalInterval(net.imglib2.FinalInterval) AbstractOpTest(net.imagej.ops.AbstractOpTest) Test(org.junit.Test)

Example 39 with Cursor

use of net.imglib2.Cursor in project imagej-ops by imagej.

the class PartialDerivativeFilterTest method testAllDerivatives.

@Test
public void testAllDerivatives() {
    Img<FloatType> img = generateFloatArrayTestImg(false, new long[] { 20, 20, 3 });
    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;
        }
    }
    CompositeIntervalView<FloatType, RealComposite<FloatType>> out = ops.filter().allPartialDerivatives(img);
    CompositeView<FloatType, RealComposite<FloatType>>.CompositeRandomAccess outRA = out.randomAccess();
    FloatType type = Util.getTypeFromInterval(img).createVariable();
    // position 9,8 in all dimensions
    outRA.setPosition(new int[] { 9, 8, 0 });
    RealComposite<FloatType> outvalue = outRA.get();
    Float[] correctValues = new Float[] { 12.0f, 4.0f, 0.0f };
    int i = 0;
    for (FloatType value : outvalue) {
        type.set(correctValues[i]);
        assertEquals(type, value);
        i++;
    }
    outRA.setPosition(new int[] { 9, 8, 1 });
    outvalue = outRA.get();
    correctValues = new Float[] { 12.0f, 4.0f, 0.0f };
    i = 0;
    for (FloatType value : outvalue) {
        type.set(correctValues[i]);
        assertEquals(type, value);
        i++;
    }
    outRA.setPosition(new int[] { 9, 8, 2 });
    outvalue = outRA.get();
    correctValues = new Float[] { 12.0f, 4.0f, 0.0f };
    i = 0;
    for (FloatType value : outvalue) {
        type.set(correctValues[i]);
        assertEquals(type, value);
        i++;
    }
    // position 9,9 in all dimensions
    outRA.setPosition(new int[] { 9, 9, 0 });
    outvalue = outRA.get();
    correctValues = new Float[] { 4.0f, 4.0f, 0.0f };
    i = 0;
    for (FloatType value : outvalue) {
        type.set(correctValues[i]);
        assertEquals(type, value);
        i++;
    }
    outRA.setPosition(new int[] { 9, 9, 1 });
    outvalue = outRA.get();
    correctValues = new Float[] { 4.0f, 4.0f, 0.0f };
    i = 0;
    for (FloatType value : outvalue) {
        type.set(correctValues[i]);
        assertEquals(type, value);
        i++;
    }
    outRA.setPosition(new int[] { 9, 9, 2 });
    outvalue = outRA.get();
    correctValues = new Float[] { 4.0f, 4.0f, 0.0f };
    i = 0;
    for (FloatType value : outvalue) {
        type.set(correctValues[i]);
        assertEquals(type, value);
        i++;
    }
    // position 9,10 in all dimensions
    outRA.setPosition(new int[] { 9, 10, 0 });
    outvalue = outRA.get();
    correctValues = new Float[] { 0.0f, 0.0f, 0.0f };
    i = 0;
    for (FloatType value : outvalue) {
        type.set(correctValues[i]);
        assertEquals(type, value);
        i++;
    }
    outRA.setPosition(new int[] { 9, 10, 1 });
    outvalue = outRA.get();
    correctValues = new Float[] { 0.0f, 0.0f, 0.0f };
    i = 0;
    for (FloatType value : outvalue) {
        type.set(correctValues[i]);
        assertEquals(type, value);
        i++;
    }
    outRA.setPosition(new int[] { 9, 10, 2 });
    outvalue = outRA.get();
    correctValues = new Float[] { 0.0f, 0.0f, 0.0f };
    i = 0;
    for (FloatType value : outvalue) {
        type.set(correctValues[i]);
        assertEquals(type, value);
        i++;
    }
    // position 9,11 in all dimensions
    outRA.setPosition(new int[] { 9, 11, 0 });
    outvalue = outRA.get();
    correctValues = new Float[] { 4.0f, -4.0f, 0.0f };
    i = 0;
    for (FloatType value : outvalue) {
        type.set(correctValues[i]);
        assertEquals(type, value);
        i++;
    }
    outRA.setPosition(new int[] { 9, 11, 1 });
    outvalue = outRA.get();
    correctValues = new Float[] { 4.0f, -4.0f, 0.0f };
    i = 0;
    for (FloatType value : outvalue) {
        type.set(correctValues[i]);
        assertEquals(type, value);
        i++;
    }
    outRA.setPosition(new int[] { 9, 11, 2 });
    outvalue = outRA.get();
    correctValues = new Float[] { 4.0f, -4.0f, 0.0f };
    i = 0;
    for (FloatType value : outvalue) {
        type.set(correctValues[i]);
        assertEquals(type, value);
        i++;
    }
}
Also used : CompositeView(net.imglib2.view.composite.CompositeView) RealComposite(net.imglib2.view.composite.RealComposite) FloatType(net.imglib2.type.numeric.real.FloatType) AbstractOpTest(net.imagej.ops.AbstractOpTest) Test(org.junit.Test)

Example 40 with Cursor

use of net.imglib2.Cursor in project imagej-ops by imagej.

the class ColocalisationTest method loadTiffFromJar.

/**
 * Loads a Tiff file from within the jar to use as a mask Cursor.
 * So we use Img<T> which has a cursor() method.
 * The given path is treated
 * as relative to this tests-package (i.e. "Data/test.tiff" refers
 * to the test.tiff in sub-folder Data).
 *
 * @param <T> The wanted output type.
 * @param relPath The relative path to the Tiff file.
 * @return The file as ImgLib image.
 */
private <T extends RealType<T> & NativeType<T>> Img<T> loadTiffFromJar(final String relPath) {
    // InputStream is = TestImageAccessor.class.getResourceAsStream(relPath);
    // BufferedInputStream bis = new BufferedInputStream(is);
    final ImgOpener opener = new ImgOpener(context);
    // HACK: Read data from file system for now.
    // Until this is fixed, the test will not pass when run from a JAR file.
    String source = "src/test/resources/net/imagej/ops/coloc/" + relPath;
    try {
        return (Img) opener.openImgs(source).get(0);
    } catch (final ImgIOException exc) {
        throw new IllegalStateException("File " + relPath + " is unexpectedly inaccessible?");
    }
}
Also used : Img(net.imglib2.img.Img) ImgIOException(io.scif.img.ImgIOException) ImgOpener(io.scif.img.ImgOpener)

Aggregations

Test (org.junit.Test)43 AbstractOpTest (net.imagej.ops.AbstractOpTest)40 DoubleType (net.imglib2.type.numeric.real.DoubleType)30 Random (java.util.Random)21 FinalInterval (net.imglib2.FinalInterval)21 ByteType (net.imglib2.type.numeric.integer.ByteType)14 RandomAccessibleInterval (net.imglib2.RandomAccessibleInterval)12 RectangleShape (net.imglib2.algorithm.neighborhood.RectangleShape)11 ArrayList (java.util.ArrayList)10 DiamondShape (net.imglib2.algorithm.neighborhood.DiamondShape)10 Shape (net.imglib2.algorithm.neighborhood.Shape)10 BitType (net.imglib2.type.logic.BitType)8 FloatType (net.imglib2.type.numeric.real.FloatType)8 Img (net.imglib2.img.Img)7 HorizontalLineShape (net.imglib2.algorithm.neighborhood.HorizontalLineShape)6 UnsignedByteType (net.imglib2.type.numeric.integer.UnsignedByteType)6 Before (org.junit.Before)6 IterableInterval (net.imglib2.IterableInterval)4 Point (net.imglib2.Point)4 RealPoint (net.imglib2.RealPoint)4