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