Search in sources :

Example 1 with ImgLabeling

use of net.imglib2.roi.labeling.ImgLabeling in project imagej-ops by imagej.

the class WatershedBinarySingleSigma method compute.

@Override
public void compute(final RandomAccessibleInterval<T> in, final ImgLabeling<Integer, IntType> out) {
    // compute distance transform
    final RandomAccessibleInterval<FloatType> distMap = ops().image().distancetransform(in);
    final RandomAccessibleInterval<FloatType> invertedDT = ops().create().img(in, new FloatType());
    ops().image().invert(Views.iterable(invertedDT), Views.iterable(distMap));
    final RandomAccessibleInterval<FloatType> gauss = ops().filter().gauss(invertedDT, sigma);
    // run the default watershed
    ops().run(Watershed.class, out, gauss, useEightConnectivity, drawWatersheds, mask);
}
Also used : FloatType(net.imglib2.type.numeric.real.FloatType)

Example 2 with ImgLabeling

use of net.imglib2.roi.labeling.ImgLabeling in project imagej-ops by imagej.

the class WatershedBinaryTest method test.

@SuppressWarnings("unchecked")
@Test
public void test() {
    // load test image
    Img<FloatType> watershedTestImg = openFloatImg(WatershedTest.class, "watershed_test_image.png");
    // threshold it
    RandomAccessibleInterval<BitType> thresholdedImg = ops.create().img(watershedTestImg, new BitType());
    ops.threshold().apply(Views.flatIterable(thresholdedImg), Views.flatIterable(watershedTestImg), new FloatType(1));
    // compute inverted distance transform and smooth it with gaussian
    // filtering
    final RandomAccessibleInterval<FloatType> distMap = ops.image().distancetransform(thresholdedImg);
    final RandomAccessibleInterval<FloatType> invertedDistMap = ops.create().img(distMap, new FloatType());
    ops.image().invert(Views.iterable(invertedDistMap), Views.iterable(distMap));
    double[] sigma = { 3.0, 3.0, 0.0 };
    final RandomAccessibleInterval<FloatType> gauss = ops.filter().gauss(invertedDistMap, sigma[0], sigma[1]);
    // run randomly only one configuration to save execution time
    int nextInt = new Random().nextInt(3);
    if (nextInt == 0) {
        // compute result
        final ImgLabeling<Integer, IntType> out1 = (ImgLabeling<Integer, IntType>) ops.run(WatershedBinary.class, null, thresholdedImg, true, false, sigma, thresholdedImg);
        final ImgLabeling<Integer, IntType> expOut1 = (ImgLabeling<Integer, IntType>) ops.run(Watershed.class, null, gauss, true, false, thresholdedImg);
        assertResults(expOut1, out1);
        final ImgLabeling<Integer, IntType> out2 = (ImgLabeling<Integer, IntType>) ops.run(WatershedBinary.class, null, thresholdedImg, true, false, sigma);
        final ImgLabeling<Integer, IntType> expOut2 = (ImgLabeling<Integer, IntType>) ops.run(Watershed.class, null, gauss, true, false);
        assertResults(expOut2, out2);
    } else if (nextInt == 1) {
        // compute result
        final ImgLabeling<Integer, IntType> out1 = (ImgLabeling<Integer, IntType>) ops.run(WatershedBinary.class, null, thresholdedImg, true, true, sigma, thresholdedImg);
        final ImgLabeling<Integer, IntType> expOut1 = (ImgLabeling<Integer, IntType>) ops.run(Watershed.class, null, gauss, true, true, thresholdedImg);
        assertResults(expOut1, out1);
        final ImgLabeling<Integer, IntType> out2 = (ImgLabeling<Integer, IntType>) ops.run(WatershedBinary.class, null, thresholdedImg, true, true, sigma);
        final ImgLabeling<Integer, IntType> expOut2 = (ImgLabeling<Integer, IntType>) ops.run(Watershed.class, null, gauss, true, true);
        assertResults(expOut2, out2);
    } else {
        // compute result
        final ImgLabeling<Integer, IntType> out1 = (ImgLabeling<Integer, IntType>) ops.run(WatershedBinary.class, null, thresholdedImg, false, true, sigma, thresholdedImg);
        final ImgLabeling<Integer, IntType> expOut1 = (ImgLabeling<Integer, IntType>) ops.run(Watershed.class, null, gauss, false, true, thresholdedImg);
        assertResults(expOut1, out1);
        final ImgLabeling<Integer, IntType> out2 = (ImgLabeling<Integer, IntType>) ops.run(WatershedBinary.class, null, thresholdedImg, false, true, sigma);
        final ImgLabeling<Integer, IntType> expOut2 = (ImgLabeling<Integer, IntType>) ops.run(Watershed.class, null, gauss, false, true);
        assertResults(expOut2, out2);
    }
}
Also used : ImgLabeling(net.imglib2.roi.labeling.ImgLabeling) FloatType(net.imglib2.type.numeric.real.FloatType) IntType(net.imglib2.type.numeric.integer.IntType) Random(java.util.Random) BitType(net.imglib2.type.logic.BitType) AbstractOpTest(net.imagej.ops.AbstractOpTest) Test(org.junit.Test)

Example 3 with ImgLabeling

use of net.imglib2.roi.labeling.ImgLabeling 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 4 with ImgLabeling

use of net.imglib2.roi.labeling.ImgLabeling in project imagej-ops by imagej.

the class WatershedSeededTest method test.

@Test
public void test() {
    long[] dims = { 15, 30 };
    // create input image
    Img<FloatType> input = ArrayImgs.floats(dims);
    Random random = new Random();
    for (FloatType b : input) {
        b.setReal(random.nextDouble());
    }
    // create 3 seeds
    Img<BitType> bits = ArrayImgs.bits(dims);
    RandomAccess<BitType> ra = bits.randomAccess();
    ra.setPosition(new int[] { 0, 0 });
    ra.get().set(true);
    ra.setPosition(new int[] { 4, 6 });
    ra.get().set(true);
    ra.setPosition(new int[] { 10, 20 });
    ra.get().set(true);
    // compute labeled seeds
    final ImgLabeling<Integer, IntType> labeledSeeds = ops.labeling().cca(bits, StructuringElement.EIGHT_CONNECTED);
    testWithoutMask(input, labeledSeeds);
    testWithMask(input, labeledSeeds);
}
Also used : Random(java.util.Random) BitType(net.imglib2.type.logic.BitType) FloatType(net.imglib2.type.numeric.real.FloatType) IntType(net.imglib2.type.numeric.integer.IntType) AbstractOpTest(net.imagej.ops.AbstractOpTest) Test(org.junit.Test)

Example 5 with ImgLabeling

use of net.imglib2.roi.labeling.ImgLabeling 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

IntType (net.imglib2.type.numeric.integer.IntType)11 ImgLabeling (net.imglib2.roi.labeling.ImgLabeling)8 BitType (net.imglib2.type.logic.BitType)8 AbstractOpTest (net.imagej.ops.AbstractOpTest)5 FloatType (net.imglib2.type.numeric.real.FloatType)5 Test (org.junit.Test)5 Random (java.util.Random)4 LabelingType (net.imglib2.roi.labeling.LabelingType)3 ArrayList (java.util.ArrayList)2 RandomAccessibleInterval (net.imglib2.RandomAccessibleInterval)2 DiamondShape (net.imglib2.algorithm.neighborhood.DiamondShape)2 Neighborhood (net.imglib2.algorithm.neighborhood.Neighborhood)2 RectangleShape (net.imglib2.algorithm.neighborhood.RectangleShape)2 Shape (net.imglib2.algorithm.neighborhood.Shape)2 ExtendedRandomAccessibleInterval (net.imglib2.view.ExtendedRandomAccessibleInterval)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