Mixed World: Using ImageJ 1.x

Familiar with ImageJ 1.x? Want to mix and match? Here's how.

Enabling ImageJ 1.x

In order to make use of ImageJ 1.x functionality, we need to ensure the ImageJ Legacy component is present on the runtime classpath.

In [1]:
%classpath config resolver scijava.public https://maven.scijava.org/content/groups/public
%%classpath add mvn
net.imagej imagej-legacy 0.35.0
net.imagej imagej 2.0.0-rc-71
Added new repo: scijava.public
In [2]:
ij = new net.imagej.ImageJ()
"ImageJ v${ij.getVersion()} is ready to go."
Out[2]:
ImageJ v2.0.0-rc-71/1.52i is ready to go.

ImageJ2 patches ImageJ 1.x so that it can run headless. However, depending on your environment, the BeakerX kernels may not launch the JVM in headless mode by default.

Let's check whether we are running headless now:

In [3]:
["System property": Boolean.getBoolean("java.awt.headless"),
"UIService": ij.ui().isHeadless(),
"GraphicsEnvironment": java.awt.GraphicsEnvironment.isHeadless()]

Calling ImageJ 1.x directly

For the most part, you can call the ImageJ1 API directly as desired:

In [4]:
// From: https://commons.wikimedia.org/wiki/File:Julia_set_for_f(z)%3D_z%5E14-z.png
juliaIJ1 = ij.IJ.openImage("https://upload.wikimedia.org/wikipedia/commons/thumb/e/e6/Julia_set_for_f%28z%29%3D_z%5E14-z.png/120px-Julia_set_for_f%28z%29%3D_z%5E14-z.png")
juliaIJ1.getImage()
Out[4]:

Converting images

The SciJava ConvertService can be used to convert an ImageJ1 ImagePlus to an ImageJ2 Dataset:

In [5]:
juliaIJ2 = ij.convert().convert(juliaIJ1, net.imagej.Dataset.class)
Out[5]:

It is also possible to convert ImageJ2 Dataset objects to ImageJ1 ImagePlus:

In [6]:
// From: https://commons.wikimedia.org/wiki/File:Mandelbrot_Grayscale_Contours.png
fractalIJ2 = ij.io().open("https://upload.wikimedia.org/wikipedia/commons/thumb/b/b8/Mandelbrot_Grayscale_Contours.png/120px-Mandelbrot_Grayscale_Contours.png")
Out[6]:
In [7]:
fractalIJ1 = ij.convert().convert(fractalIJ2, ij.ImagePlus.class)
fractalIJ1.getAllStatistics()
Out[7]:
stats[count=11520, mean=59.86666666666667, min=9.0, max=212.0]
In [8]:
["Count": ij.op().stats().size(fractalIJ2).getRealDouble(),
 "Mean": ij.op().stats().mean(fractalIJ2).getRealDouble(),
 "Min": ij.op().stats().min(fractalIJ2).getRealDouble(),
 "Max": ij.op().stats().max(fractalIJ2).getRealDouble()]