# Create image after numpy transpose

**URL:** https://discourse.itk.org/t/create-image-after-numpy-transpose/3241
**Category:** Beginner Questions
**Created:** [June 30, 2020, 11:01am UTC](https://discourse.itk.org/t/create-image-after-numpy-transpose/3241 "2020-06-30T11:01:08Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![emilljungberg](https://discourse.itk.org/user_avatar/discourse.itk.org/emilljungberg/32/1562_2.png) [@emilljungberg](https://discourse.itk.org/u/emilljungberg)
#### Post date: [June 30, 2020, 11:01am UTC](https://discourse.itk.org/t/create-image-after-numpy-transpose/3241/1 "2020-06-30T11:01:08Z")

</div>

Hi!

I’m building an image registration framework for MRI data that requires combination of numpy and ITK. I recently realised that the indexing in ITK arrays and numpy arrays are swapped from [x,y,z] to [z,y,x] (from [this forum thread)](https://discourse.itk.org/t/why-to-invert-the-order-of-the-indices-between-numpy-and-simpleitk/809/11). So in order to fix this I thought I’d apply `numpy.tranpose` before creating the image, but it turns out this has no effect on the generated image. I know that it is not recommended to modify the data like this but I want to make so that translations estimated in “X” in ITK corresponds to the 0th dimension of my numpy array (if there is a better version to do this I’m all ears).

To demonstrate the issue with an example: I have a Shepp logan phantom that I have generated using [Sigpy](https://sigpy.readthedocs.io/en/latest/) here called `phan`

```python
phan = abs(sigpy.shepp_logan((64,64,64)))
# Transpose array and create ITK image objects
phan_T = np.transpose(phan, (2,1,0))
img_phan = itk.image_from_array(phan)
img_phan_T = itk.image_from_array(phan_T)

```

If I compare the numpy arrays I can see the effect of the transpose:

```python
compare(phan, phan_T, interpolation=False, ui_collapsed=True, mode='x')

```

 ![image](https://discourse.itk.org/uploads/default/original/2X/1/15e5d21c699351244a1064320d23d2fea043f6c9.png)

But the effect of the transpose does not appear when I compare the ITK images

```python
compare(img_phan, img_phan_T, interpolation=False, ui_collapsed=True, mode='x')

```

 ![image](https://discourse.itk.org/uploads/default/original/2X/1/1732503805f4a5a31dcc79c6c623d51759c0d2b3.png)

My thought is that this has something to do with the fact that numpy is returning a view of the array and not a deep copy, but I’ve tried creating a copy of the array before producing the ITK image but with the same result. The only way I could get it to work was to create a 4D array which I slice in the `transpose` function as

```python
A = np.zeros((64,64,64,2))
A[:,:,:,0] = phan
phan_T = np.transpose(A[:,:,:,0], (2,1,0))

img_phan = itk.image_from_array(phan)
img_phan_T = itk.image_from_array(phan_T)

compare(img_phan, img_phan_T, interpolation=False, ui_collapsed=True, mode='x')

```

 ![image](https://discourse.itk.org/uploads/default/original/2X/c/ca012825d93595d6d135df0c5b30d689687d1069.png)

I feel like there is something I’m missing here in the way I’m creating the images, any help is very welcome.

Thank you very much!

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [July 14, 2020, 5:01pm UTC](https://discourse.itk.org/t/create-image-after-numpy-transpose/3241/2 "2020-07-14T17:01:35Z")

</div>

Hello @emilljungberg!

Looks like a great project! 🧠 🧲

Yes, most pain will be avoided by using the natural NumPy array indexing, which most closely corresponds to _[z, y, x]_. This is because the default indexing order of NumPy arrays is _C-order_ as opposed to _Fortran-order_, that is, the fastest-moving index, i.e. the index that data is arranged next to each other in memory, is the last index. See also the [scikit-image notes on this topic](https://scikit-image.org/docs/dev/user_guide/numpy_images.html#notes-on-the-order-of-array-dimensions).

Calling _.transpose()_ often does not actually change this order of data, it just changes the NumPy indexing order. Check the value with `.flags`

```auto
phan.flags

```

and observe the `C_CONTIGUOUS` and `F_CONTIGUOUS` values.

Note that when used in ITK, the NumPy array is consumed in _C-order_ form.

So, the data can be indexed in its natural order or it can be changed with `np.flip` or other functions instead of `np.transpose`.

---

<div class="post-metadata">

### Author: ![phcerdan](https://discourse.itk.org/user_avatar/discourse.itk.org/phcerdan/32/286_2.png) [@phcerdan](https://discourse.itk.org/u/phcerdan)
#### Post date: [December 30, 2021, 7:11am UTC](https://discourse.itk.org/t/create-image-after-numpy-transpose/3241/3 "2021-12-30T07:11:22Z")

</div>

I faced this problem as well, converting a non-standard [i, j, k] (contiguous order) to [k, j, i] (contiguous).  
I ended up with this solution, in case anyone is interested in the future (myself included 😛 )

```python
arr = np.einsum('ijk->kji', ijk_array)
arr = np.ascontiguousarray(arr)
itk_img = itk.image_from_array(arr)

```

`einsum` has an `order` parameter, but is ignored, always returning a `F` ordered array.
