Stacking RandomAccessibles and have the display interpreting the dimensions right




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

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


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

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