# Aligning segmentation volume with image volume in code using nrrd headers

**URL:** <https://discourse.slicer.org/t/aligning-segmentation-volume-with-image-volume-in-code-using-nrrd-headers/1514>\
**Category:** Support\
**Tags:** python, nrrd\
**Created:** [November 24, 2017, 4:18am UTC](https://discourse.slicer.org/t/aligning-segmentation-volume-with-image-volume-in-code-using-nrrd-headers/1514 "2017-11-24T04:18:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dcantor](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dcantor/32/782_2.png) [@dcantor](https://discourse.slicer.org/u/dcantor)\
**Post date:** [November 24, 2017, 4:18am UTC](https://discourse.slicer.org/t/aligning-segmentation-volume-with-image-volume-in-code-using-nrrd-headers/1514/1 "2017-11-24T04:18:14Z")

</div>

Hello, I am working on prostate segmentation in Slicer. After finishing the segmentation and saving the Scene, I took a look at the image volume (V) and the segmentation volume (S) nrrd files in python, the two volumes have the same orientation as I performed segmentation on axial slices (on the RA plane) and moved in the Superior-Inferior axis, segmenting one slice at a time.

Both V and S have ijk-to-ras matrices that enable me to recreate their relative location in space as shown in the figure below:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/3/1/313f7bce1885f5f3a7237bda07e9f5be8629fc8a.png)

For a given a slice in **V** , I would like to find _the corresponding slice_ in **S** in my code (or none if no intersection). Since I know that the two the volumes have the same orientation in space, I was thinking of simply taking one slice in V and check _which slice in S is coplanar_. I can do this by taking one point for each slice in S, so if S has 10 slices I have 10 points to compare with the current slice in V. Does this make sense? Or is there a simple way to achieve this?

Thank you

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [November 24, 2017, 4:25am UTC](https://discourse.slicer.org/t/aligning-segmentation-volume-with-image-volume-in-code-using-nrrd-headers/1514/2 "2017-11-24T04:25:20Z")

</div>

You can get the slice coordinates very easily by converting voxel coordinates to LPS and LPS to voxel coordinates of the other volume:

```
point_IJK_S = inv(IJKToRAS_S) * IJKToRAS_V * point_IJK_V
```

---

<div class="post-metadata">

**Author:** ![dcantor](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dcantor/32/782_2.png) [@dcantor](https://discourse.slicer.org/u/dcantor)\
**Post date:** [November 24, 2017, 1:37pm UTC](https://discourse.slicer.org/t/aligning-segmentation-volume-with-image-volume-in-code-using-nrrd-headers/1514/3 "2017-11-24T13:37:41Z")

</div>

You mean RAS right? to RAS…

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [November 24, 2017, 2:10pm UTC](https://discourse.slicer.org/t/aligning-segmentation-volume-with-image-volume-in-code-using-nrrd-headers/1514/4 "2017-11-24T14:10:37Z")

</div>

If you have a slice number (typically K coordinate) in one image and you need slice number in another image then you need to convert IJK-\>RAS-\>IJK (or IJK-\>LPS-\>IJK).

---

<div class="post-metadata">

**Author:** ![dcantor](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dcantor/32/782_2.png) [@dcantor](https://discourse.slicer.org/u/dcantor)\
**Post date:** [November 24, 2017, 3:03pm UTC](https://discourse.slicer.org/t/aligning-segmentation-volume-with-image-volume-in-code-using-nrrd-headers/1514/5 "2017-11-24T15:03:35Z")

</div>

Got it. Makes sense, simpler than what I had in mind, thank you so much Andras!

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [July 7, 2019, 12:07pm UTC](https://discourse.slicer.org/t/aligning-segmentation-volume-with-image-volume-in-code-using-nrrd-headers/1514/6 "2019-07-07T12:07:30Z")

</div>

A post was merged into an existing topic: [Aligning Segmentations and images using python](https://discourse.slicer.org/t/aligning-segmentations-and-images-using-python/7446/3)
