Search in sources :

Example 6 with Labeling

use of sc.fiji.labkit.ui.labeling.Labeling in project labkit-ui by juglab.

the class FloodFillTest method test2.

@Test
public void test2() {
    RandomAccessibleInterval<LabelingType<String>> labeling = exampleImgLabeling();
    Predicate<LabelingType<String>> visit = set -> set.contains("a") && set.contains("b") && !set.contains("ab");
    final Point seed = new Point(2, 2);
    FloodFill.cachedFloodFill(labeling, seed, visit, l -> l.add("ab"));
    assertLabelEqualsInterval(labeling, intervalA, "a");
    assertLabelEqualsInterval(labeling, intervalB, "b");
    assertLabelEqualsInterval(labeling, intervalC, "c");
    assertLabelEqualsInterval(labeling, intervalAintersectB, "ab");
}
Also used : BitType(net.imglib2.type.logic.BitType) Point(net.imglib2.Point) Predicate(java.util.function.Predicate) Set(java.util.Set) Test(org.junit.Test) Consumer(java.util.function.Consumer) Intervals(net.imglib2.util.Intervals) Cursor(net.imglib2.Cursor) RandomAccessibleInterval(net.imglib2.RandomAccessibleInterval) Labeling(sc.fiji.labkit.ui.labeling.Labeling) LabelingType(net.imglib2.roi.labeling.LabelingType) ArrayImgs(net.imglib2.img.array.ArrayImgs) ImgLabeling(net.imglib2.roi.labeling.ImgLabeling) Interval(net.imglib2.Interval) Label(sc.fiji.labkit.ui.labeling.Label) Views(net.imglib2.view.Views) Assert.assertEquals(org.junit.Assert.assertEquals) Point(net.imglib2.Point) LabelingType(net.imglib2.roi.labeling.LabelingType) Test(org.junit.Test)

Example 7 with Labeling

use of sc.fiji.labkit.ui.labeling.Labeling in project labkit-ui by juglab.

the class CustomSegmenter method train.

/**
 * Calculates the mean intensity value for all foreground pixel, and the mean
 * intensity value for all background pixel.
 */
@Override
public void train(List<Pair<ImgPlus<?>, Labeling>> data) {
    MeanCalculator foregroundMean = new MeanCalculator();
    MeanCalculator backgroundMean = new MeanCalculator();
    for (Pair<ImgPlus<?>, Labeling> imageAndLabeling : data) {
        RandomAccessibleInterval<? extends RealType<?>> image = grayScale(imageAndLabeling.getA());
        Labeling labeling = imageAndLabeling.getB();
        Map<Label, IterableRegion<BitType>> regions = labeling.iterableRegions();
        for (Label label : regions.keySet()) {
            String name = label.name();
            IterableRegion<BitType> region = regions.get(label);
            switch(name) {
                case "foreground":
                    foregroundMean.addSample(image, region);
                    break;
                case "background":
                    backgroundMean.addSample(image, region);
                    break;
                default:
                    throw new RuntimeException("Unsupported label name \"" + name + "\"\n" + "Threshold expects the labels to be named \"foreground\" and \"background\".");
            }
        }
    }
    this.foreground = foregroundMean.mean();
    this.background = backgroundMean.mean();
}
Also used : ImgPlus(net.imagej.ImgPlus) IterableRegion(net.imglib2.roi.IterableRegion) BitType(net.imglib2.type.logic.BitType) Label(sc.fiji.labkit.ui.labeling.Label) Labeling(sc.fiji.labkit.ui.labeling.Labeling)

Example 8 with Labeling

use of sc.fiji.labkit.ui.labeling.Labeling in project labkit-ui by juglab.

the class InitialLabelingTest method testInitialLabeling.

@Test
public void testInitialLabeling() throws IOException {
    File empty = File.createTempFile("labkit-InitialLabelingTest-", ".czi.labeling");
    try {
        ImgPlus<UnsignedByteType> image = ImgPlus.wrap(ArrayImgs.unsignedBytes(2, 3));
        image.setSource(empty.getAbsolutePath().replace(".labeling", ""));
        DatasetInputImage inputImage = new DatasetInputImage(image);
        Context context = new Context();
        List<String> defaultLabels = Collections.emptyList();
        Labeling result = InitialLabeling.initLabeling(inputImage, context, defaultLabels);
        assertNotNull(result);
    } finally {
        empty.delete();
    }
}
Also used : Context(org.scijava.Context) DatasetInputImage(sc.fiji.labkit.ui.inputimage.DatasetInputImage) UnsignedByteType(net.imglib2.type.numeric.integer.UnsignedByteType) Labeling(sc.fiji.labkit.ui.labeling.Labeling) File(java.io.File) Test(org.junit.Test)

Example 9 with Labeling

use of sc.fiji.labkit.ui.labeling.Labeling in project labkit-ui by juglab.

the class TrainableSegmentationSegmenterTest method testClassify3DStackInSlices.

@Test
public void testClassify3DStackInSlices() {
    Context context = SingletonContext.getInstance();
    TrainableSegmentationSegmenter segmenter = new TrainableSegmentationSegmenter(context);
    // Settings to 2D
    segmenter.setFeatureSettings(new FeatureSettings(GlobalSettings.default2d().build(), SingleFeatures.identity()));
    // Train on 3D image
    ImgPlus<?> image3d = new ImgPlus<>(ArrayImgs.ints(new int[] { 0, 1, 1, 0 }, 2, 1, 2), "name", new AxisType[] { Axes.X, Axes.Y, Axes.Z });
    Labeling labeling = initLabeling();
    segmenter.train(Collections.singletonList(new ValuePair<>(image3d, labeling)));
    // Segment 3D image
    Img<IntType> result = ArrayImgs.ints(2, 1, 2);
    segmenter.segment(image3d, result);
    ImgLib2Assert.assertImageEquals(image3d, result, Object::equals);
    Img<IntType> expected2d = ArrayImgs.ints(new int[] { 0, 1, 0 }, 1, 3);
    // Segment 2d image
    ImgPlus<IntType> image2d = new ImgPlus<>(expected2d, "");
    Img<IntType> result2d = ArrayImgs.ints(1, 3);
    segmenter.segment(image2d, result2d);
    ImgLib2Assert.assertImageEquals(image2d, result2d, Object::equals);
}
Also used : Context(org.scijava.Context) SingletonContext(sc.fiji.labkit.pixel_classification.utils.SingletonContext) FeatureSettings(sc.fiji.labkit.pixel_classification.pixel_feature.settings.FeatureSettings) ImgPlus(net.imagej.ImgPlus) ValuePair(net.imglib2.util.ValuePair) Labeling(sc.fiji.labkit.ui.labeling.Labeling) IntType(net.imglib2.type.numeric.integer.IntType) Test(org.junit.Test)

Example 10 with Labeling

use of sc.fiji.labkit.ui.labeling.Labeling in project labkit-ui by juglab.

the class DefaultSegmentationModelTest method testLoadClassifierWithDifferentLabels.

@Test
public void testLoadClassifierWithDifferentLabels() throws IOException {
    // This test reproduced a NoSuchElementException, that appeared when
    // a classifier is loaded, that was trained on a label, whose name
    // doesn't appear in the current labeling.
    // create model
    DatasetInputImage image = new DatasetInputImage(ArrayImgs.unsignedBytes(1, 1));
    SegmentationModel model = new DefaultSegmentationModel(new Context(), image);
    // train classifier
    Labeling labeling = model.imageLabelingModel().labeling().get();
    List<Label> labels = labeling.getLabels();
    Views.iterable(labeling.getRegion(labels.get(0))).forEach(BitType::setOne);
    SegmentationItem item = model.segmenterList().addSegmenter(PixelClassificationPlugin.create());
    item.train(Collections.singletonList(new ValuePair<>(image.imageForSegmentation(), labeling)));
    // save classifier
    File tmpFile = File.createTempFile("model", ".classifier");
    tmpFile.deleteOnExit();
    item.saveModel(tmpFile.getAbsolutePath());
    // remove labels
    for (Label label : labels) labeling.removeLabel(label);
    // open classifier
    item.openModel(tmpFile.getAbsolutePath());
}
Also used : Context(org.scijava.Context) DatasetInputImage(sc.fiji.labkit.ui.inputimage.DatasetInputImage) BitType(net.imglib2.type.logic.BitType) ValuePair(net.imglib2.util.ValuePair) Label(sc.fiji.labkit.ui.labeling.Label) Labeling(sc.fiji.labkit.ui.labeling.Labeling) File(java.io.File) Test(org.junit.Test)

Aggregations

Labeling (sc.fiji.labkit.ui.labeling.Labeling)26 LabelingType (net.imglib2.roi.labeling.LabelingType)8 Label (sc.fiji.labkit.ui.labeling.Label)8 FinalInterval (net.imglib2.FinalInterval)7 BitType (net.imglib2.type.logic.BitType)7 Test (org.junit.Test)7 Context (org.scijava.Context)7 ImgPlus (net.imagej.ImgPlus)6 Interval (net.imglib2.Interval)6 Cursor (net.imglib2.Cursor)5 RandomAccessibleInterval (net.imglib2.RandomAccessibleInterval)5 DatasetInputImage (sc.fiji.labkit.ui.inputimage.DatasetInputImage)5 File (java.io.File)4 List (java.util.List)4 Intervals (net.imglib2.util.Intervals)4 ValuePair (net.imglib2.util.ValuePair)4 Views (net.imglib2.view.Views)4 ArrayList (java.util.ArrayList)3 Set (java.util.Set)3 Collectors (java.util.stream.Collectors)3