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