Convert or Copy RandomAccessibleInterval (such as Views) to Img<T>

imglib2
views
copy
convert
rai

#1

Here we go again,

having trouble with above conversion. I found this solution on the forum.

Img<T> img = ImgView.wrap(rai, new ArrayImgFactory<>());

However, Eclipse is giving me strike outs reporting that this solution is deprecated. I am thankful for quick help.

Edits (Explanations / Corrections):
I am passing a View as RandomAccessibleInterval to a function. Now I want to make a copy of that View there, in order to do some processing, but no method is available to just create a copy of that View. I am fine with just a new empty image with same dimensions, but then again I cannot call

Img<T> img = rai.factory().create(rai);

because factory() is undefined for the Type RandomAccessibleInterval


#2

You should be able to copy from a RandomAccessibleInterval into an Img like this:

import net.imglib2.RandomAccessibleInterval;
import net.imglib2.img.Img;
import net.imglib2.util.Util;
import net.imglib2.loops.LoopBuilder;
import net.imglib2.type.Type;

final RandomAccessibleInterval<T> rai = ...
final Img<T> img = Util.getSuitableImgFactory(rai, Util.getTypeFromInterval(rai)).create(rai);
LoopBuilder.setImages(img, rai).forEachPixel(Type::set);

In many cases it is not necessary and/or beneficial to materialize a view into a new RandomAccessibleInterval or Img. If you share the relevant parts of your code, maybe we can figure out if it is necessary in your case.