# Resample to same origin, spacing and size as another image.

**URL:** https://discourse.itk.org/t/resample-to-same-origin-spacing-and-size-as-another-image/3031
**Category:** Beginner Questions
**Tags:** python, simpleitk
**Created:** [May 5, 2020, 11:16am UTC](https://discourse.itk.org/t/resample-to-same-origin-spacing-and-size-as-another-image/3031 "2020-05-05T11:16:00Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![acambray](https://discourse.itk.org/letter_avatar_proxy/v4/letter/a/ea666f/32.png) [@acambray](https://discourse.itk.org/u/acambray)
#### Post date: [May 5, 2020, 11:16am UTC](https://discourse.itk.org/t/resample-to-same-origin-spacing-and-size-as-another-image/3031/1 "2020-05-05T11:16:00Z")

</div>

Hi,

I have two nii images, which I load into SimpleITK on Python:

- 3D CT scan
- 3D Segmentation Mask (0s and 1s)

The segmentation mask has the size of the minimum bounding box around the segmentation and therefore is much smaller than the CT Scan, has a different origin and also a different spacing than the CT scan. What I want to accomplish is to transform the segmentation image into an image which has:

- Same size as the CT image
- Same spacing as the CT image
- Same origin as the CT image
- All the extra voxels must be filled with zeros.

Essentially, turn the current segmentation mask into an image where each voxel corresponds to the same same voxel in the CT scan.

How should I go about doing this?

---

<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: [May 5, 2020, 1:00pm UTC](https://discourse.itk.org/t/resample-to-same-origin-spacing-and-size-as-another-image/3031/2 "2020-05-05T13:00:56Z")

</div>

Hello @acambray,

This can be readily done via resampling ([details in section titled resampling](https://github.com/InsightSoftwareConsortium/SimpleITK-Notebooks/blob/master/Python/21_Transforms_and_Resampling.ipynb)):

```auto
sitk.Resample(mask, image, sitk.Transform(), sitk.NearestNeighbor, 0, mask.GetPixelID())

```

The one liner assumes that the CT scan is called `image` the segmentation `mask`, they are aligned in physical space (i.e. transformation is the identity), and we use the nearest-neighbor interpolator so that we do not introduce new labels.

---

<div class="post-metadata">

### Author: ![acambray](https://discourse.itk.org/letter_avatar_proxy/v4/letter/a/ea666f/32.png) [@acambray](https://discourse.itk.org/u/acambray)
#### Post date: [May 5, 2020, 4:49pm UTC](https://discourse.itk.org/t/resample-to-same-origin-spacing-and-size-as-another-image/3031/3 "2020-05-05T16:49:50Z")

</div>

Thanks a lot @zivy, this worked perfectly and I now understand the Resample functionality much more.

---

<div class="post-metadata">

### Author: ![Janet\_Kok](https://discourse.itk.org/user_avatar/discourse.itk.org/janet_kok/32/1788_2.png) [@Janet\_Kok](https://discourse.itk.org/u/Janet_Kok)
#### Post date: [November 15, 2020, 11:54am UTC](https://discourse.itk.org/t/resample-to-same-origin-spacing-and-size-as-another-image/3031/4 "2020-11-15T11:54:46Z")

</div>

@zivy I have a similar problem. Also having 2 nifti images, ct scan and segmentation mask containing **(0-3s)** label.

The CT scan and segmentation mask has the following differences:

1. the origin does not match between the images:  
(-126.67142486572266, -100.2229995727539, -35.8679313659668) \<-CT scan  
(-107.0, -105.69999694824219, -169.0) \<-mask
2. the spacing does not match between the images  
(0.47460898756980896, 0.47460898756980896, 4.999993324279785) \<-CT scan  
(0.4179689884185791, 0.4179689884185791, 5.0) \<-mask
3. the direction does not match between the images  
(0.9999979873288019, 2.2101333702174357e-05, -0.0020062028280818365, 2.210133363 4976865e-05, 0.9997573031831891, 0.022030304063859862, 0.0020062028502736066, -0 .022030302844218676, 0.99975529048516) \<-CT scan  
(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0) \<-mask

I tried the following:  
original\_image = sitk.ReadImage(’./188\_0000.nii.gz’)  
original\_segmentation = sitk.ReadImage(’./188.nii.gz’)

new\_segmentation = sitk.Resample(original\_segmentation, original\_image,sitk.Transform(),sitk.sitkNearestNeighbor,0,original\_segmentation.GetPixelID())

sitk.WriteImage(new\_segmentation,‘188t.nii.gz’)

But the resulting image end up with an empty (all black) image after resampling.

Alternatively, I tried the following and still get all black  
new\_segmentation = sitk.Resample(original\_segmentation, original\_image.GetSize(),  
… sitk.Transform(),  
… sitk.sitkNearestNeighbor,  
… original\_image.GetOrigin(),  
… original\_image.GetSpacing(),  
… original\_image.GetDirection(),  
… 0,  
… original\_segmentation.GetPixelID())

Do you have any solution for this?

Many thanks

---

<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: [November 15, 2020, 5:22pm UTC](https://discourse.itk.org/t/resample-to-same-origin-spacing-and-size-as-another-image/3031/5 "2020-11-15T17:22:03Z")

</div>

Hello @Janet_Kok,

The first approach should work if the segmentation and original image are spatially aligned.

Have you checked if the segmentation and image overlap in space? They may not overlap due to some error in previous processing. This requires knowing the image size for each of them so that we can see what region in space they occupy.

If you can share the two files would be easier for us to help you along.

---

<div class="post-metadata">

### Author: ![AmericaBG](https://discourse.itk.org/user_avatar/discourse.itk.org/americabg/32/1925_2.png) [@AmericaBG](https://discourse.itk.org/u/AmericaBG)
#### Post date: [February 12, 2021, 12:27pm UTC](https://discourse.itk.org/t/resample-to-same-origin-spacing-and-size-as-another-image/3031/7 "2021-02-12T12:27:06Z")

</div>

Hi @Janet_Kok!  
Do you achieve to solve this problem?  
I’m trying to do something similar.

Thank you very much in advance 🙂
