Search in sources :

Example 11 with FinalInterval

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

the class PartialDerivativeRAI method initialize.

@SuppressWarnings("unchecked")
@Override
public void initialize() {
    RandomAccessibleInterval<T> kernel = ops().create().kernelSobel(Util.getTypeFromInterval(in()));
    RandomAccessibleInterval<T> kernelA = Views.hyperSlice(Views.hyperSlice(kernel, 3, 0), 2, 0);
    RandomAccessibleInterval<T> kernelB = Views.hyperSlice(Views.hyperSlice(kernel, 3, 0), 2, 1);
    // add dimensions to kernel to rotate properly
    if (in().numDimensions() > 2) {
        RandomAccessible<T> expandedKernelA = Views.addDimension(kernelA);
        RandomAccessible<T> expandedKernelB = Views.addDimension(kernelB);
        for (int i = 0; i < in().numDimensions() - 3; i++) {
            expandedKernelA = Views.addDimension(expandedKernelA);
            expandedKernelB = Views.addDimension(expandedKernelB);
        }
        long[] dims = new long[in().numDimensions()];
        for (int j = 0; j < in().numDimensions(); j++) {
            dims[j] = 1;
        }
        dims[0] = 3;
        Interval kernelInterval = new FinalInterval(dims);
        kernelA = Views.interval(expandedKernelA, kernelInterval);
        kernelB = Views.interval(expandedKernelB, kernelInterval);
    }
    long[] dims = new long[in().numDimensions()];
    if (dimension == 0) {
        // FIXME hack
        kernelBConvolveOp = RAIs.computer(ops(), Ops.Filter.Convolve.class, in(), new Object[] { kernelB });
    } else {
        // rotate kernel B to dimension
        for (int j = 0; j < in().numDimensions(); j++) {
            if (j == dimension) {
                dims[j] = 3;
            } else {
                dims[j] = 1;
            }
        }
        Img<DoubleType> kernelInterval = ops().create().img(dims);
        RandomAccessibleInterval<T> rotatedKernelB = kernelB;
        for (int i = 0; i < dimension; i++) {
            rotatedKernelB = Views.rotate(rotatedKernelB, i, i + 1);
        }
        rotatedKernelB = Views.interval(rotatedKernelB, kernelInterval);
        kernelBConvolveOp = RAIs.computer(ops(), Ops.Filter.Convolve.class, in(), new Object[] { rotatedKernelB });
    }
    dims = null;
    // rotate kernel A to all other dimensions
    kernelAConvolveOps = new UnaryComputerOp[in().numDimensions()];
    if (dimension != 0) {
        kernelAConvolveOps[0] = RAIs.computer(ops(), Ops.Filter.Convolve.class, in(), new Object[] { kernelA });
    }
    RandomAccessibleInterval<T> rotatedKernelA = kernelA;
    for (int i = 1; i < in().numDimensions(); i++) {
        if (i != dimension) {
            dims = new long[in().numDimensions()];
            for (int j = 0; j < in().numDimensions(); j++) {
                if (i == j) {
                    dims[j] = 3;
                } else {
                    dims[j] = 1;
                }
            }
            Img<DoubleType> kernelInterval = ops().create().img(dims);
            for (int j = 0; j < i; j++) {
                rotatedKernelA = Views.rotate(rotatedKernelA, j, j + 1);
            }
            kernelAConvolveOps[i] = RAIs.computer(ops(), Ops.Filter.Convolve.class, in(), new Object[] { Views.interval(rotatedKernelA, kernelInterval) });
            rotatedKernelA = kernelA;
        }
    }
    addOp = RAIs.binaryComputer(ops(), Ops.Math.Add.class, in(), in());
    createRAI = RAIs.function(ops(), Ops.Create.Img.class, in());
}
Also used : Img(net.imglib2.img.Img) Ops(net.imagej.ops.Ops) DoubleType(net.imglib2.type.numeric.real.DoubleType) FinalInterval(net.imglib2.FinalInterval) RandomAccessibleInterval(net.imglib2.RandomAccessibleInterval) Interval(net.imglib2.Interval) FinalInterval(net.imglib2.FinalInterval)

Example 12 with FinalInterval

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

the class ConvolveNaiveC method compute.

@Override
public void compute(final RandomAccessible<I> input, final RandomAccessibleInterval<O> output) {
    // TODO: try a decomposition of the kernel into n 1-dim kernels
    final long[] min = new long[input.numDimensions()];
    final long[] max = new long[input.numDimensions()];
    for (int d = 0; d < kernel.numDimensions(); d++) {
        min[d] = -kernel.dimension(d);
        max[d] = kernel.dimension(d) + output.dimension(d);
    }
    final RandomAccess<I> inRA = input.randomAccess(new FinalInterval(min, max));
    final Cursor<K> kernelC = Views.iterable(kernel).localizingCursor();
    final Cursor<O> outC = Views.iterable(output).localizingCursor();
    final long[] pos = new long[input.numDimensions()];
    final long[] kernelRadius = new long[kernel.numDimensions()];
    for (int i = 0; i < kernelRadius.length; i++) {
        kernelRadius[i] = kernel.dimension(i) / 2;
    }
    float val;
    while (outC.hasNext()) {
        // image
        outC.fwd();
        outC.localize(pos);
        // kernel inlined version of the method convolve
        val = 0;
        inRA.setPosition(pos);
        kernelC.reset();
        while (kernelC.hasNext()) {
            kernelC.fwd();
            for (int i = 0; i < kernelRadius.length; i++) {
                // dimension can have zero extension e.g. vertical 1d kernel
                if (kernelRadius[i] > 0) {
                    inRA.setPosition(pos[i] + kernelC.getLongPosition(i) - kernelRadius[i], i);
                }
            }
            val += inRA.get().getRealDouble() * kernelC.get().getRealDouble();
        }
        outC.get().setReal(val);
    }
}
Also used : FinalInterval(net.imglib2.FinalInterval)

Example 13 with FinalInterval

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

the class DistanceTransform2DTest method test.

@SuppressWarnings("unchecked")
@Test
public void test() {
    // create 2D image
    final RandomAccessibleInterval<BitType> in = ops.create().img(new FinalInterval(20, 20), new BitType());
    generate2DImg(in);
    /*
		 * test normal DT
		 */
    RandomAccessibleInterval<FloatType> out = (RandomAccessibleInterval<FloatType>) ops.run(DistanceTransform2D.class, null, in);
    compareResults(out, in, new double[] { 1, 1 });
    /*
		 * test calibrated DT
		 */
    final double[] calibration = new double[] { 2.54, 1.77 };
    out = (RandomAccessibleInterval<FloatType>) ops.run(DistanceTransform2DCalibration.class, null, in, calibration);
    compareResults(out, in, calibration);
}
Also used : RandomAccessibleInterval(net.imglib2.RandomAccessibleInterval) BitType(net.imglib2.type.logic.BitType) FinalInterval(net.imglib2.FinalInterval) FloatType(net.imglib2.type.numeric.real.FloatType) AbstractOpTest(net.imagej.ops.AbstractOpTest) Test(org.junit.Test)

Example 14 with FinalInterval

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

the class DistanceTransform3DTest method test.

@SuppressWarnings("unchecked")
@Test
public void test() {
    // create 3D image
    final RandomAccessibleInterval<BitType> in = ops.create().img(new FinalInterval(20, 20, 5), new BitType());
    generate3DImg(in);
    /*
		 * test normal DT
		 */
    RandomAccessibleInterval<FloatType> out = (RandomAccessibleInterval<FloatType>) ops.run(DistanceTransform3D.class, null, in);
    compareResults(out, in, new double[] { 1, 1, 1 });
    /*
		 * test calibrated DT
		 */
    final double[] calibration = new double[] { 3.74, 5.19, 1.21 };
    out = (RandomAccessibleInterval<FloatType>) ops.run(DistanceTransform3DCalibration.class, null, in, calibration);
    compareResults(out, in, calibration);
}
Also used : RandomAccessibleInterval(net.imglib2.RandomAccessibleInterval) BitType(net.imglib2.type.logic.BitType) FinalInterval(net.imglib2.FinalInterval) FloatType(net.imglib2.type.numeric.real.FloatType) AbstractOpTest(net.imagej.ops.AbstractOpTest) Test(org.junit.Test)

Example 15 with FinalInterval

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

the class ShearViewTest method ShearIntervalTest.

/**
 * Tests {@link ShearViewInterval}.
 */
@Test
public void ShearIntervalTest() {
    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.shear(Views.extendZero(img), new FinalInterval(new long[] { 0, 0 }, new long[] { 3, 3 }), 0, 1).cursor();
    RandomAccess<DoubleType> opr = ops.transform().shearView(Views.extendZero(img), 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)

Aggregations

FinalInterval (net.imglib2.FinalInterval)41 AbstractOpTest (net.imagej.ops.AbstractOpTest)13 RandomAccessibleInterval (net.imglib2.RandomAccessibleInterval)13 Test (org.junit.Test)13 DoubleType (net.imglib2.type.numeric.real.DoubleType)9 Img (net.imglib2.img.Img)7 BitType (net.imglib2.type.logic.BitType)7 Interval (net.imglib2.Interval)5 UnsignedByteType (net.imglib2.type.numeric.integer.UnsignedByteType)5 FloatType (net.imglib2.type.numeric.real.FloatType)4 ImgPlus (net.imagej.ImgPlus)3 RealType (net.imglib2.type.numeric.RealType)3 ByteType (net.imglib2.type.numeric.integer.ByteType)3 File (java.io.File)2 Random (java.util.Random)2 Dataset (net.imagej.Dataset)2 CreateImgFromImg (net.imagej.ops.create.img.CreateImgFromImg)2 DefaultMesh (net.imagej.ops.geom.geom3d.mesh.DefaultMesh)2 TriangularFacet (net.imagej.ops.geom.geom3d.mesh.TriangularFacet)2 FinalDimensions (net.imglib2.FinalDimensions)2