Page history Edit this page How do I edit this website?
This page describes content relating to the ImageJ2 platform. Click the logo for details.

ImageJ-ImageJ2 cheat sheet

This page summarizes translation of basic operations of ImageJ 1.x and ImageJ2 API. Based on the work of Robert Haase, Scientific Computing Facility, MPI-CBG Dresden.

Task

ImageJ

ImageJ2

Starting ImageJ

new ij.ImageJ();
ImageJ ij = new net.imagej.ImageJ();
ij.ui().showUI();

Show images

imp is an ImagePlus object

imp.show();
ij.ui().show(imp);
ImageJFunctions.show(imp);
ImageJFunctions.wrap(imp,"Image").show();

Retrieve an active image object

ImagePlus imp = IJ.getImage();

Script parameter (the same for Dataset, ImagePlus, etc.):

#@ Img image

In Java code:

@Parameter
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(urlOrFilePath);
imp.show();

IJ.open() automatically shows the image without returning ImagePlus.

IJ.open(urlOrFilePath);
ImagePlus imp = IJ.getImage();

Using IOService:

Object image = ij.io().open(urlOrFilePath);

Using DatasetIOService (for type safety):

Dataset image = ij.scifio().datasetIO().open(urlOrFilePath);

Save an image file

IJ.saveasTiff(imp, "/path/to/image.tif")

Using IOService:

ij.io().save(dataset, "/path/to/image.tif");

Using DatasetIOService:

ij.scifio().datasetIO().save(dataset, "/path/to/image.tif");

Convert image types

Convert from ImgLib2 Img object to ImageJ ImagePlus object:

ImagePlus imp = ImageJFunctions.wrap(img,"Title");

Convert from ImageJ ImagePlus object to ImgLib2 Img object:

Img img = ij.convert().convert(imp, Img.class);
Img<T> realImg = ImageJFunctions.wrapReal(imp);
Img<FloatType> floatImg = ImageJFunctions.convertFloat(imp);
Img<FloatType> realImg2 = ImageJFunctions.wrap(imp);

Show regions

imagePlus.setRoi(roi);
Img<BitType> mask; // = ...
Roi roi = ij.convert().convert(mask, Roi.class);
imagePlus.setRoi(roi);

Run plugins

IJ.run(imagePlus,"Normalisation","");
ij.command().run(ImageNormalizerIJ2Plugin.class, true,
                 "input", img, "ij", ij);

Define plugins

public class ImageNormalizerPlugin implements PluginFilter {
  ...
}

In resources/plugins.config:

Plugins>Filtering, "Normalisation", NormalizerPlugin
@Plugin(type = Command.class, menuPath = "Plugins>Normalization")
public class ImageNormalizerIJ2Plugin implements Command {
  ...
}

See also

on GitHub