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);
}
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);
}
}
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);
}
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);
}
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);
}
Aggregations