Search in sources :

Example 76 with Cursor

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

the class SubsampleViewTest method testIntervalSubsample.

@Test
public void testIntervalSubsample() {
    Img<DoubleType> img = ArrayImgs.doubles(10, 10);
    Random r = new Random();
    for (DoubleType d : img) {
        d.set(r.nextDouble());
    }
    SubsampleIntervalView<DoubleType> expected = Views.subsample((RandomAccessibleInterval<DoubleType>) img, 2);
    SubsampleIntervalView<DoubleType> actual = (SubsampleIntervalView<DoubleType>) ops.transform().subsampleView((RandomAccessibleInterval<DoubleType>) img, 2);
    Cursor<DoubleType> il2C = Views.interval(expected, new long[] { 0, 0 }, new long[] { 4, 4 }).localizingCursor();
    RandomAccess<DoubleType> oprRA = actual.randomAccess();
    while (il2C.hasNext()) {
        il2C.next();
        oprRA.setPosition(il2C);
        assertEquals(il2C.get().get(), oprRA.get().get(), 1e-10);
    }
    assertTrue(Intervals.equals(expected, actual));
}
Also used : SubsampleIntervalView(net.imglib2.view.SubsampleIntervalView) Random(java.util.Random) RandomAccessibleInterval(net.imglib2.RandomAccessibleInterval) DoubleType(net.imglib2.type.numeric.real.DoubleType) AbstractOpTest(net.imagej.ops.AbstractOpTest) Test(org.junit.Test)

Example 77 with Cursor

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

the class PartialDerivativeFilterTest method test.

@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().partialDerivative(img, 0);
    FloatType type = Util.getTypeFromInterval(out).createVariable();
    type.set(4.0f);
    RandomAccess<FloatType> outRA = out.randomAccess();
    for (int i = 0; i < 8; i++) {
        outRA.setPosition(new int[] { 9, i });
        assertEquals(type, outRA.get());
    }
    outRA.setPosition(new int[] { 9, 8 });
    type.set(3.0f);
    assertEquals(type, outRA.get());
    outRA.setPosition(new int[] { 9, 10 });
    type.set(0.0f);
    assertEquals(type, outRA.get());
    outRA.setPosition(new int[] { 9, 11 });
    type.set(1.0f);
    assertEquals(type, outRA.get());
    outRA.setPosition(new int[] { 9, 12 });
    type.set(3.0f);
    assertEquals(type, outRA.get());
    type.set(4.0f);
    for (int i = 13; i < 20; i++) {
        outRA.setPosition(new int[] { 9, i });
        assertEquals(type, outRA.get());
    }
    type.set(-4.0f);
    for (int i = 0; i < 8; i++) {
        outRA.setPosition(new int[] { 12, i });
        assertEquals(type, outRA.get());
    }
    outRA.setPosition(new int[] { 12, 8 });
    type.set(-3.0f);
    assertEquals(type, outRA.get());
    outRA.setPosition(new int[] { 12, 10 });
    type.set(0.0f);
    assertEquals(type, outRA.get());
    outRA.setPosition(new int[] { 12, 11 });
    type.set(-1.0f);
    assertEquals(type, outRA.get());
    outRA.setPosition(new int[] { 12, 12 });
    type.set(-3.0f);
    assertEquals(type, outRA.get());
    type.set(-4.0f);
    for (int i = 13; i < 20; i++) {
        outRA.setPosition(new int[] { 12, i });
        assertEquals(type, outRA.get());
    }
}
Also used : FloatType(net.imglib2.type.numeric.real.FloatType) AbstractOpTest(net.imagej.ops.AbstractOpTest) Test(org.junit.Test)

Example 78 with Cursor

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

the class HessianFilterTest method test.

@Test
public void test() {
    Img<FloatType> img = generateFloatArrayTestImg(false, new long[] { 50, 50 });
    Cursor<FloatType> cursorImg = img.cursor();
    int counterX = 0;
    int counterY = 0;
    while (cursorImg.hasNext()) {
        if (counterX > 20 && counterX < 30 || counterY > 20 && counterY < 30) {
            cursorImg.next().setOne();
        } else {
            cursorImg.next().setZero();
        }
        counterX++;
        if (counterX % 50 == 0) {
            counterY++;
        }
        if (counterX == 50) {
            counterX = 0;
        }
        if (counterY == 50) {
            counterY = 0;
        }
    }
    CompositeIntervalView<FloatType, RealComposite<FloatType>> out = ops.filter().hessian(img);
    Cursor<RealComposite<FloatType>> outCursor = Views.iterable(out).cursor();
    while (outCursor.hasNext()) {
        RealComposite<FloatType> values = outCursor.next();
        assertEquals(values.get(1), values.get(2));
    }
    CompositeView<FloatType, RealComposite<FloatType>>.CompositeRandomAccess outRA = out.randomAccess();
    // two numbers represent a coordinate: 20|0 ; 21|0 ...
    int[] positions = new int[] { 20, 0, 21, 0, 19, 31, 19, 30 };
    float[] valuesXX = new float[] { 16.0f, -16.0f, 15.0f, 11.0f };
    float[] valuesXY = new float[] { 0.0f, 0.0f, 1.0f, 3.0f };
    float[] valuesYY = new float[] { 0.0f, 0.0f, 15.0f, 15.0f };
    FloatType type = Util.getTypeFromInterval(img).createVariable();
    int i = 0;
    int j = 0;
    while (i < positions.length - 1) {
        int[] pos = new int[2];
        pos[0] = positions[i];
        pos[1] = positions[i + 1];
        outRA.setPosition(pos);
        type.set(valuesXX[j]);
        assertEquals(type, outRA.get().get(0));
        outRA.setPosition(pos);
        type.set(valuesXY[j]);
        assertEquals(type, outRA.get().get(1));
        outRA.setPosition(pos);
        type.set(valuesYY[j]);
        assertEquals(type, outRA.get().get(3));
        i += 2;
        j++;
    }
}
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 79 with Cursor

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

the class SobelFilterTest method test.

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

Example 80 with Cursor

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

the class AbstractOpTest method generateRandomlyFilledUnsignedByteTestImgWithSeed.

public Img<UnsignedByteType> generateRandomlyFilledUnsignedByteTestImgWithSeed(final long[] dims, final long tempSeed) {
    final Img<UnsignedByteType> img = ArrayImgs.unsignedBytes(dims);
    final Random rand = new Random(tempSeed);
    final Cursor<UnsignedByteType> cursor = img.cursor();
    while (cursor.hasNext()) {
        cursor.next().set(rand.nextInt((int) img.firstElement().getMaxValue()));
    }
    return img;
}
Also used : Random(java.util.Random) UnsignedByteType(net.imglib2.type.numeric.integer.UnsignedByteType)

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