Render ImgLib2 data into notebook objects.
imglib2
jupyter
notebook
Author

Stephan Saalfeld

Published

September 14, 2022

In this notebook, we will explore how to store, process and visualize data with ImgLib2 in a notebook.

First let’s add the necessary dependencies. We will use ImageJ to load example images and to generate RenderedImage outputs that we can use to render in the notebook. Then, we will import ImgLib2 and the modules to share data between ImgLib2 and ImageJ and the imglib2-realtransform module that includes various transformations.

Code
%mavenRepo scijava.public https://maven.scijava.org/content/groups/public
%maven net.imglib2:imglib2:7.1.2
%maven net.imglib2:imglib2-ij:2.0.3
%maven org.scijava:scijava-common:2.99.0

Let’s open one of ImageJ’s example images and show it in the notebook. This uses Spencer Park’s image renderer:

Code

If we want to work with this image in ImgLib2, we need to provide it as an ImgLib2 interface:

There is no default renderer for ImgLib2 interfaces available to the notebook kernel, so we see a default String representation of the result (when rendering this cell the first time). So let’s register some simple renderers that use ImgLib2’s ImageJ bridge and Spencer Park’s image renderer to render ImgLib2 data into the notebook. We add a version that renders the first 2D slice of a RandomAccessibleInterval and a second version that renders a default interval 512x512+0+0 of the 2D slice at position 0 in all other dimensions of an infinite RandomAccessible.

Code
import io.github.spencerpark.jupyter.kernel.display.common.*;
import io.github.spencerpark.jupyter.kernel.display.mime.*;
import net.imglib2.img.display.imagej.*;
import net.imglib2.view.*;

import ij.*;
import net.imglib2.*;
import net.imglib2.img.imageplus.*;

getKernelInstance().getRenderer().createRegistration(RandomAccessibleInterval.class)
        .preferring(MIMEType.IMAGE_PNG)
        .supporting(MIMEType.IMAGE_JPEG, MIMEType.IMAGE_GIF)
        .register((rai, context) -> Image.renderImage(
                ImageJFunctions.wrap(rai, rai.toString()).getBufferedImage(),
                context));

getKernelInstance().getRenderer().createRegistration(RandomAccessible.class)
        .preferring(MIMEType.IMAGE_PNG)
        .supporting(MIMEType.IMAGE_JPEG, MIMEType.IMAGE_GIF)
        .register((ra, context) -> Image.renderImage(
                ImageJFunctions.wrap(
                        Views.interval(
                                ra,
                                new FinalInterval(
                                        Arrays.copyOf(
                                                new long[]{512, 512},
                                                ra.numDimensions()))),
                        ra.toString()).getBufferedImage(),
                context));
Code
var imp = IJ.openImage("https://mirror.imagej.net/ij/images/clown.jpg");
imp.getBufferedImage();

Code
RandomAccessibleInterval rai = ImagePlusImgs.from(imp);
System.out.println( rai );
IntImagePlus [320x200]
Code
rai;