# GetImageViewFromArray in SimpleITK

**URL:** <https://discourse.itk.org/t/getimageviewfromarray-in-simpleitk/4436>\
**Category:** Engineering\
**Tags:** simpleitk\
**Created:** [September 22, 2021, 9:48am UTC](https://discourse.itk.org/t/getimageviewfromarray-in-simpleitk/4436 "2021-09-22T09:48:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rogertrullo](https://discourse.itk.org/user_avatar/discourse.itk.org/rogertrullo/32/1953_2.png) [@rogertrullo](https://discourse.itk.org/u/rogertrullo)\
**Post date:** [September 22, 2021, 9:48am UTC](https://discourse.itk.org/t/getimageviewfromarray-in-simpleitk/4436/1 "2021-09-22T09:48:44Z")

</div>

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!!

---

<div class="post-metadata">

**Author:** ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)\
**Post date:** [September 22, 2021, 1:38pm UTC](https://discourse.itk.org/t/getimageviewfromarray-in-simpleitk/4436/2 "2021-09-22T13:38:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rogertrullo](https://discourse.itk.org/user_avatar/discourse.itk.org/rogertrullo/32/1953_2.png) [@rogertrullo](https://discourse.itk.org/u/rogertrullo)\
**Post date:** [September 22, 2021, 1:45pm UTC](https://discourse.itk.org/t/getimageviewfromarray-in-simpleitk/4436/3 "2021-09-22T13:45:32Z")

</div>

> [@zivy](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [September 22, 2021, 1:54pm UTC](https://discourse.itk.org/t/getimageviewfromarray-in-simpleitk/4436/4 "2021-09-22T13:54:42Z")

</div>

You could use ITK with Python wrapping (instead of SimpleITK), which does have [GetImageViewFromArray](https://blog.kitware.com/convert-itk-data-structures-to-numpy-arrays/). You could take a look at the [quick start guide](https://itkpythonpackage.readthedocs.io/en/master/Quick_start_guide.html) to see sample usage.

---

<div class="post-metadata">

**Author:** ![rogertrullo](https://discourse.itk.org/user_avatar/discourse.itk.org/rogertrullo/32/1953_2.png) [@rogertrullo](https://discourse.itk.org/u/rogertrullo)\
**Post date:** [September 22, 2021, 1:57pm UTC](https://discourse.itk.org/t/getimageviewfromarray-in-simpleitk/4436/5 "2021-09-22T13:57:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [September 22, 2021, 1:58pm UTC](https://discourse.itk.org/t/getimageviewfromarray-in-simpleitk/4436/6 "2021-09-22T13:58:02Z")

</div>

> [@rogertrullo](#):
>
> 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.

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).

---

<div class="post-metadata">

**Author:** ![rogertrullo](https://discourse.itk.org/user_avatar/discourse.itk.org/rogertrullo/32/1953_2.png) [@rogertrullo](https://discourse.itk.org/u/rogertrullo)\
**Post date:** [September 22, 2021, 2:03pm UTC](https://discourse.itk.org/t/getimageviewfromarray-in-simpleitk/4436/7 "2021-09-22T14:03:12Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [September 22, 2021, 2:03pm UTC](https://discourse.itk.org/t/getimageviewfromarray-in-simpleitk/4436/8 "2021-09-22T14:03:22Z")

</div>

> [@rogertrullo](#):
>
> that means I will need to port the other functions

Correct. But there are examples for both [resampling](https://itk.org/ITKExamples/src/Filtering/ImageGrid/ResampleAnImage/Documentation.html) and [registration](https://itk.org/ITKExamples/src/Registration/Common/Perform2DTranslationRegistrationWithMeanSquares/Documentation.html), and differences to SimpleITK should not be significant.
