Skip to content

Commit

Permalink
Add IJM macro compatible commands
Browse files Browse the repository at this point in the history
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
maarzt committed Jan 11, 2024
1 parent 3052e6b commit 214ff98
Show file tree
Hide file tree
Showing 6 changed files with 294 additions and 12 deletions.
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -29,26 +29,16 @@

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;
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.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;
Expand Down
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;
}
}
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();
}
}
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();
}
}
2 changes: 2 additions & 0 deletions src/test/java/sc/fiji/labkit/ui/utils/TestResources.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package sc.fiji.labkit.ui.utils;public class TestResources {
}

0 comments on commit 214ff98

Please sign in to comment.