GetImageViewFromArray in SimpleITK

Hi all, I am using Simpleitk and it seems that there is no GetImageViewFromArray funciton. I have the GetArrayViewFromImage function though. Is there any reason on why is this function not available? When I do GetImageFromArray the code is doing a deep copy which can be very slow for large arrays.
Thanks!!

Hello @rogertrullo,

The missing GetImageViewFromArray is intentional. SimpleITK memory management uses reference counting and this form of memory sharing is fragile and can lead to unexpected results. We want to keep all SimpleITK operations safe, memory-wise.

Thanks @zivy , do you know if is there any way to do this arraytoimage operation faster? I am only doing read operation on the itk image e.g. registration and resampling.

You could use ITK with Python wrapping (instead of SimpleITK), which does have GetImageViewFromArray. You could take a look at the quick start guide to see sample usage.

@dzenanz Thanks, that means I will need to port the other functions as well right? registration, resampling etc.

Do you have any timing information with example code? We have found that bulk copying of image data to be very fast even with large images (100GB).

Thanks @blowekamp I am just doing this:

img_itk = sitk.GetImageFromArray(img_np, isVector=True)
img_np is a (69120, 42240, 6) numpy array and the function takes ~185 seconds.
Is this the expected speed?

Correct. But there are examples for both resampling and registration, and differences to SimpleITK should not be significant.

1 Like