-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
There is a bug somewhere in the SciJava IJ1 compatibility layer. An output ImagePlus or Dataset is not properly recognized in IJM scripts. It's unlikely that this bug will be fixed soon. So I provide a work around for Labkit.
- Loading branch information
Showing
6 changed files
with
294 additions
and
12 deletions.
There are no files selected for viewing
99 changes: 99 additions & 0 deletions
99
src/main/java/sc/fiji/labkit/ui/plugin/CalculateProbabilityMapWithLabkitIJ1Plugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
src/main/java/sc/fiji/labkit/ui/plugin/SegmentImageWithLabkitIJ1Plugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/test/java/sc/fiji/labkit/ui/plugin/CalculateProbabilityMapWithLabkitIJ1PluginTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/test/java/sc/fiji/labkit/ui/plugin/SegmentImageWithLabkitIJ1PluginTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
package sc.fiji.labkit.ui.utils;public class TestResources { | ||
} |