Best way to copy data between ImgLib2 images

I have two ImgLib2 RandomAccessibleInterval objects with the same min/max bounds. One is empty, the other is populated with sample values.

I want to copy the populated image to the empty one. What is the best way to do that according to the ImgLib2 paradigm?

I know that one way is to use LoopBuilder:

LoopBuilder.setImages(srcImage, destImage).forEachPixel((src, dest) -> dest.set(src));

This is elegant, but uses a BiConsumer lambda. I want to do the copy from Python (via pyimagej), so I’d prefer to call a method that doesn’t require a lambda.

Another way is to use ImageJ Ops:"copy.rai", destImage, srcImage);

And ultimately this is how I want to do it, because it will select the most performant copy.rai op based on the image types. But as a short-term performance fix, we are looking for a static method in ImgLib2 itself to do it directly, until the new SciJava Ops is released.

@tpietzsch @axtimwalde @maarzt Does one exist?

Relatedly, there is an open PR seeking to add this:

For those interested, the relevant pyimagej issue is:


Just to tie up this topic: I added a method net.imagej.util.Images.copy(src, dest) which copies sample values from a source RandomAccessibleInterval to a destination one. It uses LoopBuilder with multiThreaded() internally to be fast.

The Images.copy method is part of imagej-common 0.30.0, released just now.

For further discussion, see: