Search in sources :

Example 1 with SparseRandomAccessIntType

use of sc.fiji.labkit.ui.utils.sparse.SparseRandomAccessIntType 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 SparseRandomAccessIntType

use of sc.fiji.labkit.ui.utils.sparse.SparseRandomAccessIntType in project labkit-ui by juglab.

the class MeasureConnectedComponents method connectedComponetsSizes.

static List<Long> connectedComponetsSizes(IterableRegion<BitType> region) {
    List<Long> sizes = new ArrayList<>();
    Cursor<Void> cursor = region.cursor();
    SparseRandomAccessIntType visitedImage = new SparseRandomAccessIntType(region);
    RandomAccess<IntType> visited = visitedImage.randomAccess();
    int currentIndex = 0;
    while (cursor.hasNext()) {
        cursor.fwd();
        visited.setPosition(cursor);
        if (visited.get().get() == 0) {
            currentIndex++;
            long countBefore = visitedImage.sparsityPattern().size();
            Filter<Pair<BitType, IntType>, Pair<BitType, IntType>> filter = (current, seed) -> current.getA().get() && current.getB().get() == 0;
            FloodFill.fill(region, visitedImage, cursor, new IntType(currentIndex), new DiamondShape(1), filter);
            long countAfter = visitedImage.sparsityPattern().size();
            sizes.add(countAfter - countBefore);
        }
    }
    return sizes;
}
Also used : DiamondShape(net.imglib2.algorithm.neighborhood.DiamondShape) IterableRegion(net.imglib2.roi.IterableRegion) LinearAxis(net.imagej.axis.LinearAxis) Pair(net.imglib2.util.Pair) LabelingModel(sc.fiji.labkit.ui.models.LabelingModel) GenericTable(org.scijava.table.GenericTable) DefaultColumn(org.scijava.table.DefaultColumn) Context(org.scijava.Context) ArrayList(java.util.ArrayList) DoubleColumn(org.scijava.table.DoubleColumn) Cursor(net.imglib2.Cursor) MenuBar(sc.fiji.labkit.ui.MenuBar) DefaultGenericTable(org.scijava.table.DefaultGenericTable) Labeling(sc.fiji.labkit.ui.labeling.Labeling) SparseRandomAccessIntType(sc.fiji.labkit.ui.utils.sparse.SparseRandomAccessIntType) FloodFill(net.imglib2.algorithm.fill.FloodFill) Extensible(sc.fiji.labkit.ui.Extensible) CalibratedAxis(net.imagej.axis.CalibratedAxis) Filter(net.imglib2.algorithm.fill.Filter) BitType(net.imglib2.type.logic.BitType) RandomAccess(net.imglib2.RandomAccess) ItemIO(org.scijava.ItemIO) Parameter(org.scijava.plugin.Parameter) IOException(java.io.IOException) IntType(net.imglib2.type.numeric.integer.IntType) UIService(org.scijava.ui.UIService) File(java.io.File) Command(org.scijava.command.Command) Plugin(org.scijava.plugin.Plugin) List(java.util.List) StringJoiner(java.util.StringJoiner) LabelingSerializer(sc.fiji.labkit.ui.labeling.LabelingSerializer) Table(org.scijava.table.Table) Column(org.scijava.table.Column) DiamondShape(net.imglib2.algorithm.neighborhood.DiamondShape) ArrayList(java.util.ArrayList) SparseRandomAccessIntType(sc.fiji.labkit.ui.utils.sparse.SparseRandomAccessIntType) IntType(net.imglib2.type.numeric.integer.IntType) SparseRandomAccessIntType(sc.fiji.labkit.ui.utils.sparse.SparseRandomAccessIntType) Pair(net.imglib2.util.Pair)

Example 3 with SparseRandomAccessIntType

use of sc.fiji.labkit.ui.utils.sparse.SparseRandomAccessIntType in project labkit-ui by juglab.

the class TrainableSegmentationSegmenter method trainFrame.

private void trainFrame(Training training, List<String> classes, Labeling labeling, ImgPlus<?> image, FeatureCalculator featuresCalculator) {
    SparseRandomAccessIntType classIndices = getClassIndices(labeling, classes);
    if (classIndices.sparsityPattern().size() == 0)
        return;
    DiskCachedCellImg<FloatType, ?> cachedFeatureBlock = cachedFeatureBlock(featuresCalculator, image);
    try {
        RandomAccessible<? extends Composite<FloatType>> features = Views.collapse(cachedFeatureBlock);
        addSamples(training, classIndices, features);
    } finally {
        cachedFeatureBlock.shutdown();
    }
}
Also used : SparseRandomAccessIntType(sc.fiji.labkit.ui.utils.sparse.SparseRandomAccessIntType) FloatType(net.imglib2.type.numeric.real.FloatType)

Example 4 with SparseRandomAccessIntType

use of sc.fiji.labkit.ui.utils.sparse.SparseRandomAccessIntType in project labkit-ui by juglab.

the class Labeling method initImgLabling.

private static ImgLabeling<Label, ?> initImgLabling(Map<Label, IterableRegion<BitType>> regions) {
    Interval interval = getInterval(regions.values());
    ImgLabeling<Label, ?> imgLabeling = new ImgLabeling<>(new SparseRandomAccessIntType(interval));
    RandomAccess<LabelingType<Label>> ra = imgLabeling.randomAccess();
    regions.forEach((label, region) -> {
        Cursor<Void> cursor = region.cursor();
        while (cursor.hasNext()) {
            cursor.fwd();
            ra.setPosition(cursor);
            ra.get().add(label);
        }
    });
    return imgLabeling;
}
Also used : SparseRandomAccessIntType(sc.fiji.labkit.ui.utils.sparse.SparseRandomAccessIntType) ImgLabeling(net.imglib2.roi.labeling.ImgLabeling) LabelingType(net.imglib2.roi.labeling.LabelingType)

Aggregations

SparseRandomAccessIntType (sc.fiji.labkit.ui.utils.sparse.SparseRandomAccessIntType)4 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Cursor (net.imglib2.Cursor)2 RandomAccess (net.imglib2.RandomAccess)2 LabelingType (net.imglib2.roi.labeling.LabelingType)2 IntType (net.imglib2.type.numeric.integer.IntType)2 Pair (net.imglib2.util.Pair)2 Context (org.scijava.Context)2 Labeling (sc.fiji.labkit.ui.labeling.Labeling)2 FastRandomForest (hr.irb.fastRandomForest.FastRandomForest)1 File (java.io.File)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Set (java.util.Set)1 StringJoiner (java.util.StringJoiner)1 CancellationException (java.util.concurrent.CancellationException)1 BiConsumer (java.util.function.BiConsumer)1