# How to co-register mask on neuroimage generated from RTStruct dicom file?

**URL:** https://discourse.itk.org/t/how-to-co-register-mask-on-neuroimage-generated-from-rtstruct-dicom-file/5825
**Category:** Beginner Questions
**Tags:** registration, python, simpleitk
**Created:** [March 23, 2023, 11:35am UTC](https://discourse.itk.org/t/how-to-co-register-mask-on-neuroimage-generated-from-rtstruct-dicom-file/5825 "2023-03-23T11:35:26Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Sunita\_Patel](https://discourse.itk.org/user_avatar/discourse.itk.org/sunita_patel/32/3347_2.png) [@Sunita\_Patel](https://discourse.itk.org/u/Sunita_Patel)
#### Post date: [March 23, 2023, 11:35am UTC](https://discourse.itk.org/t/how-to-co-register-mask-on-neuroimage-generated-from-rtstruct-dicom-file/5825/1 "2023-03-23T11:35:26Z")

</div>

Dear Simple ITK Team,

We have two DICOM series and two RTSTRUCT masks which are converted into Nifti files. The original dimension of the nifti files are (256, 256, 216). We are trying to do registration and resampling to a new dimension (240, 240, 155) using Simple ITK library. When we are doing registration and resampling of the original nifti file into the new dimension (240, 240, 155), the masks are not matching with the tumor. There is mismatch of mask with the tumor image. The mask started 15 slices ahead than it appears in real tumor image.

When we are doing only resampling without registration using Simple ITK the generated mask and tumor in the images are matching.

Why we see such a discrepancy? Are we doing something wrong.

Your guidance will be highly appreciated.

Best regards,  
Sunita

---

<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: [March 23, 2023, 5:33pm UTC](https://discourse.itk.org/t/how-to-co-register-mask-on-neuroimage-generated-from-rtstruct-dicom-file/5825/2 "2023-03-23T17:33:01Z")

</div>

Hello @Sunita_Patel,

If I understand correctly, there are two images and their corresponding segmentations: `fixed_image`, `fixed_image_segmentation`, `moving_image`, `moving_image_segmentation`

You then perform a registration:

```auto
tx = register(fixed_image, moving_image)

```

The transform `tx` maps points from the `fixed_image` coordinate system to the `moving_image` coordinate system. We can use it to resample `moving_image_segmentation` onto the `fixed_image` grid:

```auto
# use nearest neighbor interpolation so we don't introduce labels that weren't in the original data
moving_image_segmentation_resampled = sitk.Resample(moving_image_segmentation, fixed_image, tx, sitk.sitkNearestNeighbor) 

```

If we want to resample the `fixed_image_segmentation` onto the `moving_image` grid we need to invert the transformation, if it is a global transformation call `GetInverse`, otherwise it is slightly more complicated, see [this notebook](https://github.com/InsightSoftwareConsortium/SimpleITK-Notebooks/blob/master/Python/22_Transforms.ipynb):

```auto
tx_inverted = tx.GetInverse()
fixed_image_segmentation_resampled = sitk.Resample(fixed_image_segmentation, moving_image, tx_inverted, sitk.sitkNearestNeighbor)

```

---

<div class="post-metadata">

### Author: ![Sunita\_Patel](https://discourse.itk.org/user_avatar/discourse.itk.org/sunita_patel/32/3347_2.png) [@Sunita\_Patel](https://discourse.itk.org/u/Sunita_Patel)
#### Post date: [March 24, 2023, 9:17am UTC](https://discourse.itk.org/t/how-to-co-register-mask-on-neuroimage-generated-from-rtstruct-dicom-file/5825/3 "2023-03-24T09:17:02Z")

</div>

> [@zivy](#):
>
> `tx`

Hi Zivy,

We tried resampling the way you suggested but got empty mask. We have five nii files to perform the task.

SRI (240, 240, 155)  
T1C\_img (256, 256, 216), T1C\_mask (256, 256, 216)  
Flair\_img (256, 256, 216), Flair\_mask (256, 256, 216)

step 1: We are doing registration and resampling of T1C\_img (moving) with SRI (fixed) to obtained similar dimension as SRI.

step 2: Registration and resampling of T1C\_img (fixed, 240,240,155) with Flair\_img (moving, 256, 256, 216)

step 3: Resampling of T1C\_img (fixed, 240, 240, 155) with T1C\_mask (moving, 256, 256, 216) using transform map obtained from step 1.

step 4: Resampling of Flair\_jmg (fixed, 240, 240, 155) with Flair\_mask (moving, 256, 256, 216) using transform map obtained from step 2.

It is generating the resampled T1C\_mask and Flair\_mask of 240,240,155 dimension but these are essentially empty masks.

Pls suggest where we are doing wrong.

Would you like to go through the code?

Thank you so much Zivy for your guidance.  
Sunita

---

<div class="post-metadata">

### Author: ![Sunita\_Patel](https://discourse.itk.org/user_avatar/discourse.itk.org/sunita_patel/32/3347_2.png) [@Sunita\_Patel](https://discourse.itk.org/u/Sunita_Patel)
#### Post date: [March 25, 2023, 12:52pm UTC](https://discourse.itk.org/t/how-to-co-register-mask-on-neuroimage-generated-from-rtstruct-dicom-file/5825/4 "2023-03-25T12:52:41Z")

</div>

Hi Zivy,

Could you pls guide on how to do registration and resampling of image with mask. I followed your suggestions but got empty mask.

Awaiting your reply.

Best regards,  
Sunita

---

<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: [March 27, 2023, 1:58pm UTC](https://discourse.itk.org/t/how-to-co-register-mask-on-neuroimage-generated-from-rtstruct-dicom-file/5825/5 "2023-03-27T13:58:54Z")

</div>

Hello @Sunita_Patel,

If I understand your goal, you would like to transform all the information to be coincident with the spatial grid of the SRI image:

```auto
# register sri and t1c image
tx_sri_t1c = register(fixed_image=sri, moving_image=t1c_image)

# register sri and flair image
tx_sri_flair = register(fixed_image=sri, moving_image=flair_image)

# if for some reason you have to register the t1c to flair and can't do the sri to flair directly you can obtain it as below
#tx_t1c_flair = register(fixed_image=t1c_image, moving_image=flair_image)
#tx_sri_flair = sitk.CompositeTransform([tx_t1c_flair, tx_sri_t1c])

t1c_image_resampled = sitk.Resample(t1c_image, sri, tx_sri_t1c )
t1c_mask_resampled = sitk.Resample(t1c_mask, sri, tx_sri_t1c, sitk.sitkNearestNeighbor)

flair_image_resampled = sitk.Resample(flair_image, sri, tx_sri_flair )
flair_mask_resampled = sitk.Resample(flair_mask, sri, tx_sri_flair, sitk.sitkNearestNeighbor)

```
