ImageJ1-ImageJ2 cheat sheet

Revision as of 03:07, 9 October 2018 by Eglinger (talk | contribs) (Add ImageJ2 options for retrieving, opening and saving images)

This page summarizes translation of basic operations of ImageJ1 and ImageJ2 API. Based on the work of Robert Hasse, Scientific Computing Facility, MPI CBG Dresden.

Task ImageJ1 ImageJ2
Starting ImageJ
ImageJ ij = new net.imagej.ImageJ();
Show images imp is an ImagePlus object
Retrieve an active image object
ImagePlus imp = IJ.getImage();
Script parameter (the same for Dataset, ImagePlus, etc.):
#@ Img image

In Java code:

private Img image;

Using ImageDisplayService:

Dataset image = ij.imageDisplay().getActiveDataset();
Open an image file IJ.openImage() returns an ImagePlus object without showing.
ImagePlus imp = IJ.openImage("/path/to/image.tif");;
ImagePlus imp = IJ.openImage("");; automatically shows the image without returning ImagePlus."/path/to/image.tif");
ImagePlus imp = IJ.getImage()"");
ImagePlus imp = IJ.getImage()
Using DatasetIOService:
Dataset image = ij.scifio().datasetIO().open(path);
Save an image file
IJ.saveasTiff(imp, "/path/to/image.tif")
Using DatasetIOService:
ij.scifio().datasetIO().save(dataset, "/path/to/image.tif");
Convert image types Convert from ImageJ2 Img object to ImageJ1 ImagePlus object:
ImagePlus imp = ImageJFunctions.wrap(img,"Title");
Convert from ImageJ1 ImagePlus object to ImageJ Img object:
Img<T> realImg = ImageJFunctions.wrapReal(imp);
Img<FloatType> floatImg = ImageJFunctions.convertFloat(imp);
Img<FloatType> realImg2 = ImageJFunctions.wrap(imp);
Show regions
Img<BitType> mask; // = ...
ImagePlus maskImp =ImageJFunctions.wrap(mask, "mask");
// threshold the mask to get an ROI
ImageProcessor imageProcessor = maskImp.getProcessor();
imageProcessor.setThreshold(128,128,ImageProcessor NO_LUT_UPDATE);
Roi roi = new ThresholdToSelection().convert(imageProcessor);
Run plugins,"Normalisation","");
ij.command().run(ImageNormalizerIJ2Plugin.class,false, new Object[]{"input", img, "ij", ij});,"Normalisation (IJ2)", "");
// don't forget the IJ legacy dependency
Define plugins
public class ImageNormalizerPlugin implements PluginFilter {
Pluins>Filtering, "Normalisation", NormalizerPlugin
@Plugin(type = Command.class, menuPath = "Plugins>Normalization")
public class ImageNormalizerIJ2Plugin implements Command {

See also