Search in sources :

Example 1 with Shape

use of net.imglib2.algorithm.neighborhood.Shape 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 Shape

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

the class ListErode method compute.

@Override
public void compute(final RandomAccessibleInterval<T> in1, final List<Shape> in2, final IterableInterval<T> out) {
    final long[][] minSize = Morphologies.computeMinSize(in1, in2);
    final Interval interval = new FinalInterval(minSize[1]);
    Img<T> upstream = imgCreator.calculate(interval);
    Img<T> downstream = imgCreator.calculate(interval);
    Img<T> tmp;
    erodeComputer.compute(in1, in2.get(0), Views.translate(downstream, minSize[0]));
    for (int i = 1; i < in2.size(); i++) {
        // Ping-ponging intermediate results between upstream and downstream to
        // avoid repetitively creating new Imgs.
        tmp = downstream;
        downstream = upstream;
        upstream = tmp;
        erodeComputer.compute(Views.interval(Views.extendValue(upstream, maxVal), interval), in2.get(i), downstream);
    }
    if (isFull)
        copyImg.compute(downstream, out);
    else
        copyImg.compute(Views.interval(Views.translate(downstream, minSize[0]), out), out);
}
Also used : FinalInterval(net.imglib2.FinalInterval) RandomAccessibleInterval(net.imglib2.RandomAccessibleInterval) Interval(net.imglib2.Interval) FinalInterval(net.imglib2.FinalInterval) IterableInterval(net.imglib2.IterableInterval)

Example 3 with Shape

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

the class DefaultCoarsenessFeature method mean.

/**
 * Apply mean filter with given size of reactangle shape
 *
 * @param input
 *            Input image
 * @param i
 *            Size of rectangle shape
 * @return Filered mean image
 */
@SuppressWarnings("unchecked")
private Img<I> mean(final RandomAccessibleInterval<I> input, final int i) {
    long[] dims = new long[input.numDimensions()];
    input.dimensions(dims);
    final byte[] array = new byte[(int) Intervals.numElements(new FinalInterval(dims))];
    Img<I> meanImg = (Img<I>) ArrayImgs.unsignedBytes(array, dims);
    OutOfBoundsMirrorFactory<ByteType, Img<ByteType>> oobFactory = new OutOfBoundsMirrorFactory<>(Boundary.SINGLE);
    ops().run(MeanFilterOp.class, meanImg, input, new RectangleShape(i, true), oobFactory);
    return meanImg;
}
Also used : Img(net.imglib2.img.Img) RectangleShape(net.imglib2.algorithm.neighborhood.RectangleShape) FinalInterval(net.imglib2.FinalInterval) ByteType(net.imglib2.type.numeric.integer.ByteType) OutOfBoundsMirrorFactory(net.imglib2.outofbounds.OutOfBoundsMirrorFactory)

Example 4 with Shape

use of net.imglib2.algorithm.neighborhood.Shape 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 5 with Shape

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

the class BlackTopHatTest method testSingleBlackTopHat.

@Test
public void testSingleBlackTopHat() {
    final Shape shape = new DiamondShape(1);
    final List<Shape> shapes = Arrays.asList(shape);
    @SuppressWarnings("unchecked") final Img<ByteType> out1 = (Img<ByteType>) ops.run(ListBlackTopHat.class, Img.class, in, shapes);
    final Img<ByteType> out2 = BlackTopHat.blackTopHat(in, shape, 1);
    assertIterationsEqual(out2, out1);
}
Also used : Img(net.imglib2.img.Img) DiamondShape(net.imglib2.algorithm.neighborhood.DiamondShape) Shape(net.imglib2.algorithm.neighborhood.Shape) RectangleShape(net.imglib2.algorithm.neighborhood.RectangleShape) HorizontalLineShape(net.imglib2.algorithm.neighborhood.HorizontalLineShape) DiamondShape(net.imglib2.algorithm.neighborhood.DiamondShape) ByteType(net.imglib2.type.numeric.integer.ByteType) AbstractOpTest(net.imagej.ops.AbstractOpTest) Test(org.junit.Test)

Aggregations

RectangleShape (net.imglib2.algorithm.neighborhood.RectangleShape)26 ByteType (net.imglib2.type.numeric.integer.ByteType)24 Test (org.junit.Test)24 Shape (net.imglib2.algorithm.neighborhood.Shape)23 AbstractOpTest (net.imagej.ops.AbstractOpTest)22 DiamondShape (net.imglib2.algorithm.neighborhood.DiamondShape)20 HorizontalLineShape (net.imglib2.algorithm.neighborhood.HorizontalLineShape)18 ArrayList (java.util.ArrayList)12 IterableInterval (net.imglib2.IterableInterval)11 Img (net.imglib2.img.Img)11 RandomAccessibleInterval (net.imglib2.RandomAccessibleInterval)5 Neighborhood (net.imglib2.algorithm.neighborhood.Neighborhood)4 BitType (net.imglib2.type.logic.BitType)4 FinalInterval (net.imglib2.FinalInterval)3 Interval (net.imglib2.Interval)3 RandomAccess (net.imglib2.RandomAccess)2 RectangleNeighborhood (net.imglib2.algorithm.neighborhood.RectangleNeighborhood)2 LabelingType (net.imglib2.roi.labeling.LabelingType)2 IntType (net.imglib2.type.numeric.integer.IntType)2 ExtendedRandomAccessibleInterval (net.imglib2.view.ExtendedRandomAccessibleInterval)2