Stacking RandomAccessibles and have the display interpreting the dimensions right

imglib2
imagej2
Tags: #<Tag:0x00007fb87d58b348> #<Tag:0x00007fb87d58b1b8>

#1

Hello

I have some trouble getting the visualisation of a image stack right. What I end up with is something like outlined bellow.

long[] dim = new long[]{50, 100};
RandomAccessibleInterval<UnsignedByteType> rai1 = ... // loaded somehow
RandomAccessibleInterval<UnsignedByteType> rai2 = ... // proceessed stuff
RandomAccessibleInterval<UnsignedByteType> rai3 = ... // whatever

RandomAccessibleInterval< UnsignedByteType > stack = Views.stack( rai1, rai2, rai3);

Then I thought I could tell ImageJ what my axis are before displaying the data:

ImgPlus<UnsignedByteType> img = new ImgPlus<UnsignedByteType>(stack, "My Stack", new AxisType[]{Axes.X, Axes.Y, Axes.CHANNEL});
ImageJ ij = new Image();
ij.ui().show(img);

But ImgPlus wants Img as input, and even if the rai where created with ArrayImgFactory(), once I used Views.stack(), I cannot cast them to Img anymore. So I feeling a bit like swimming upstream… What would be the correct way to get from the Views.stack() output to something that knows which axis is which and that is understood by ij.ui().show()?


#2

You could use ImgView.wrap( stack, new ArrayImgFactory<>()) to wrap the rai into an Img and put this into the ImgPlus constructor:

ImgPlus<UnsignedByteType> img = new ImgPlus<UnsignedByteType>(
                                       ImgView.wrap( stack, new ArrayImgFactory<>()),
                                       "My Stack",
                                       new AxisType[]{Axes.X, Axes.Y, Axes.CHANNEL});
ImageJ ij = new ImageJ();
ij.ui().show(img);

I cannot guarantee that this is the best way to do it but it should work.