Convert RandomAccessibleInterval to ImgPlus or Dataset





How does one convert a RandomAccessibleInterval to an ImgPlus or Dataset?
Basically something that I can properly display using ij.ui().show().

I know that I can give ij.ui().show() a RandomAccessibleInterval as an input directly, but then it does not know what the AxisTypes are.

In my specific case right now the RAI is 4-D with XYCT and I already do have an AxisType[ ] that I can provide for the proper conversion.


Hi @Christian_Tischer,

I tagged your post, so that it can be more easily found in the future. I highly recommend that you take the few seconds to do this yourself in the future, for the benefit of the community :slight_smile:

Use the ImagePlus(Img, String, AxisType[]) constructor.

Whether this shows correctly depends on which UI you’re using. For LegacyUI there might be some bugs, as ImageJ 1.x always assumes XYCZT order for stacks.


Hello @Christian_Tischer,

You could use imagej-ops to convert from a RandomAccessibleInterval to Img. Something like:

Img<T> img =, rai);, img, rai);

The first line just creates an Img of the same type and size, and then the second copies the data from the RandomAccessibleInterval into the Img. And then from there you can use the ImgPlus(Img, String, AxisType[]) constructor to get an ImgPlus.



Thanks @imagejan for the hint with the tagging! I will do this!

The ImgPlus( Img, String, AxisType[ ] ) constructor does not work because it wants an Img and not an RAI…

This is good to know! But do I have to take care of this myself or will the ImgPlus( Img, String, AxisType[ ] ) together with the ui().show() command do the job for me and create the right output for viewing it in ImageJ?


Hi Allison,

Thanks for the info!

Do you have a full java code example for this somewhere?
I am struggling importing the function…

Also I am wondering whether there shouldn’t be some really easy way to convert an RAI to an Img?

I found below statement in the java doc
"An Img is a RandomAccessibleInterval that has its min at 0 and and its max positive. Imgs store pixels and thus are the basis for conventional image processing."

Sounds to me like an Img is more or less nothing much more than an RAI, isn’t it?

…in fact I wonder now whether the statement in the java doc relates to the axes or to the gray values. I guess the axes, right? Feels like a simple Views.zeroMin( rai ) should more or less do the job?!


I tried some more using your suggestions and this works for me:

ImageJ ij = new ImageJ();
DatasetService datasetService = ij.dataset();
AxisType[] axisTypes = new AxisType[]{ Axes.X, Axes.Y, Axes.CHANNEL, Axes.TIME };
ImgPlus imgPlus = new ImgPlus( datasetService.create( rai  ), "image", axisTypes );
ij.ui().show( imgPlus );

Thanks both of you: @imagejan and @awalter17!

[Edit] DatasetService is now set to ij.dataset() as @imagejan recommended.

Convert ImgLib2 array to IJ1 stack

Instead of creating a new Service object, it is best practice to always get the service from the existing context, in this case using ij.dataset():

DatasetService datasetService = ij.dataset();