ImgLib2 in Detail

ImgLib2 is a general-purpose, multidimensional image processing library. Writing code using ImgLib2 is independent of image dimensionality, data type, and data storage strategy. It's designed to be reusable, to decouple algorithm development and data management, and to be extensible and adaptable through the addition of new algorithms, pixel types, and storage strategies.

Find javadocs for ImgLib2 here.

Quick Start

In [1]:
%classpath config resolver imagej.public https://maven.imagej.net/content/groups/public
%classpath add mvn net.imagej imagej 2.0.0-rc-71
ij = new net.imagej.ImageJ()
Added new repo: imagej.public

Creating and Displaying an Image

The following piece of code creates and displays an 400x320 8-bit gray-level image:

In [2]:
import net.imglib2.img.Img
import net.imglib2.img.array.ArrayImgFactory
import net.imglib2.type.numeric.integer.UnsignedByteType
 
// will create a window showing a black 400x320 image
long[] dimensions = [400, 320]
final Img< UnsignedByteType > img = new ArrayImgFactory<>( new UnsignedByteType() ).create( dimensions )
Out[2]:

Breaking lines 6-7 into individual steps...

  1. final ImgFactory< UnsignedByteType > factory = new ArrayImgFactory<>( new UnsignedByteType() );

  2. final long[] dimensions = new long[] { 400, 320 };

  3. final Img< UnsignedByteType > img = factory.create( dimensions );

Line 1: Pixel images in ImgLib2 are created using an ImgFactory. There are different ImgFactories, that create pixel containers with different memory layouts. Here, we create an ArrayImgFactory. This factory creates containers that map to a single flat Java array.

The type parameter of the factory specifies the value type of the image we want to create. We want to create a 8-bit gray-level image, thus we use UnsignedByteType.

Line 2: Next we create a long[] array that specifies the image size in every dimension. The length of the array specifies the number of dimensions. Here, we state that we want to create 400x320 2D image.

Line 3: We create the image, using the factory and dimensions. We store the result of the create() method in an Img variable. Img is a convenience interface that gathers properties of pixel image containers such as having a number of dimensions, being able to iterate its pixels, etc.

Opening and Displaying Image Files

You can open image files with the IO utility class of SCIFIO which calls Bio-Formats as needed. The following opens and displays an image file.

In [3]:
import net.imglib2.img.Img
import net.imglib2.img.array.ArrayImgFactory
import net.imglib2.type.numeric.integer.UnsignedByteType
import io.scif.img.IO

// save path for an image of Lena
path = "https://samples.fiji.sc/new-lenna.jpg"

// load image
final Img< UnsignedByteType > img = IO.openImg( path,
    new ArrayImgFactory<>( new UnsignedByteType() ) );
[INFO] Populating metadata
[INFO] Populating metadata
Out[3]: