# Resample to same Spacing and Size and Align to same Origin

**URL:** <https://discourse.itk.org/t/resample-to-same-spacing-and-size-and-align-to-same-origin/1378>\
**Category:** Beginner Questions\
**Tags:** dicom, registration, python\
**Created:** [October 27, 2018, 5:04pm UTC](https://discourse.itk.org/t/resample-to-same-spacing-and-size-and-align-to-same-origin/1378 "2018-10-27T17:04:51Z")\
**Posts on this page:** 8\
**Page:** 2

<div class="post-metadata">

**Author:** ![Samarth](https://discourse.itk.org/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@Samarth](https://discourse.itk.org/u/Samarth)\
**Post date:** [August 24, 2023, 9:15pm UTC](https://discourse.itk.org/t/resample-to-same-spacing-and-size-and-align-to-same-origin/1378/21 "2023-08-24T21:15:27Z")

</div>

Hello @zivy,

Thank you for helping me with that clarification. I understand there are subtle differences in the way NIFTI and DICOM data are stored. I was under the impression I would require resampling the segmentation mask to the image grid in order to match (as they are spatially not aligned).

I tried what you suggested and unfortunately the labels still remain misaligned. I wrote the result into a NIFTI file using SITK and visualized it on the ITK tool. I am attaching screenshots of the ITK Image Summary for :

1. Original DICOM Series  
 ![dicom_IS](https://discourse.itk.org/uploads/default/original/2X/4/424b2cee468b901a045a736a27f191635c3561fe.png)

2. Original DICOM Segmentation Mask (Works correctly)  
 ![dicomseg_IS](https://discourse.itk.org/uploads/default/original/2X/8/81cf8bc3dfea3fe6583dc41520d39b6019b3e26e.png)

3)NIFTI Converted Image  
 ![image_IS](https://discourse.itk.org/uploads/default/original/2X/c/ce505d0e3d5c9c107430f2c086acae7706e1f640.png)

1. NIFTI Mask (after modifying with PermuteAxes and setting direction and origin)  
 ![seg_IS_mod](https://discourse.itk.org/uploads/default/original/2X/3/373059cbbb5ba3d51d582fdb20ff75deb868e804.png)

Here is my code that I tried : (seg : Original Segmentation on DICOM, img : NIFTI converted image from DICOM)

```auto
seg_modified = sitk.PermuteAxes(seg,[2,1,0])
seg_modified.SetDirection(img.GetDirection())
seg_modified.SetOrigin(img.GetOrigin())

result = seg_modified

```

Grateful for any further advice on this matter - the mask remains not aligned, and I am unable to figure out why - even though my origin,direction and spacing are appropriate.  
Thanks a lot.

---

<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:** [August 24, 2023, 11:35pm UTC](https://discourse.itk.org/t/resample-to-same-spacing-and-size-and-align-to-same-origin/1378/22 "2023-08-24T23:35:46Z")

</div>

Hello @Samarth,

Can you possibly share the mask and image? Otherwise we are guessing, based on experience, and it is much harder to arrive at the solution.

---

<div class="post-metadata">

**Author:** ![Samarth](https://discourse.itk.org/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@Samarth](https://discourse.itk.org/u/Samarth)\
**Post date:** [August 25, 2023, 4:48pm UTC](https://discourse.itk.org/t/resample-to-same-spacing-and-size-and-align-to-same-origin/1378/23 "2023-08-25T16:48:12Z")

</div>

Hello @zivy ,  
I understand.  
I can share a zoomed in snippet highlighting the region of interest, please find the same attached here (permuted axis/matched origin/matched direction). Please note the segments were created using the ITK platform, and the lesion is highlighted.

Snippet 1 - Aligned Segmentation on DICOM

 ![aligned_snippet](https://discourse.itk.org/uploads/default/original/2X/2/2b855178e65801498eb70a3ab87420773dc8f6ec.png)

Snippet 2 - Misaligned Segmentation on NIFTI

 ![misaligned_snippet](https://discourse.itk.org/uploads/default/original/2X/8/8adee3ff8e56f9474e030b0d20a61551fa9003e5.png)

Thanks for your continued help on this matter - I also wanted to point out , I tried CopyInformation() to ensure metadata matches, but unfortunately that is not of use.

---

<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:** [August 25, 2023, 7:09pm UTC](https://discourse.itk.org/t/resample-to-same-spacing-and-size-and-align-to-same-origin/1378/24 "2023-08-25T19:09:10Z")

</div>

Hello @Samarth,

See if flipping the y axis does it, I believe that is the difference between the DICOM and nii coordinate systems:

```auto
seg_modified = seg[:,::-1,:]

```

---

<div class="post-metadata">

**Author:** ![Samarth](https://discourse.itk.org/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@Samarth](https://discourse.itk.org/u/Samarth)\
**Post date:** [August 25, 2023, 7:19pm UTC](https://discourse.itk.org/t/resample-to-same-spacing-and-size-and-align-to-same-origin/1378/25 "2023-08-25T19:19:12Z")

</div>

Thanks @zivy for your response.  
I wanted to ensure I understand this correctly : permute axes changes the image size from 512_512_240 to 240_512_512. After permuting axes, then I go ahead and flip the same? I believe I would have to flip both x and y, as only Z axis direction is maintained. I shall try and ensure I report the results here.

I found this figure from dcm2niix ([NITRC: dcm2nii: MainPage](https://www.nitrc.org/plugins/mwiki/index.php/dcm2nii:MainPage)) , the tool I utilized for converting DICOM to NIFTI :

![Screenshot 2023-08-25 at 3.16.28 PM](https://discourse.itk.org/uploads/default/original/2X/3/309dda8f5c2e6a81cfb9ec8545f556ba36ccef1d.png)

---

<div class="post-metadata">

**Author:** ![Samarth](https://discourse.itk.org/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@Samarth](https://discourse.itk.org/u/Samarth)\
**Post date:** [September 7, 2023, 12:52am UTC](https://discourse.itk.org/t/resample-to-same-spacing-and-size-and-align-to-same-origin/1378/26 "2023-09-07T00:52:36Z")

</div>

Hello @zivy,

I wanted to update on my problem status - at this point, unfortunately, a simple permute and flipping the axes have not been useful for me. The annotations still remain misaligned, albeit slightly. If there is anything else that may be suggested, I would be grateful - for this seemingly straightforward but currently intractable problem.

---

<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 7, 2023, 1:31am UTC](https://discourse.itk.org/t/resample-to-same-spacing-and-size-and-align-to-same-origin/1378/27 "2023-09-07T01:31:45Z")

</div>

Hello @Samarth,

Unfortunately, at this point the only way we can help is if you provide a dataset exhibiting the issue. This can be an artificial dataset, or you can use publicly available DICOM data to recreate the problem (see [TCIA](https://www.cancerimagingarchive.net/) for publicly available data).

---

<div class="post-metadata">

**Author:** ![Samarth](https://discourse.itk.org/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@Samarth](https://discourse.itk.org/u/Samarth)\
**Post date:** [September 8, 2023, 3:42pm UTC](https://discourse.itk.org/t/resample-to-same-spacing-and-size-and-align-to-same-origin/1378/28 "2023-09-08T15:42:04Z")

</div>

Thank you @zivy for guiding me in that direction. I will be able to use publicly available data to recreate the problem and hope to share it here as soon as I am done. I will be trying to map a single point in space in the DICOM to NIFTY transform.

[Previous page](https://discourse.itk.org/t/resample-to-same-spacing-and-size-and-align-to-same-origin/1378.md?page=1)
