# Are numpy arrays supported in the object-oriented API?

**URL:** <https://discourse.itk.org/t/are-numpy-arrays-supported-in-the-object-oriented-api/6137>\
**Category:** Beginner Questions\
**Tags:** numpy\
**Created:** [August 10, 2023, 1:27pm UTC](https://discourse.itk.org/t/are-numpy-arrays-supported-in-the-object-oriented-api/6137 "2023-08-10T13:27:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![AwesomeLemon](https://discourse.itk.org/user_avatar/discourse.itk.org/awesomelemon/32/3626_2.png) [@AwesomeLemon](https://discourse.itk.org/u/AwesomeLemon)\
**Post date:** [August 10, 2023, 1:27pm UTC](https://discourse.itk.org/t/are-numpy-arrays-supported-in-the-object-oriented-api/6137/1 "2023-08-10T13:27:28Z")

</div>

Input in the form of numpy arrays works with the procedural API but doesn’t work with the object-oriented API (I get “itk.ElastixRegistrationMethod is not wrapped for input type `None`”).  
Is that a known limitation or am I doing something wrong?

(I need to switch to the object-oriented API to do multi-image registration, which is only supported by the object-oriented API)

Code sample:

```auto
import imageio
import itk
import numpy as np

fixed = imageio.imread('CT_2D_head_fixed.mha')
fixed = np.asarray(fixed).astype(np.float32)

moving = imageio.imread('CT_2D_head_moving.mha')
moving = np.asarray(moving).astype(np.float32)

if False:
    # procedural interface
    registered, parameters = itk.elastix_registration_method(fixed, moving)
else:
    parameter_object = itk.ParameterObject.New()
    default_rigid_parameter_map = parameter_object.GetDefaultParameterMap('rigid')
    parameter_object.AddParameterMap(default_rigid_parameter_map)

    # Load Elastix Image Filter Object
    elastix_object = itk.ElastixRegistrationMethod.New(fixed, moving)
    # elastix_object.SetFixedImage(fixed_image)
    # elastix_object.SetMovingImage(moving_image)
    elastix_object.SetParameterObject(parameter_object)

    # Set additional options
    elastix_object.SetLogToConsole(False)

    # Update filter object (required)
    elastix_object.UpdateLargestPossibleRegion()

    # Results of Registration
    result_image = elastix_object.GetOutput()
    result_transform_parameters = elastix_object.GetTransformParameterObject()

```

---

<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:** [August 10, 2023, 2:35pm UTC](https://discourse.itk.org/t/are-numpy-arrays-supported-in-the-object-oriented-api/6137/2 "2023-08-10T14:35:42Z")

</div>

If you move these two lines  
`fixed = np.asarray(fixed).astype(np.float32)`  
`moving = np.asarray(moving).astype(np.float32)`  
into `# procedural interface`, it should work.

---

<div class="post-metadata">

**Author:** ![AwesomeLemon](https://discourse.itk.org/user_avatar/discourse.itk.org/awesomelemon/32/3626_2.png) [@AwesomeLemon](https://discourse.itk.org/u/AwesomeLemon)\
**Post date:** [August 10, 2023, 2:59pm UTC](https://discourse.itk.org/t/are-numpy-arrays-supported-in-the-object-oriented-api/6137/3 "2023-08-10T14:59:34Z")

</div>

Yes, but then the Object-oriented interface would work with these “mha” images rather than numpy arrays. These filenames in my code snippet are placeholders from the documentation (to make reproduction simpler), my actual images are .png (which I convert to numpy arrays).

---

<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:** [August 10, 2023, 3:01pm UTC](https://discourse.itk.org/t/are-numpy-arrays-supported-in-the-object-oriented-api/6137/4 "2023-08-10T15:01:40Z")

</div>

Then use ITK/numpy conversion functions, as per the [documentation](https://itkpythonpackage.readthedocs.io/en/stable/Quick_start_guide.html#itk-and-numpy), and pass proper ITK images to ITK object interface.

---

<div class="post-metadata">

**Author:** ![AwesomeLemon](https://discourse.itk.org/user_avatar/discourse.itk.org/awesomelemon/32/3626_2.png) [@AwesomeLemon](https://discourse.itk.org/u/AwesomeLemon)\
**Post date:** [August 10, 2023, 5:21pm UTC](https://discourse.itk.org/t/are-numpy-arrays-supported-in-the-object-oriented-api/6137/5 "2023-08-10T17:21:49Z")

</div>

Thank you, that was precisely what I needed!
