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