From d607eb3516c861d80f331ca5c57957f340e2b8c8 Mon Sep 17 00:00:00 2001 From: Matthias Arzt Date: Wed, 10 Oct 2018 16:50:12 +0200 Subject: [PATCH] Update to parent pom-indago-2.2.11 because for new labkit version 0.1.14 --- pom.xml | 2 +- .../indago/tr2d/plugins/seg/LabkitPanel.java | 2 +- .../tr2d/plugins/seg/MySegmentationItem.java | 3 +- .../tr2d/plugins/seg/PredictionLayer.java | 9 ++-- .../plugins/seg/SegmentationComponent.java | 52 ++++++------------- .../tr2d/plugins/seg/SegmentationModel.java | 10 ++-- .../tr2d/plugins/seg/ThresholdButton.java | 2 +- .../seg/SegmentationComponentDemo.java | 6 +-- .../plugins/seg/SegmentationModelTest.java | 35 +++++++++---- 9 files changed, 58 insertions(+), 63 deletions(-) diff --git a/pom.xml b/pom.xml index 5f0d38d..1810deb 100755 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ sc.fiji pom-indago - 2.2.10 + 2.2.11 diff --git a/src/main/java/com/indago/tr2d/plugins/seg/LabkitPanel.java b/src/main/java/com/indago/tr2d/plugins/seg/LabkitPanel.java index 2895a8d..3acc59f 100644 --- a/src/main/java/com/indago/tr2d/plugins/seg/LabkitPanel.java +++ b/src/main/java/com/indago/tr2d/plugins/seg/LabkitPanel.java @@ -65,7 +65,7 @@ public boolean isUsable() { public List> getOutputs() { if(!isUsable()) return Collections.emptyList(); List< RandomAccessibleInterval< IntType > > segmentations = - segmentation.getSegmentations(); + segmentationModel.getSegmentations(); saveSettings(); return segmentations; } diff --git a/src/main/java/com/indago/tr2d/plugins/seg/MySegmentationItem.java b/src/main/java/com/indago/tr2d/plugins/seg/MySegmentationItem.java index 255d403..57c5569 100644 --- a/src/main/java/com/indago/tr2d/plugins/seg/MySegmentationItem.java +++ b/src/main/java/com/indago/tr2d/plugins/seg/MySegmentationItem.java @@ -57,8 +57,7 @@ public MySegmentationItem(SegmentationModel model, Segmenter segmenter) { } private static Labeling defaultLabeling(SegmentationModel model) { - return new Labeling(Arrays.asList("background", "foreground"), model - .image()); + return Labeling.createEmpty(Arrays.asList("background", "foreground"), model.image()); } private void resetProbability() { diff --git a/src/main/java/com/indago/tr2d/plugins/seg/PredictionLayer.java b/src/main/java/com/indago/tr2d/plugins/seg/PredictionLayer.java index b81580f..ebb922f 100644 --- a/src/main/java/com/indago/tr2d/plugins/seg/PredictionLayer.java +++ b/src/main/java/com/indago/tr2d/plugins/seg/PredictionLayer.java @@ -35,6 +35,7 @@ public class PredictionLayer implements BdvLayer { private final RandomAccessibleContainer segmentationContainer; private final SharedQueue queue = new SharedQueue(Runtime.getRuntime() .availableProcessors()); + private final Holder< Boolean > visibility; private Notifier listeners = new Notifier<>(); private Notifier makeVisible = new Notifier<>(); private RandomAccessibleInterval> view; @@ -42,13 +43,15 @@ public class PredictionLayer implements BdvLayer { private Set alreadyRegistered = Collections.newSetFromMap( new WeakHashMap<>()); - public PredictionLayer(Holder model) { + public PredictionLayer(Holder< ? extends MySegmentationItem > model, + Holder< Boolean > visibility) { this.model = model; SegmentationResultsModel selected = model.get().results(); this.segmentationContainer = new RandomAccessibleContainer<>( getEmptyPrediction(selected)); this.transformation = selected.transformation(); this.view = Views.interval(segmentationContainer, selected.interval()); + this.visibility = visibility; classifierChanged(); model.notifier().add(ignore -> classifierChanged()); registerListener(model.get()); @@ -147,7 +150,7 @@ public String title() { } @Override - public Notifier< Runnable > makeVisible() { - return makeVisible; + public Holder< Boolean > visibility() { + return visibility; } } diff --git a/src/main/java/com/indago/tr2d/plugins/seg/SegmentationComponent.java b/src/main/java/com/indago/tr2d/plugins/seg/SegmentationComponent.java index 62711bf..63f3bf5 100644 --- a/src/main/java/com/indago/tr2d/plugins/seg/SegmentationComponent.java +++ b/src/main/java/com/indago/tr2d/plugins/seg/SegmentationComponent.java @@ -1,68 +1,57 @@ package com.indago.tr2d.plugins.seg; -import net.imglib2.RandomAccessibleInterval; -import net.imglib2.labkit.DefaultExtensible; -import net.imglib2.labkit.Extensible; import net.imglib2.labkit.BasicLabelingComponent; -import net.imglib2.labkit.actions.SelectClassifier; +import net.imglib2.labkit.DefaultExtensible; +import net.imglib2.labkit.actions.ClassifierSettingsAction; +import net.imglib2.labkit.actions.LabelEditAction; import net.imglib2.labkit.models.ColoredLabelsModel; -import net.imglib2.labkit.panel.GuiUtils; +import net.imglib2.labkit.panel.ImageInfoPanel; import net.imglib2.labkit.panel.LabelPanel; import net.imglib2.labkit.panel.SegmenterPanel; import net.imglib2.labkit.segmentation.TrainClassifier; -import net.imglib2.type.numeric.NumericType; -import net.imglib2.type.numeric.integer.IntType; import net.miginfocom.swing.MigLayout; -import org.scijava.Context; import javax.swing.*; -import java.util.List; public class SegmentationComponent implements AutoCloseable { private final JSplitPane panel; - private final JFrame dialogBoxOwner = null; - private final BasicLabelingComponent labelingComponent; - private final Context context; - private final SegmentationModel segmentationModel; + private final DefaultExtensible extensible; + public SegmentationComponent(SegmentationModel segmentationModel) { this.segmentationModel = segmentationModel; - this.context = segmentationModel.getContext(); + JFrame dialogBoxOwner = null; + this.extensible = new DefaultExtensible(segmentationModel.getContext(), + dialogBoxOwner); labelingComponent = new BasicLabelingComponent(dialogBoxOwner, segmentationModel.imageLabelingModel()); labelingComponent.addBdvLayer(new PredictionLayer(segmentationModel - .selectedSegmenter())); + .selectedSegmenter(), segmentationModel.segmentationVisibility())); initActions(); JPanel leftPanel = initLeftPanel(); this.panel = initPanel(leftPanel, labelingComponent.getComponent()); } private void initActions() { - Extensible extensible = new DefaultExtensible(context, dialogBoxOwner, - labelingComponent); new TrainClassifier(extensible, segmentationModel); - new SelectClassifier(extensible, segmentationModel.selectedSegmenter()); + new LabelEditAction(extensible, true, new ColoredLabelsModel(segmentationModel.imageLabelingModel())); + new ClassifierSettingsAction(extensible, segmentationModel.selectedSegmenter()); + labelingComponent.addShortcuts(extensible.getShortCuts()); } private JPanel initLeftPanel() { JPanel panel = new JPanel(); panel.setLayout(new MigLayout("", "[grow]", "[][grow][grow][]")); - ActionMap actions = getActions(); - panel.add(GuiUtils.createCheckboxGroupedPanel(actions.get("Image"), GuiUtils - .createDimensionsInfo(segmentationModel.image())), "grow, wrap"); - panel.add(GuiUtils.createCheckboxGroupedPanel(actions.get("Labeling"), - new LabelPanel(dialogBoxOwner, new ColoredLabelsModel(segmentationModel - .imageLabelingModel()), true).getComponent()), "grow, wrap"); - panel.add(GuiUtils.createCheckboxGroupedPanel(actions.get("Segmentation"), - new SegmenterPanel(segmentationModel).getComponent()), - "grow, wrap"); + panel.add(ImageInfoPanel.newFramedImageInfoPanel(segmentationModel.imageLabelingModel()), "grow, wrap"); + panel.add(LabelPanel.newFramedLabelPanel(segmentationModel.imageLabelingModel(), extensible, true), "grow, wrap"); + panel.add(SegmenterPanel.newFramedSegmeterPanel(segmentationModel, extensible), "grow, wrap"); panel.add(new ThresholdButton(segmentationModel).getComponent(), "grow"); return panel; } @@ -80,15 +69,6 @@ public JComponent getComponent() { return panel; } - private ActionMap getActions() { - return labelingComponent.getActions(); - } - - public List> getSegmentations() - { - return segmentationModel.getSegmentations(); - } - @Override public void close() { labelingComponent.close(); diff --git a/src/main/java/com/indago/tr2d/plugins/seg/SegmentationModel.java b/src/main/java/com/indago/tr2d/plugins/seg/SegmentationModel.java index 57bf31f..f717d89 100644 --- a/src/main/java/com/indago/tr2d/plugins/seg/SegmentationModel.java +++ b/src/main/java/com/indago/tr2d/plugins/seg/SegmentationModel.java @@ -4,7 +4,6 @@ import com.indago.io.ProjectFolder; import net.imglib2.RandomAccessibleInterval; import net.imglib2.img.cell.CellGrid; -import net.imglib2.labkit.color.ColorMap; import net.imglib2.labkit.inputimage.DefaultInputImage; import net.imglib2.labkit.inputimage.InputImage; import net.imglib2.labkit.labeling.Labeling; @@ -41,6 +40,7 @@ public class SegmentationModel implements private List segmenters = new ArrayList<>(); private final RandomAccessibleInterval> compatibleImage; private final CellGrid grid; + private final Holder segmentationVisibility = new DefaultHolder<>(true); private Context context; @@ -97,11 +97,6 @@ public Holder selectedSegmenter() { return selectedSegmenter; } - @Override - public ColorMap colorMap() { - return imageLabelingModel.colorMapProvider().colorMap(); - } - @Override public AffineTransform3D labelTransformation() { return imageLabelingModel.labelTransformation(); @@ -210,4 +205,7 @@ public void save(ProjectFolder folder) throws IOException { } } + public Holder segmentationVisibility() { + return segmentationVisibility; + } } diff --git a/src/main/java/com/indago/tr2d/plugins/seg/ThresholdButton.java b/src/main/java/com/indago/tr2d/plugins/seg/ThresholdButton.java index 3554476..8ed4d2f 100644 --- a/src/main/java/com/indago/tr2d/plugins/seg/ThresholdButton.java +++ b/src/main/java/com/indago/tr2d/plugins/seg/ThresholdButton.java @@ -76,7 +76,7 @@ public static void main(String... args) { ArrayImgs.unsignedBytes(100, 100, 100), new Context()); JFrame frame = new JFrame(); - frame.add(new SegmenterPanel(segmentationModel) + frame.add(new SegmenterPanel(segmentationModel, ignore -> new JPopupMenu()) .getComponent()); frame.add(new ThresholdButton(segmentationModel).getComponent(), BorderLayout.PAGE_END); diff --git a/src/test/java/com/indago/tr2d/plugins/seg/SegmentationComponentDemo.java b/src/test/java/com/indago/tr2d/plugins/seg/SegmentationComponentDemo.java index c3d5d25..6132083 100644 --- a/src/test/java/com/indago/tr2d/plugins/seg/SegmentationComponentDemo.java +++ b/src/test/java/com/indago/tr2d/plugins/seg/SegmentationComponentDemo.java @@ -18,6 +18,7 @@ public class SegmentationComponentDemo { private final SegmentationComponent segmenter; + private final SegmentationModel model; public static void main(String... args) { new SegmentationComponentDemo(); @@ -28,8 +29,7 @@ private SegmentationComponentDemo() { Img> image = ImageJFunctions.wrap(new ImagePlus( "/home/arzt/Documents/Notes/Tr2d/ProjectFiles/raw.tif")); Context context = new Context(); - SegmentationModel model = - new SegmentationModel(image, context); + model = new SegmentationModel(image, context); segmenter = new SegmentationComponent(model); frame.add(segmenter.getComponent()); frame.add(getBottomPanel(), BorderLayout.PAGE_END); @@ -46,7 +46,7 @@ private JPanel getBottomPanel() { } private void showSegmentation() { - for (RandomAccessibleInterval segmentation : segmenter + for (RandomAccessibleInterval segmentation : model .getSegmentations()) { Views.iterable(segmentation).forEach(x -> x.mul(50)); diff --git a/src/test/java/com/indago/tr2d/plugins/seg/SegmentationModelTest.java b/src/test/java/com/indago/tr2d/plugins/seg/SegmentationModelTest.java index d176892..41c7888 100644 --- a/src/test/java/com/indago/tr2d/plugins/seg/SegmentationModelTest.java +++ b/src/test/java/com/indago/tr2d/plugins/seg/SegmentationModelTest.java @@ -8,6 +8,8 @@ import net.imglib2.img.array.ArrayImg; import net.imglib2.img.array.ArrayImgs; import net.imglib2.img.basictypeaccess.array.ByteArray; +import net.imglib2.labkit.labeling.Label; +import net.imglib2.labkit.labeling.Labeling; import net.imglib2.type.numeric.integer.ByteType; import net.imglib2.type.numeric.integer.IntType; import org.junit.Test; @@ -18,6 +20,8 @@ import java.nio.file.Path; import java.util.Collections; import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; import static junit.framework.TestCase.assertFalse; import static org.junit.Assert.assertEquals; @@ -35,13 +39,18 @@ public void testSerializeSingleLabeling() throws IOException { // setup SegmentationModel model = new SegmentationModel(image, context); ProjectFolder projectFolder = tmpProjectFolder(); - getPixel(model.labeling(), 0,0,0).add("foreground"); + addPixel(model.labeling(), "foreground", 0, 0, 0); // process model.save(projectFolder); SegmentationModel model2 = SegmentationModel .open(image, context, projectFolder); // test - assertEquals(Collections.singleton("foreground"), getPixel(model2.labeling(), 0, 0, 0)); + assertEquals(Collections.singleton("foreground"), getPixelNames(model2.labeling(), 0, 0, 0)); + } + + private Set getPixelNames(Labeling labeling, long... position) { + return getPixel(labeling, position).stream().map(Label::name).collect( + Collectors.toSet()); } @Test @@ -64,14 +73,14 @@ public void testSerializeSingleLabeling2() throws IOException { assertSame(model.labeling(), model.segmenters().get(0).labeling()); ProjectFolder projectFolder = tmpProjectFolder(); model.addSegmenter(); - getPixel(model.segmenters().get(0).labeling(), 0,0,0).add("foreground"); + addPixel(model.segmenters().get(0).labeling(), "foreground", 0, 0, 0); // process model.save(projectFolder); SegmentationModel model2 = SegmentationModel .open(image, context, projectFolder); // test assertSame(model2.labeling(), model2.segmenters().get(0).labeling()); - assertEquals(Collections.singleton("foreground"), getPixel(model2.segmenters().get(0).labeling(), 0, 0, 0)); + assertEquals(Collections.singleton("foreground"), getPixelNames(model2.segmenters().get(0).labeling(), 0, 0, 0)); } @Test @@ -81,15 +90,20 @@ public void testSerializeMultipleLabelings() throws IOException { ProjectFolder projectFolder = tmpProjectFolder(); model.addSegmenter(); List< MySegmentationItem > segmenters = model.segmenters(); - getPixel(segmenters.get(0).labeling(), 0, 0, 0).add("foreground"); - getPixel(segmenters.get(1).labeling(), 0, 0, 0).add("background"); + addPixel(segmenters.get(0).labeling(), "foreground", 0, 0, 0); + addPixel(segmenters.get(1).labeling(), "background", 0, 0, 0); // process model.save(projectFolder); SegmentationModel model2 = SegmentationModel .open(image, context, projectFolder); // test - assertEquals(Collections.singleton("foreground"), getPixel(model2.segmenters().get(0).labeling(), 0, 0, 0)); - assertEquals(Collections.singleton("background"), getPixel(model2.segmenters().get(1).labeling(), 0, 0, 0)); + assertEquals(Collections.singleton("foreground"), getPixelNames(model2.segmenters().get(0).labeling(), 0, 0, 0)); + assertEquals(Collections.singleton("background"), getPixelNames(model2.segmenters().get(1).labeling(), 0, 0, 0)); + } + + private void addPixel(Labeling labeling, String foreground, long... position) + { + getPixel(labeling, position).add(labeling.getLabel(foreground)); } @Test @@ -139,8 +153,9 @@ public void testSerialize() throws IOException { } private void train(MySegmentationItem item) { - getPixel(item.labeling(),0,0,0).add("foreground"); - getPixel(item.labeling(),1,1,1).add("background"); + final Labeling labeling = item.labeling(); + addPixel(labeling, "foreground", 0, 0, 0); + addPixel(labeling, "background", 1, 1, 1); item.train(); }