diff --git a/src/main/java/sc/fiji/labkit/ui/plugin/CalculateProbabilityMapWithLabkitIJ1Plugin.java b/src/main/java/sc/fiji/labkit/ui/plugin/CalculateProbabilityMapWithLabkitIJ1Plugin.java new file mode 100644 index 00000000..cb4d55dd --- /dev/null +++ b/src/main/java/sc/fiji/labkit/ui/plugin/CalculateProbabilityMapWithLabkitIJ1Plugin.java @@ -0,0 +1,99 @@ +/*- + * #%L + * The Labkit image segmentation tool for Fiji. + * %% + * Copyright (C) 2017 - 2023 Matthias Arzt + * %% + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * #L% + */ + +package sc.fiji.labkit.ui.plugin; + +import ij.ImagePlus; +import net.imglib2.img.VirtualStackAdapter; +import net.imglib2.img.display.imagej.ImageJFunctions; +import org.scijava.Cancelable; +import org.scijava.Context; +import org.scijava.ItemIO; +import org.scijava.app.StatusService; +import org.scijava.command.Command; +import org.scijava.plugin.Parameter; +import org.scijava.plugin.Plugin; +import sc.fiji.labkit.ui.segmentation.SegmentationTool; +import sc.fiji.labkit.ui.utils.progress.StatusServiceProgressWriter; + +import java.io.File; + +/** + * @author Robert Haase + * @author Matthias Arzt + */ +@Plugin(type = Command.class, + menuPath = "Plugins > Labkit > Macro Recordable > Calculate Probability Map With Labkit (IJ1)") +public class CalculateProbabilityMapWithLabkitIJ1Plugin implements Command, Cancelable { + + @Parameter + private Context context; + + @Parameter + private StatusService statusService; + + @Parameter + private ImagePlus input; + + @Parameter + private File segmenter_file; + + @Parameter(type = ItemIO.OUTPUT) + private ImagePlus output; + + @Parameter(required = false) + private Boolean use_gpu = false; + + @Override + public void run() { + SegmentationTool segmenter = new SegmentationTool(); + segmenter.setContext(context); + segmenter.openModel(segmenter_file.getAbsolutePath()); + segmenter.setUseGpu(use_gpu); + segmenter.setProgressWriter(new StatusServiceProgressWriter(statusService)); + output = ImageJFunctions.wrap( + segmenter.probabilityMap(VirtualStackAdapter.wrap(input)), ""); + output.show(); + } + + @Override + public boolean isCanceled() { + return false; + } + + @Override + public void cancel(String reason) { + + } + + @Override + public String getCancelReason() { + return null; + } +} diff --git a/src/main/java/sc/fiji/labkit/ui/plugin/CalculateProbabilityMapWithLabkitPlugin.java b/src/main/java/sc/fiji/labkit/ui/plugin/CalculateProbabilityMapWithLabkitPlugin.java index 7b4f4dc9..aade6ef5 100644 --- a/src/main/java/sc/fiji/labkit/ui/plugin/CalculateProbabilityMapWithLabkitPlugin.java +++ b/src/main/java/sc/fiji/labkit/ui/plugin/CalculateProbabilityMapWithLabkitPlugin.java @@ -6,13 +6,13 @@ * %% * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: - * + * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -29,13 +29,8 @@ package sc.fiji.labkit.ui.plugin; -import bdv.export.ProgressWriterConsole; import net.imagej.Dataset; import net.imagej.DatasetService; -import net.imagej.ImgPlus; -import net.imglib2.img.Img; -import net.imglib2.type.numeric.real.FloatType; -import net.imglib2.util.Intervals; import org.scijava.Cancelable; import org.scijava.Context; import org.scijava.ItemIO; @@ -43,12 +38,7 @@ import org.scijava.command.Command; import org.scijava.plugin.Parameter; import org.scijava.plugin.Plugin; -import sc.fiji.labkit.ui.inputimage.DatasetInputImage; import sc.fiji.labkit.ui.segmentation.SegmentationTool; -import sc.fiji.labkit.ui.segmentation.SegmentationUtils; -import sc.fiji.labkit.ui.segmentation.Segmenter; -import sc.fiji.labkit.ui.segmentation.weka.TrainableSegmentationSegmenter; -import sc.fiji.labkit.ui.utils.ParallelUtils; import sc.fiji.labkit.ui.utils.progress.StatusServiceProgressWriter; import java.io.File; diff --git a/src/main/java/sc/fiji/labkit/ui/plugin/SegmentImageWithLabkitIJ1Plugin.java b/src/main/java/sc/fiji/labkit/ui/plugin/SegmentImageWithLabkitIJ1Plugin.java new file mode 100644 index 00000000..90508cfb --- /dev/null +++ b/src/main/java/sc/fiji/labkit/ui/plugin/SegmentImageWithLabkitIJ1Plugin.java @@ -0,0 +1,97 @@ +/*- + * #%L + * The Labkit image segmentation tool for Fiji. + * %% + * Copyright (C) 2017 - 2023 Matthias Arzt + * %% + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * #L% + */ + +package sc.fiji.labkit.ui.plugin; + +import java.io.File; + +import ij.ImagePlus; +import net.imglib2.img.VirtualStackAdapter; +import net.imglib2.img.display.imagej.ImageJFunctions; +import org.scijava.Cancelable; +import org.scijava.Context; +import org.scijava.ItemIO; +import org.scijava.app.StatusService; +import org.scijava.command.Command; +import org.scijava.plugin.Parameter; +import org.scijava.plugin.Plugin; +import sc.fiji.labkit.ui.segmentation.SegmentationTool; +import sc.fiji.labkit.ui.utils.progress.StatusServiceProgressWriter; + +/** + * @author Matthias Arzt + */ +@Plugin(type = Command.class, + menuPath = "Plugins > Labkit > Macro Recordable > Segment Image With Labkit (IJ1)") +public class SegmentImageWithLabkitIJ1Plugin implements Command, Cancelable { + + @Parameter + private Context context; + + @Parameter + private StatusService statusService; + + @Parameter + private ImagePlus input; + + @Parameter + private File segmenter_file; + + @Parameter(type = ItemIO.OUTPUT) + private ImagePlus output; + + @Parameter(required = false) + private Boolean use_gpu = false; + + @Override + public void run() { + SegmentationTool segmenter = new SegmentationTool(); + segmenter.setContext(context); + segmenter.setUseGpu(use_gpu); + segmenter.setProgressWriter(new StatusServiceProgressWriter(statusService)); + segmenter.openModel(segmenter_file.getAbsolutePath()); + output = ImageJFunctions.wrap(segmenter.segment(VirtualStackAdapter.wrap(input)), ""); + output.show(); + } + + @Override + public boolean isCanceled() { + return false; + } + + @Override + public void cancel(String reason) { + + } + + @Override + public String getCancelReason() { + return null; + } +} diff --git a/src/test/java/sc/fiji/labkit/ui/plugin/CalculateProbabilityMapWithLabkitIJ1PluginTest.java b/src/test/java/sc/fiji/labkit/ui/plugin/CalculateProbabilityMapWithLabkitIJ1PluginTest.java new file mode 100644 index 00000000..60ee404e --- /dev/null +++ b/src/test/java/sc/fiji/labkit/ui/plugin/CalculateProbabilityMapWithLabkitIJ1PluginTest.java @@ -0,0 +1,47 @@ +package sc.fiji.labkit.ui.plugin; + +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.io.IOException; + +import ij.IJ; +import ij.ImagePlus; +import ij.macro.Interpreter; +import net.imglib2.img.VirtualStackAdapter; +import net.imglib2.test.ImgLib2Assert; +import org.junit.Test; +import sc.fiji.labkit.pixel_classification.utils.SingletonContext; + +public class CalculateProbabilityMapWithLabkitIJ1PluginTest { + + @Test + public void test() throws IOException { + SingletonContext.getInstance(); + String inputImage = fullPath("/blobs.tif"); + String blobsModel = fullPath("/blobs.classifier"); + String source = fullPath("/blobs_probability_map.tif"); + File outputImage = File.createTempFile("labkit-segmentation-test", ".tif"); + String macroTemplate = "close('*');\n" + + "open('INPUT_TIF');\n" + + "run('Calculate Probability Map With Labkit (IJ1)', 'segmenter_file=SEGMENTER_FILE use_gpu=false');\n" + + "selectImage('probability map for blobs.tif');\n" + + "saveAs('Tiff', 'OUTPUT_TIF');\n" + + "close('*');\n"; + String macro = macroTemplate + .replace('\'', '"') + .replace("INPUT_TIF", inputImage) + .replace("SEGMENTER_FILE", blobsModel) + .replace("OUTPUT_TIF", outputImage.getAbsolutePath()); + new Interpreter().run(macro); + assertTrue(outputImage.exists()); + ImagePlus expected = IJ.openImage(source); + ImagePlus result = IJ.openImage(outputImage.getAbsolutePath()); + ImgLib2Assert.assertImageEquals(VirtualStackAdapter.wrap(expected), VirtualStackAdapter.wrap(result), Object::equals); + } + + private String fullPath(String name) { + return SegmentImageWithLabkitPluginTest.class.getResource( + name).getFile(); + } +} diff --git a/src/test/java/sc/fiji/labkit/ui/plugin/SegmentImageWithLabkitIJ1PluginTest.java b/src/test/java/sc/fiji/labkit/ui/plugin/SegmentImageWithLabkitIJ1PluginTest.java new file mode 100644 index 00000000..325f4d5e --- /dev/null +++ b/src/test/java/sc/fiji/labkit/ui/plugin/SegmentImageWithLabkitIJ1PluginTest.java @@ -0,0 +1,47 @@ +package sc.fiji.labkit.ui.plugin; + +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.io.IOException; + +import ij.IJ; +import ij.ImagePlus; +import ij.macro.Interpreter; +import net.imglib2.img.VirtualStackAdapter; +import net.imglib2.test.ImgLib2Assert; +import org.junit.Test; +import sc.fiji.labkit.pixel_classification.utils.SingletonContext; + +public class SegmentImageWithLabkitIJ1PluginTest { + + @Test + public void test() throws IOException { + SingletonContext.getInstance(); + String inputImage = fullPath("/blobs.tif"); + String blobsModel = fullPath("/blobs.classifier"); + String source = fullPath("/blobs_segmentation.tif"); + File outputImage = File.createTempFile("labkit-segmentation-test", ".tif"); + String macroTemplate = "close('*');\n" + + "open('INPUT_TIF');\n" + + "run('Segment Image With Labkit (IJ1)', 'segmenter_file=SEGMENTER_FILE use_gpu=false');\n" + + "selectImage('segmentation of blobs.tif');\n" + + "saveAs('Tiff', 'OUTPUT_TIF');\n" + + "close('*');\n"; + String macro = macroTemplate + .replace('\'', '"') + .replace("INPUT_TIF", inputImage) + .replace("SEGMENTER_FILE", blobsModel) + .replace("OUTPUT_TIF", outputImage.getAbsolutePath()); + new Interpreter().run(macro); + assertTrue(outputImage.exists()); + ImagePlus expected = IJ.openImage(source); + ImagePlus result = IJ.openImage(outputImage.getAbsolutePath()); + ImgLib2Assert.assertImageEquals(VirtualStackAdapter.wrap(expected), VirtualStackAdapter.wrap(result), Object::equals); + } + + private String fullPath(String name) { + return SegmentImageWithLabkitPluginTest.class.getResource( + name).getFile(); + } +} diff --git a/src/test/java/sc/fiji/labkit/ui/utils/TestResources.java b/src/test/java/sc/fiji/labkit/ui/utils/TestResources.java new file mode 100644 index 00000000..ed1a0cc7 --- /dev/null +++ b/src/test/java/sc/fiji/labkit/ui/utils/TestResources.java @@ -0,0 +1,2 @@ +package sc.fiji.labkit.ui.utils;public class TestResources { +}