# How to add Patient Position tag (Origin) to each slice in a SimpleITK image?

**URL:** https://discourse.itk.org/t/how-to-add-patient-position-tag-origin-to-each-slice-in-a-simpleitk-image/5218
**Category:** Beginner Questions
**Tags:** itk, python, dicom, simpleitk
**Created:** [July 26, 2022, 1:54pm UTC](https://discourse.itk.org/t/how-to-add-patient-position-tag-origin-to-each-slice-in-a-simpleitk-image/5218 "2022-07-26T13:54:30Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![lbrandao](https://discourse.itk.org/letter_avatar_proxy/v4/letter/l/9de0a6/32.png) [@lbrandao](https://discourse.itk.org/u/lbrandao)
#### Post date: [July 26, 2022, 1:54pm UTC](https://discourse.itk.org/t/how-to-add-patient-position-tag-origin-to-each-slice-in-a-simpleitk-image/5218/1 "2022-07-26T13:54:30Z")

</div>

I have a multi-slice volume [shape (256,256,14)] stored in a .vtk file. The header of the file, contains the following information:

```auto
# vtk DataFile Version 3.0    
vtk output
BINARY
DATASET STRUCTURED_POINTS
DIMENSIONS 256 256 14
SPACING 1.25 1.25 8
ORIGIN 111.81 -205.959 104.612
POINT_DATA 917504
SCALARS scalars float
LOOKUP_TABLE default

```

The ORIGIN refers to the Patient Position [DICOM Tag (0020,0032)] for slice 0.

Each slice [there are 14 slices], has a common Orientation Position [DICOM Tag (0020,0037)] but different Patient Position values.

I read this .vtk file as a SimpleITK image and use the TransformPhysicalPointToIndex() to convert world coordinates to image coordinates.

```auto
import SimpleITK as sitk

path = "multi_slice_volume.vtk"

reader = sitk.ImageFileReader()
reader.SetFileName(path)
multi_slice_vlm = reader.Execute()

world_coord = np.array([[72.424, -171.130, 125.821]])
image_coord = multi_slice_vlm.TransformPhysicalPointToIndex(world_coord)

```

I know in advance the correct value for image\_coord - it should be (5,5,7) [this point belongs to slice 7]. However, the image\_coord that I get is (13, 26, 4).

After some research, I realised this happens because, during the conversion world\_coord-\>image\_coord:

- a) the Orientation Patient value used is the Simple ITK default value (1,0,0,0,1,0,0,0,1),
- b) the Position Patient value used is the one from slice 0 (111.81 -205.959 104.612)

I was able to fix the a) issue using:

```auto
multi_slice_colume.SetDirection((0.43, -0.58, 0.0, 0.85, 0.014, 0.0, -0.29, -0.81, 1.0))

```

However, I’m struggling with issue b). For each slice of the Simple ITK image, I need to set the corresponding Position Patient tag. Here’s what I tried:

```auto
multi_slice_vlm[:, :, 0].SetOrigin([111.809579849243, -205.95926731824, 104.612316131591]) # slice 0
multi_slice_vlm[:, :, 1].SetOrigin( [106.316583633422, -201.75588673353, 108.632091522216]) # slice 1
    (...)
multi_slice_vlm[:, :, 13].SetOrigin( [100.823587417602, -197.55250614881, 112.651866912841]) # slice 13

```

But in the end, I continue to get the wrong value for image\_coords.

---

<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: [July 26, 2022, 4:10pm UTC](https://discourse.itk.org/t/how-to-add-patient-position-tag-origin-to-each-slice-in-a-simpleitk-image/5218/2 "2022-07-26T16:10:59Z")

</div>

You should use [TransformIndexToPhysicalPoint()](https://itk.org/Doxygen/html/classitk_1_1ImageBase.html#a8360b9623d685997725c81c85716c4d2) to get slice positions. So set proper direction to the image, then in a for loop [construct indices](https://examples.itk.org/src/core/common/createaindex/documentation) [0,0,0] through [0,0,13]. For each index, get its physical position via `multi_slice_colume.TransformIndexToPhysicalPoint(index)`.

---

<div class="post-metadata">

### Author: ![lbrandao](https://discourse.itk.org/letter_avatar_proxy/v4/letter/l/9de0a6/32.png) [@lbrandao](https://discourse.itk.org/u/lbrandao)
#### Post date: [July 28, 2022, 3:26pm UTC](https://discourse.itk.org/t/how-to-add-patient-position-tag-origin-to-each-slice-in-a-simpleitk-image/5218/3 "2022-07-28T15:26:02Z")

</div>

Thank you @dzenanz. I’m quite new to ITK so I’m having some trouble understanding your answer.

**Step 1 - “set proper direction to the image”:**

```auto
path = "multi_slice_volume.vtk"

reader = sitk.ImageFileReader()
reader.SetFileName(path)
multi_slice_vlm = reader.Execute()

multi_slice_vlm.SetDirection((0.43, -0.58, 0.0, 0.85, 0.014, 0.0, -0.29, -0.81, 1.0))

```

**Step 2 - " in a for loop construct indices [0,0,0] through [0,0,13]"**  
I checked the link you provided (namely the Python example) but I was not able to understand how to use the itk.Index().

Also, it seems that I need to first convert the SimpleITK image in a ITK image, right?
