Search in sources :

Example 6 with Ops

use of net.imagej.ops.Ops in project imagej-ops by imagej.

the class SobelRAI method initialize.

@SuppressWarnings("unchecked")
@Override
public void initialize() {
    createRAI = RAIs.function(ops(), Ops.Create.Img.class, in());
    Sqr squareOp = ops().op(Ops.Math.Sqr.class, RealType.class, RealType.class);
    squareMapOp = RAIs.computer(ops(), Ops.Map.class, in(), squareOp);
    Sqrt sqrtOp = ops().op(Ops.Math.Sqrt.class, RealType.class, RealType.class);
    sqrtMapOp = RAIs.computer(ops(), Ops.Map.class, in(), sqrtOp);
    addOp = RAIs.binaryComputer(ops(), Ops.Math.Add.class, in(), in());
    derivativeComputers = new UnaryComputerOp[in().numDimensions()];
    for (int i = 0; i < in().numDimensions(); i++) {
        derivativeComputers[i] = RAIs.computer(ops(), Ops.Filter.PartialDerivative.class, in(), i);
    }
}
Also used : Ops(net.imagej.ops.Ops) Sqr(net.imagej.ops.Ops.Math.Sqr) Sqrt(net.imagej.ops.Ops.Math.Sqrt)

Example 7 with Ops

use of net.imagej.ops.Ops in project imagej-ops by imagej.

the class MapTest method testIIAndIIInplaceParallelCellImg.

@Test
public void testIIAndIIInplaceParallelCellImg() {
    final Img<ByteType> first = generateByteTestCellImg(true, 40, 20);
    final Img<ByteType> firstCopy = first.copy();
    final Img<ByteType> second = generateByteTestCellImg(false, 40, 20);
    for (final ByteType px : second) px.set((byte) 1);
    final Img<ByteType> secondCopy = second.copy();
    sub = Inplaces.binary(ops, Ops.Math.Subtract.class, ByteType.class);
    final BinaryInplaceOp<? super Img<ByteType>, Img<ByteType>> map = Inplaces.binary(ops, MapIIAndIIInplaceParallel.class, firstCopy, second, sub);
    map.run(firstCopy, second, firstCopy);
    map.run(first, secondCopy, secondCopy);
    assertImgSubEquals(first, second, firstCopy);
    assertImgSubEquals(first, second, secondCopy);
}
Also used : Img(net.imglib2.img.Img) Ops(net.imagej.ops.Ops) ByteType(net.imglib2.type.numeric.integer.ByteType) AbstractOpTest(net.imagej.ops.AbstractOpTest) Test(org.junit.Test)

Example 8 with Ops

use of net.imagej.ops.Ops in project imagej-ops by imagej.

the class MapTest method testIIAndIIInplaceParallel.

@Test
public void testIIAndIIInplaceParallel() {
    final Img<ByteType> first = generateByteArrayTestImg(true, 10, 10);
    final Img<ByteType> firstCopy = first.copy();
    final Img<ByteType> second = generateByteArrayTestImg(false, 10, 10);
    for (final ByteType px : second) px.set((byte) 1);
    final Img<ByteType> secondCopy = second.copy();
    final Img<ByteType> secondDiffDims = generateByteArrayTestImg(false, 10, 10, 2);
    sub = Inplaces.binary(ops, Ops.Math.Subtract.class, ByteType.class);
    final BinaryInplaceOp<? super Img<ByteType>, Img<ByteType>> map = Inplaces.binary(ops, MapIIAndIIInplaceParallel.class, firstCopy, second, sub);
    map.run(firstCopy, second, firstCopy);
    map.run(first, secondCopy, secondCopy);
    assertImgSubEquals(first, second, firstCopy);
    assertImgSubEquals(first, second, secondCopy);
    // Expect exception when in2 has different dimensions
    thrown.expect(IllegalArgumentException.class);
    ops.op(MapIIAndIIInplaceParallel.class, first, secondDiffDims, sub);
}
Also used : Img(net.imglib2.img.Img) Ops(net.imagej.ops.Ops) ByteType(net.imglib2.type.numeric.integer.ByteType) AbstractOpTest(net.imagej.ops.AbstractOpTest) Test(org.junit.Test)

Example 9 with Ops

use of net.imagej.ops.Ops in project imagej-ops by imagej.

the class MapTest method testIIAndIIInplace.

@Test
public void testIIAndIIInplace() {
    final Img<ByteType> first = generateByteArrayTestImg(true, 10, 10);
    final Img<ByteType> firstCopy = first.copy();
    final Img<ByteType> second = generateByteArrayTestImg(false, 10, 10);
    for (final ByteType px : second) px.set((byte) 1);
    final Img<ByteType> secondCopy = second.copy();
    final Img<ByteType> secondDiffDims = generateByteArrayTestImg(false, 10, 10, 2);
    sub = Inplaces.binary(ops, Ops.Math.Subtract.class, ByteType.class);
    final BinaryInplaceOp<? super Img<ByteType>, Img<ByteType>> map = Inplaces.binary(ops, MapIIAndIIInplace.class, firstCopy, second, sub);
    map.run(firstCopy, second, firstCopy);
    map.run(first, secondCopy, secondCopy);
    assertImgSubEquals(first, second, firstCopy);
    assertImgSubEquals(first, second, secondCopy);
    // Expect exception when in2 has different dimensions
    thrown.expect(IllegalArgumentException.class);
    ops.op(MapIIAndIIInplace.class, first, secondDiffDims, sub);
}
Also used : Img(net.imglib2.img.Img) Ops(net.imagej.ops.Ops) ByteType(net.imglib2.type.numeric.integer.ByteType) AbstractOpTest(net.imagej.ops.AbstractOpTest) Test(org.junit.Test)

Example 10 with Ops

use of net.imagej.ops.Ops in project imagej-ops by imagej.

the class WatershedSeeded method compute.

@SuppressWarnings("unchecked")
@Override
public void compute(final RandomAccessibleInterval<T> in, final ImgLabeling<Integer, IntType> out) {
    // extend border to be able to do a quick check, if a voxel is inside
    final LabelingType<Integer> oustide = out.firstElement().copy();
    oustide.clear();
    oustide.add(OUTSIDE);
    final ExtendedRandomAccessibleInterval<LabelingType<Integer>, ImgLabeling<Integer, IntType>> outExt = Views.extendValue(out, oustide);
    final OutOfBounds<LabelingType<Integer>> raOut = outExt.randomAccess();
    // if no mask provided, set the mask to the whole image
    if (mask == null) {
        mask = (RandomAccessibleInterval<B>) ops().create().img(in, new BitType());
        for (B b : Views.flatIterable(mask)) {
            b.set(true);
        }
    }
    // initialize output labels
    final Cursor<B> maskCursor = Views.flatIterable(mask).cursor();
    while (maskCursor.hasNext()) {
        maskCursor.fwd();
        if (maskCursor.get().get()) {
            raOut.setPosition(maskCursor);
            raOut.get().clear();
            raOut.get().add(INIT);
        }
    }
    // RandomAccess for Mask, Seeds and Neighborhoods
    final RandomAccess<B> raMask = mask.randomAccess();
    final RandomAccess<LabelingType<Integer>> raSeeds = seeds.randomAccess();
    final Shape shape;
    if (useEightConnectivity) {
        shape = new RectangleShape(1, true);
    } else {
        shape = new DiamondShape(1);
    }
    final RandomAccessible<Neighborhood<T>> neighborhoods = shape.neighborhoodsRandomAccessible(in);
    final RandomAccess<Neighborhood<T>> raNeigh = neighborhoods.randomAccess();
    /*
		 * Carry over the seeding points to the new label and adds them to a
		 * voxel priority queue
		 */
    final PriorityQueue<WatershedVoxel> pq = new PriorityQueue<>();
    // Only iterate seeds that are not excluded by the mask
    final IterableRegion<B> maskRegions = Regions.iterable(mask);
    final IterableInterval<LabelingType<Integer>> seedsMasked = Regions.sample(maskRegions, seeds);
    final Cursor<LabelingType<Integer>> cursorSeeds = seedsMasked.localizingCursor();
    while (cursorSeeds.hasNext()) {
        final Set<Integer> l = cursorSeeds.next();
        if (l.isEmpty()) {
            continue;
        }
        if (l.size() > 1) {
            throw new IllegalArgumentException("Seeds must have exactly one label!");
        }
        final Integer label = l.iterator().next();
        if (label < 0) {
            throw new IllegalArgumentException("Seeds must have positive integers as labels!");
        }
        raNeigh.setPosition(cursorSeeds);
        final Cursor<T> neighborhood = raNeigh.get().cursor();
        // Add unlabeled neighbors to priority queue
        while (neighborhood.hasNext()) {
            neighborhood.fwd();
            raSeeds.setPosition(neighborhood);
            raMask.setPosition(neighborhood);
            raOut.setPosition(neighborhood);
            final Integer labelNeigh = raOut.get().iterator().next();
            if (labelNeigh != INQUEUE && labelNeigh != OUTSIDE && !raOut.isOutOfBounds() && raMask.get().get() && raSeeds.get().isEmpty()) {
                raOut.setPosition(neighborhood);
                pq.add(new WatershedVoxel(IntervalIndexer.positionToIndex(neighborhood, in), neighborhood.get().getRealDouble()));
                raOut.get().clear();
                raOut.get().add(INQUEUE);
            }
        }
        // Overwrite label in output with the seed label
        raOut.setPosition(cursorSeeds);
        raOut.get().clear();
        raOut.get().add(label);
    }
    /*
		 * Pop the head of the priority queue, label and push all unlabeled
		 * neighbored pixels.
		 */
    // list to store neighbor labels
    final ArrayList<Integer> neighborLabels = new ArrayList<>();
    // list to store neighbor voxels
    final ArrayList<WatershedVoxel> neighborVoxels = new ArrayList<>();
    // iterate the queue
    final Point pos = new Point(in.numDimensions());
    while (!pq.isEmpty()) {
        IntervalIndexer.indexToPosition(pq.poll().getPos(), out, pos);
        // reset list of neighbor labels
        neighborLabels.clear();
        // reset list of neighbor voxels
        neighborVoxels.clear();
        // iterate the neighborhood of the pixel
        raNeigh.setPosition(pos);
        final Cursor<T> neighborhood = raNeigh.get().cursor();
        while (neighborhood.hasNext()) {
            neighborhood.fwd();
            // Unlabeled neighbors go into the queue if they are not there
            // yet
            raOut.setPosition(neighborhood);
            raMask.setPosition(raOut);
            if (!raOut.get().isEmpty()) {
                final Integer label = raOut.get().iterator().next();
                if (label == INIT && raMask.get().get()) {
                    neighborVoxels.add(new WatershedVoxel(IntervalIndexer.positionToIndex(neighborhood, out), neighborhood.get().getRealDouble()));
                } else {
                    if (label > WSHED && (!drawWatersheds || !neighborLabels.contains(label))) {
                        // store labels of neighbors in a list
                        neighborLabels.add(label);
                    }
                }
            }
        }
        if (drawWatersheds) {
            // if the neighbors of the extracted voxel that have already
            // been labeled
            // all have the same label, then the voxel is labeled with their
            // label.
            raOut.setPosition(pos);
            raOut.get().clear();
            if (neighborLabels.size() == 1) {
                raOut.get().add(neighborLabels.get(0));
                // list
                for (final WatershedVoxel v : neighborVoxels) {
                    IntervalIndexer.indexToPosition(v.getPos(), out, raOut);
                    raOut.get().clear();
                    raOut.get().add(INQUEUE);
                    pq.add(v);
                }
            } else if (neighborLabels.size() > 1)
                raOut.get().add(WSHED);
        } else {
            if (neighborLabels.size() > 0) {
                raOut.setPosition(pos);
                raOut.get().clear();
                // take the label which most of the neighbors have
                if (neighborLabels.size() > 2) {
                    final Map<Integer, Long> countLabels = neighborLabels.stream().collect(Collectors.groupingBy(e -> e, Collectors.counting()));
                    final Integer keyMax = Collections.max(countLabels.entrySet(), Comparator.comparingLong(Map.Entry::getValue)).getKey();
                    raOut.get().add(keyMax);
                } else {
                    raOut.get().add(neighborLabels.get(0));
                }
                // list
                for (final WatershedVoxel v : neighborVoxels) {
                    IntervalIndexer.indexToPosition(v.getPos(), out, raOut);
                    raOut.get().clear();
                    raOut.get().add(INQUEUE);
                    pq.add(v);
                }
            }
        }
    }
    /*
		 * Merge already present labels before calculation of watershed
		 */
    if (out() != null) {
        final Cursor<LabelingType<Integer>> cursor = out().cursor();
        while (cursor.hasNext()) {
            cursor.fwd();
            raOut.setPosition(cursor);
            final List<Integer> labels = new ArrayList<>();
            cursor.get().iterator().forEachRemaining(labels::add);
            raOut.get().addAll(labels);
        }
    }
}
Also used : DiamondShape(net.imglib2.algorithm.neighborhood.DiamondShape) IterableRegion(net.imglib2.roi.IterableRegion) PriorityQueue(java.util.PriorityQueue) Contingent(net.imagej.ops.Contingent) Point(net.imglib2.Point) OutOfBounds(net.imglib2.outofbounds.OutOfBounds) ArrayList(java.util.ArrayList) Intervals(net.imglib2.util.Intervals) Cursor(net.imglib2.Cursor) RandomAccessibleInterval(net.imglib2.RandomAccessibleInterval) BooleanType(net.imglib2.type.BooleanType) Map(java.util.Map) AbstractUnaryHybridCF(net.imagej.ops.special.hybrid.AbstractUnaryHybridCF) Functions(net.imagej.ops.special.function.Functions) Views(net.imglib2.view.Views) ExtendedRandomAccessibleInterval(net.imglib2.view.ExtendedRandomAccessibleInterval) BitType(net.imglib2.type.logic.BitType) RandomAccess(net.imglib2.RandomAccess) Shape(net.imglib2.algorithm.neighborhood.Shape) Regions(net.imglib2.roi.Regions) Parameter(org.scijava.plugin.Parameter) Set(java.util.Set) IntervalIndexer(net.imglib2.util.IntervalIndexer) IntType(net.imglib2.type.numeric.integer.IntType) Collectors(java.util.stream.Collectors) RectangleShape(net.imglib2.algorithm.neighborhood.RectangleShape) AtomicLong(java.util.concurrent.atomic.AtomicLong) Plugin(org.scijava.plugin.Plugin) CreateImgLabelingFromInterval(net.imagej.ops.create.imgLabeling.CreateImgLabelingFromInterval) List(java.util.List) Neighborhood(net.imglib2.algorithm.neighborhood.Neighborhood) LabelingType(net.imglib2.roi.labeling.LabelingType) UnaryFunctionOp(net.imagej.ops.special.function.UnaryFunctionOp) ImgLabeling(net.imglib2.roi.labeling.ImgLabeling) Ops(net.imagej.ops.Ops) Interval(net.imglib2.Interval) RandomAccessible(net.imglib2.RandomAccessible) Comparator(java.util.Comparator) RealType(net.imglib2.type.numeric.RealType) Collections(java.util.Collections) IterableInterval(net.imglib2.IterableInterval) DiamondShape(net.imglib2.algorithm.neighborhood.DiamondShape) Shape(net.imglib2.algorithm.neighborhood.Shape) RectangleShape(net.imglib2.algorithm.neighborhood.RectangleShape) ArrayList(java.util.ArrayList) BitType(net.imglib2.type.logic.BitType) LabelingType(net.imglib2.roi.labeling.LabelingType) ImgLabeling(net.imglib2.roi.labeling.ImgLabeling) DiamondShape(net.imglib2.algorithm.neighborhood.DiamondShape) Point(net.imglib2.Point) PriorityQueue(java.util.PriorityQueue) Neighborhood(net.imglib2.algorithm.neighborhood.Neighborhood) RectangleShape(net.imglib2.algorithm.neighborhood.RectangleShape) AtomicLong(java.util.concurrent.atomic.AtomicLong)

Aggregations

Ops (net.imagej.ops.Ops)17 UnaryComputerOp (net.imagej.ops.special.computer.UnaryComputerOp)8 BitType (net.imglib2.type.logic.BitType)8 DoubleType (net.imglib2.type.numeric.real.DoubleType)8 LocalThresholdMethod (net.imagej.ops.threshold.LocalThresholdMethod)5 Img (net.imglib2.img.Img)4 Test (org.junit.Test)4 AbstractOpTest (net.imagej.ops.AbstractOpTest)3 RandomAccessibleInterval (net.imglib2.RandomAccessibleInterval)3 ArrayList (java.util.ArrayList)2 MapNeighborhood (net.imagej.ops.map.neighborhood.MapNeighborhood)2 AbstractUnaryComputerOp (net.imagej.ops.special.computer.AbstractUnaryComputerOp)2 Interval (net.imglib2.Interval)2 ByteType (net.imglib2.type.numeric.integer.ByteType)2 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 List (java.util.List)1 Map (java.util.Map)1 PriorityQueue (java.util.PriorityQueue)1 Set (java.util.Set)1