Convert RandomAccessibleInterval to ImgPlus or Dataset

imglib2
imagej
imagej2
java

#1

Hello!

@ctrueden

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.


#2

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.


#3

Hello @Christian_Tischer,

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

Img<T> img = ops.run(net.imagej.ops.create.img.CreateImgFromRAI.class, rai);
ops.run(net.imagej.ops.copy.CopyRAI.class, 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.

Cheers,
Alison


#4

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?


#5

Hi Allison,

Thanks for the info!

Do you have a full java code example for this somewhere?
I am struggling importing the ops.run 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."
http://javadoc.scijava.org/ImgLib2/net/imglib2/img/Img.html?is-external=true

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?!


#6

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
#7

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();