Search in sources :

Example 1 with Label

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

the class TrainableSegmentationSegmenter method getClassIndices.

private SparseRandomAccessIntType getClassIndices(Labeling labeling, List<String> classes) {
    SparseRandomAccessIntType result = new SparseRandomAccessIntType(labeling, -1);
    Map<Set<Label>, Integer> classIndices = new HashMap<>();
    Function<Set<Label>, Integer> compute = set -> set.stream().mapToInt(label -> classes.indexOf(label.name())).filter(i -> i >= 0).min().orElse(-1);
    Cursor<?> cursor = labeling.sparsityCursor();
    RandomAccess<LabelingType<Label>> randomAccess = labeling.randomAccess();
    RandomAccess<IntType> out = result.randomAccess();
    while (cursor.hasNext()) {
        cursor.fwd();
        randomAccess.setPosition(cursor);
        Set<Label> labels = randomAccess.get();
        if (labels.isEmpty())
            continue;
        Integer classIndex = classIndices.computeIfAbsent(labels, compute);
        out.setPosition(cursor);
        out.get().set(classIndex);
    }
    return result;
}
Also used : Pair(net.imglib2.util.Pair) GsonUtils(sc.fiji.labkit.pixel_classification.gson.GsonUtils) WekaException(weka.core.WekaException) Context(org.scijava.Context) FloatType(net.imglib2.type.numeric.real.FloatType) DiskCachedCellImgOptions(net.imglib2.cache.img.DiskCachedCellImgOptions) Cursor(net.imglib2.Cursor) SparseRandomAccessIntType(sc.fiji.labkit.ui.utils.sparse.SparseRandomAccessIntType) SingleFeatures(sc.fiji.labkit.pixel_classification.pixel_feature.filter.SingleFeatures) FeatureSettings(sc.fiji.labkit.pixel_classification.pixel_feature.settings.FeatureSettings) Map(java.util.Map) DiskCachedCellImgFactory(net.imglib2.cache.img.DiskCachedCellImgFactory) Views(net.imglib2.view.Views) RandomAccess(net.imglib2.RandomAccess) CancellationException(java.util.concurrent.CancellationException) Set(java.util.Set) Collectors(java.util.stream.Collectors) PrefService(org.scijava.prefs.PrefService) Objects(java.util.Objects) DiskCachedCellImg(net.imglib2.cache.img.DiskCachedCellImg) List(java.util.List) Labelings(sc.fiji.labkit.ui.labeling.Labelings) LabelingType(net.imglib2.roi.labeling.LabelingType) Cast(net.imglib2.util.Cast) RealType(net.imglib2.type.numeric.RealType) ImgPlus(net.imagej.ImgPlus) ARGBType(net.imglib2.type.numeric.ARGBType) IntegerType(net.imglib2.type.numeric.IntegerType) Axes(net.imagej.axis.Axes) Composite(net.imglib2.view.composite.Composite) HashMap(java.util.HashMap) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Intervals(net.imglib2.util.Intervals) RandomAccessibleInterval(net.imglib2.RandomAccessibleInterval) Labeling(sc.fiji.labkit.ui.labeling.Labeling) LabkitUtils(sc.fiji.labkit.ui.utils.LabkitUtils) FastRandomForest(hr.irb.fastRandomForest.FastRandomForest) BiConsumer(java.util.function.BiConsumer) AxisType(net.imagej.axis.AxisType) Label(sc.fiji.labkit.ui.labeling.Label) CellLoader(net.imglib2.cache.img.CellLoader) Training(sc.fiji.labkit.pixel_classification.classification.Training) Segmenter(sc.fiji.labkit.ui.segmentation.Segmenter) GlobalSettings(sc.fiji.labkit.pixel_classification.pixel_feature.settings.GlobalSettings) FeatureCalculator(sc.fiji.labkit.pixel_classification.pixel_feature.calculator.FeatureCalculator) GroupedFeatures(sc.fiji.labkit.pixel_classification.pixel_feature.filter.GroupedFeatures) IntType(net.imglib2.type.numeric.integer.IntType) ChannelSetting(sc.fiji.labkit.pixel_classification.pixel_feature.settings.ChannelSetting) RandomAccessible(net.imglib2.RandomAccessible) ImgPlusViews(net.imglib2.img.display.imagej.ImgPlusViews) ImgPlusViewsOld(sc.fiji.labkit.ui.inputimage.ImgPlusViewsOld) javax.swing(javax.swing) Set(java.util.Set) HashMap(java.util.HashMap) Label(sc.fiji.labkit.ui.labeling.Label) SparseRandomAccessIntType(sc.fiji.labkit.ui.utils.sparse.SparseRandomAccessIntType) IntType(net.imglib2.type.numeric.integer.IntType) SparseRandomAccessIntType(sc.fiji.labkit.ui.utils.sparse.SparseRandomAccessIntType) LabelingType(net.imglib2.roi.labeling.LabelingType)

Example 2 with Label

use of sc.fiji.labkit.ui.labeling.Label 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 3 with Label

use of sc.fiji.labkit.ui.labeling.Label 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)

Example 4 with Label

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

the class BitmapImportExportAction method exportLabel.

private void exportLabel(Void ignore, String filename) throws IOException {
    Label label = model.selectedLabel().get();
    exportLabel(label, filename);
}
Also used : Label(sc.fiji.labkit.ui.labeling.Label)

Example 5 with Label

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

the class SegmentationAsLabelAction method addLabel.

// TODO move to better place
private static void addLabel(Labeling labeling, String name, RandomAccessibleInterval<BitType> mask) {
    Cursor<BitType> cursor = Views.iterable(mask).cursor();
    Label label = labeling.addLabel(name);
    RandomAccess<LabelingType<Label>> ra = labeling.randomAccess();
    while (cursor.hasNext()) {
        boolean value = cursor.next().get();
        if (value) {
            ra.setPosition(cursor);
            ra.get().add(label);
        }
    }
}
Also used : BitType(net.imglib2.type.logic.BitType) Label(sc.fiji.labkit.ui.labeling.Label) LabelingType(net.imglib2.roi.labeling.LabelingType)

Aggregations

Label (sc.fiji.labkit.ui.labeling.Label)15 Labeling (sc.fiji.labkit.ui.labeling.Labeling)6 LabelingType (net.imglib2.roi.labeling.LabelingType)5 BitType (net.imglib2.type.logic.BitType)5 Cursor (net.imglib2.Cursor)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Set (java.util.Set)2 Function (java.util.function.Function)2 Collectors (java.util.stream.Collectors)2 ImgPlus (net.imagej.ImgPlus)2 Interval (net.imglib2.Interval)2 Point (net.imglib2.Point)2 RandomAccessibleInterval (net.imglib2.RandomAccessibleInterval)2 RealPoint (net.imglib2.RealPoint)2 IterableRegion (net.imglib2.roi.IterableRegion)2 ARGBType (net.imglib2.type.numeric.ARGBType)2 Context (org.scijava.Context)2 FastRandomForest (hr.irb.fastRandomForest.FastRandomForest)1 File (java.io.File)1