Search in sources :

Example 1 with Neighborhood

use of net.imglib2.algorithm.neighborhood.Neighborhood in project imagej-ops by imagej.

the class Morphologies method computeMinSize.

/**
 * Computes the min coordinate and the size of an {@link Interval} after
 * padding with a list of {@link Shape}s in a series morphology operations.
 *
 * @param source the interval to be applied with some morphology operation
 * @param shapes the list of Shapes for padding
 * @return a size-2 array storing the min coordinate and the size of the
 *         padded interval
 */
public static final long[][] computeMinSize(final Interval source, final List<Shape> shapes) {
    final int numDims = source.numDimensions();
    final long[] min = new long[numDims];
    final long[] size = new long[numDims];
    for (int i = 0; i < numDims; i++) {
        min[i] = source.min(i);
        size[i] = source.dimension(i);
    }
    for (final Shape shape : shapes) {
        final Neighborhood<BitType> nh = MorphologyUtils.getNeighborhood(shape, source);
        for (int i = 0; i < numDims; i++) {
            min[i] += nh.min(i);
            size[i] += nh.dimension(i) - 1;
        }
    }
    return new long[][] { min, size };
}
Also used : Shape(net.imglib2.algorithm.neighborhood.Shape) BitType(net.imglib2.type.logic.BitType)

Example 2 with Neighborhood

use of net.imglib2.algorithm.neighborhood.Neighborhood in project imagej-ops by imagej.

the class LocalPhansalkarThreshold method unaryComputer.

@Override
protected CenterAwareComputerOp<T, BitType> unaryComputer(final T inClass, final BitType outClass) {
    final LocalThresholdMethod<T> op = new LocalThresholdMethod<T>() {

        private UnaryComputerOp<Iterable<T>, DoubleType> mean;

        private UnaryComputerOp<Iterable<T>, DoubleType> stdDeviation;

        @Override
        public void compute(final Iterable<T> neighborhood, final T center, final BitType output) {
            if (mean == null) {
                mean = Computers.unary(ops(), Ops.Stats.Mean.class, new DoubleType(), neighborhood);
            }
            if (stdDeviation == null) {
                stdDeviation = Computers.unary(ops(), Ops.Stats.StdDev.class, new DoubleType(), neighborhood);
            }
            final DoubleType meanValue = new DoubleType();
            mean.compute(neighborhood, meanValue);
            final DoubleType stdDevValue = new DoubleType();
            stdDeviation.compute(neighborhood, stdDevValue);
            double threshold = meanValue.get() * (1.0d + p * Math.exp(-q * meanValue.get()) + k * ((stdDevValue.get() / r) - 1.0));
            output.set(center.getRealDouble() >= threshold);
        }
    };
    op.setEnvironment(ops());
    return op;
}
Also used : UnaryComputerOp(net.imagej.ops.special.computer.UnaryComputerOp) Ops(net.imagej.ops.Ops) BitType(net.imglib2.type.logic.BitType) DoubleType(net.imglib2.type.numeric.real.DoubleType) LocalThresholdMethod(net.imagej.ops.threshold.LocalThresholdMethod)

Example 3 with Neighborhood

use of net.imglib2.algorithm.neighborhood.Neighborhood in project imagej-ops by imagej.

the class HistogramOfOrientedGradients2D method compute.

@SuppressWarnings("unchecked")
@Override
public void compute(RandomAccessibleInterval<T> in, RandomAccessibleInterval<T> out) {
    final RandomAccessible<FloatType> convertedIn = Converters.convert(Views.extendMirrorDouble(in), converterToFloat, new FloatType());
    // compute partial derivative for each dimension
    RandomAccessibleInterval<FloatType> derivative0 = createImgOp.calculate();
    RandomAccessibleInterval<FloatType> derivative1 = createImgOp.calculate();
    // case of grayscale image
    if (in.numDimensions() == 2) {
        PartialDerivative.gradientCentralDifference(convertedIn, derivative0, 0);
        PartialDerivative.gradientCentralDifference(convertedIn, derivative1, 1);
    } else // case of color image
    {
        List<RandomAccessibleInterval<FloatType>> listDerivs0 = new ArrayList<>();
        List<RandomAccessibleInterval<FloatType>> listDerivs1 = new ArrayList<>();
        for (int i = 0; i < in.dimension(2); i++) {
            final RandomAccessibleInterval<FloatType> deriv0 = createImgOp.calculate();
            final RandomAccessibleInterval<FloatType> deriv1 = createImgOp.calculate();
            PartialDerivative.gradientCentralDifference(Views.interval(convertedIn, new long[] { 0, 0, i }, new long[] { in.max(0), in.max(1), i }), deriv0, 0);
            PartialDerivative.gradientCentralDifference(Views.interval(convertedIn, new long[] { 0, 0, i }, new long[] { in.max(0), in.max(1), i }), deriv1, 1);
            listDerivs0.add(deriv0);
            listDerivs1.add(deriv1);
        }
        derivative0 = Converters.convert(Views.collapse(Views.stack(listDerivs0)), converterGetMax, new FloatType());
        derivative1 = Converters.convert(Views.collapse(Views.stack(listDerivs1)), converterGetMax, new FloatType());
    }
    final RandomAccessibleInterval<FloatType> finalderivative0 = derivative0;
    final RandomAccessibleInterval<FloatType> finalderivative1 = derivative1;
    // compute angles and magnitudes
    final RandomAccessibleInterval<FloatType> angles = createImgOp.calculate();
    final RandomAccessibleInterval<FloatType> magnitudes = createImgOp.calculate();
    final CursorBasedChunk chunkable = new CursorBasedChunk() {

        @Override
        public void execute(int startIndex, int stepSize, int numSteps) {
            final Cursor<FloatType> cursorAngles = Views.flatIterable(angles).localizingCursor();
            final Cursor<FloatType> cursorMagnitudes = Views.flatIterable(magnitudes).localizingCursor();
            final Cursor<FloatType> cursorDerivative0 = Views.flatIterable(finalderivative0).localizingCursor();
            final Cursor<FloatType> cursorDerivative1 = Views.flatIterable(finalderivative1).localizingCursor();
            setToStart(cursorAngles, startIndex);
            setToStart(cursorMagnitudes, startIndex);
            setToStart(cursorDerivative0, startIndex);
            setToStart(cursorDerivative1, startIndex);
            for (int i = 0; i < numSteps; i++) {
                final float x = cursorDerivative0.get().getRealFloat();
                final float y = cursorDerivative1.get().getRealFloat();
                cursorAngles.get().setReal(getAngle(x, y));
                cursorMagnitudes.get().setReal(getMagnitude(x, y));
                cursorAngles.jumpFwd(stepSize);
                cursorMagnitudes.jumpFwd(stepSize);
                cursorDerivative0.jumpFwd(stepSize);
                cursorDerivative1.jumpFwd(stepSize);
            }
        }
    };
    ops().thread().chunker(chunkable, Views.flatIterable(magnitudes).size());
    // stores each Thread to execute
    final List<Callable<Void>> listCallables = new ArrayList<>();
    // compute descriptor (default 3x3, i.e. 9 channels: one channel for
    // each bin)
    final RectangleShape shape = new RectangleShape(spanOfNeighborhood, false);
    final NeighborhoodsAccessible<FloatType> neighborHood = shape.neighborhoodsRandomAccessible(angles);
    for (int i = 0; i < in.dimension(0); i++) {
        listCallables.add(new ComputeDescriptor(Views.interval(convertedIn, in), i, angles.randomAccess(), magnitudes.randomAccess(), (RandomAccess<FloatType>) out.randomAccess(), neighborHood.randomAccess()));
    }
    try {
        es.invokeAll(listCallables);
    } catch (final InterruptedException e) {
        throw new RuntimeException(e);
    }
    listCallables.clear();
}
Also used : CursorBasedChunk(net.imagej.ops.thread.chunker.CursorBasedChunk) ArrayList(java.util.ArrayList) RandomAccess(net.imglib2.RandomAccess) Callable(java.util.concurrent.Callable) FloatType(net.imglib2.type.numeric.real.FloatType) RectangleShape(net.imglib2.algorithm.neighborhood.RectangleShape) RandomAccessibleInterval(net.imglib2.RandomAccessibleInterval)

Example 4 with Neighborhood

use of net.imglib2.algorithm.neighborhood.Neighborhood in project imagej-ops by imagej.

the class WatershedSeededTest method testWithMask.

@SuppressWarnings("unchecked")
private void testWithMask(final RandomAccessibleInterval<FloatType> in, final ImgLabeling<Integer, IntType> seeds) {
    // create mask which is 1 everywhere
    long[] dims = new long[in.numDimensions()];
    in.dimensions(dims);
    Img<BitType> mask = ArrayImgs.bits(dims);
    RandomAccess<BitType> raMask = mask.randomAccess();
    for (BitType b : mask) {
        b.setZero();
    }
    for (int x = 0; x < 10; x++) {
        for (int y = 0; y < 10; y++) {
            raMask.setPosition(new int[] { x, y });
            raMask.get().setOne();
        }
    }
    /*
		 * use 8-connected neighborhood
		 */
    // compute result without watersheds
    ImgLabeling<Integer, IntType> out = (ImgLabeling<Integer, IntType>) ops.run(WatershedSeeded.class, null, in, seeds, true, false, mask);
    assertResults(in, out, seeds, mask, false, true);
    // compute result with watersheds
    ImgLabeling<Integer, IntType> out2 = (ImgLabeling<Integer, IntType>) ops.run(WatershedSeeded.class, null, in, seeds, true, true, mask);
    assertResults(in, out2, seeds, mask, true, true);
    /*
		 * use 4-connected neighborhood
		 */
    // compute result without watersheds
    ImgLabeling<Integer, IntType> out3 = (ImgLabeling<Integer, IntType>) ops.run(WatershedSeeded.class, null, in, seeds, false, false, mask);
    assertResults(in, out3, seeds, mask, false, true);
    // compute result with watersheds
    ImgLabeling<Integer, IntType> out4 = (ImgLabeling<Integer, IntType>) ops.run(WatershedSeeded.class, null, in, seeds, false, true, mask);
    assertResults(in, out4, seeds, mask, true, true);
}
Also used : BitType(net.imglib2.type.logic.BitType) ImgLabeling(net.imglib2.roi.labeling.ImgLabeling) IntType(net.imglib2.type.numeric.integer.IntType)

Example 5 with Neighborhood

use of net.imglib2.algorithm.neighborhood.Neighborhood in project imagej-ops by imagej.

the class WatershedTest method testWithoutMask.

@SuppressWarnings("unchecked")
private void testWithoutMask(final RandomAccessibleInterval<FloatType> in) {
    // create mask which is 1 everywhere
    long[] dims = new long[in.numDimensions()];
    in.dimensions(dims);
    Img<BitType> mask = ArrayImgs.bits(dims);
    for (BitType b : mask) {
        b.setOne();
    }
    /*
		 * use 8-connected neighborhood
		 */
    // compute result without watersheds
    ImgLabeling<Integer, IntType> out = (ImgLabeling<Integer, IntType>) ops.run(Watershed.class, null, in, true, false);
    assertResults(in, out, mask, true, false, false);
    // compute result with watersheds
    ImgLabeling<Integer, IntType> out2 = (ImgLabeling<Integer, IntType>) ops.run(Watershed.class, null, in, true, true);
    assertResults(in, out2, mask, true, true, false);
    /*
		 * use 4-connected neighborhood
		 */
    // compute result without watersheds
    ImgLabeling<Integer, IntType> out3 = (ImgLabeling<Integer, IntType>) ops.run(Watershed.class, null, in, false, false);
    assertResults(in, out3, mask, false, false, false);
    // compute result with watersheds
    ImgLabeling<Integer, IntType> out4 = (ImgLabeling<Integer, IntType>) ops.run(Watershed.class, null, in, false, true);
    assertResults(in, out4, mask, false, true, false);
}
Also used : BitType(net.imglib2.type.logic.BitType) ImgLabeling(net.imglib2.roi.labeling.ImgLabeling) IntType(net.imglib2.type.numeric.integer.IntType)

Aggregations

BitType (net.imglib2.type.logic.BitType)11 Ops (net.imagej.ops.Ops)7 RectangleShape (net.imglib2.algorithm.neighborhood.RectangleShape)7 UnaryComputerOp (net.imagej.ops.special.computer.UnaryComputerOp)6 IntType (net.imglib2.type.numeric.integer.IntType)6 DoubleType (net.imglib2.type.numeric.real.DoubleType)6 LocalThresholdMethod (net.imagej.ops.threshold.LocalThresholdMethod)5 Shape (net.imglib2.algorithm.neighborhood.Shape)5 ImgLabeling (net.imglib2.roi.labeling.ImgLabeling)5 RandomAccessibleInterval (net.imglib2.RandomAccessibleInterval)4 Neighborhood (net.imglib2.algorithm.neighborhood.Neighborhood)4 ByteType (net.imglib2.type.numeric.integer.ByteType)4 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)3 AbstractOpTest (net.imagej.ops.AbstractOpTest)2 Op (net.imagej.ops.Op)2 AbstractUnaryComputerOp (net.imagej.ops.special.computer.AbstractUnaryComputerOp)2 Interval (net.imglib2.Interval)2 RandomAccess (net.imglib2.RandomAccess)2 DiamondShape (net.imglib2.algorithm.neighborhood.DiamondShape)2