# Resampling image

**URL:** https://discourse.itk.org/t/resampling-image/4793
**Category:** Algorithms
**Tags:** python, dicom, simpleitk
**Created:** [February 8, 2022, 9:02am UTC](https://discourse.itk.org/t/resampling-image/4793 "2022-02-08T09:02:10Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![tutti](https://discourse.itk.org/letter_avatar_proxy/v4/letter/t/f4b2a3/32.png) [@tutti](https://discourse.itk.org/u/tutti)
#### Post date: [February 8, 2022, 9:02am UTC](https://discourse.itk.org/t/resampling-image/4793/1 "2022-02-08T09:02:10Z")

</div>

Hello everyone,  
I try to resample image 1 (nii) [128,128,128], spacing [1,1,1] to image2 [512,512,476], spacing2 [0.6,0.6,0.6], but I got image as following.

 ![re](https://discourse.itk.org/uploads/default/original/2X/6/6a67989d147c40cbd75e62d5c4de3153471a9a0d.png)

```auto
def resample_img(itk_image1, itk_image2, out_spacing=[0.6, 0.6, 0.6]):
    original_spacing = itk_image2.GetSpacing()
    original_size = itk_image1.GetSize()
    out_size = itk_image2.GetSize()
# int(np.round(original_size[0] * (original_spacing[0] / out_spacing[0]))),
# int(np.round(original_size[1] * (original_spacing[1] / out_spacing[1]))),
# int(np.round(original_size[2] * (original_spacing[2] / out_spacing[2])))]  
    resample = sitk.ResampleImageFilter()
    resample.SetOutputSpacing(out_spacing)
    resample.SetSize(out_size)
    resample.SetOutputDirection(itk_image2.GetDirection())
    resample.SetOutputOrigin(itk_image2.GetOrigin())
    resample.SetTransform(sitk.Transform())
    resample.SetDefaultPixelValue(itk_image2.GetPixelIDValue())
    
    resample.SetInterpolator(sitk.sitkBSpline)
# resample.SetDefaultPixelValue(-1000) 
    
    return resample.Execute(itk_image)
re = resample_img(image1, image2)
sitk.WriteImage(re, 're.nii')

```

Did I miss something? 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: [February 8, 2022, 2:12pm UTC](https://discourse.itk.org/t/resampling-image/4793/2 "2022-02-08T14:12:16Z")

</div>

Hello @tutti,

The resampling filter allows you to use an arbitrary grid, which in this case doesn’t match the size you actually want, it occupies a larger physical region than the original image grid. The physical image extent, between center of first voxel and last, is defined by (size-1)\*spacing. To define grids that cover the same physical extent you can define the output spacing and derive the output size or define the output size and derive the output spacing:

```auto
out_spacing = [0.6,0.6,0.6]
out_size = [int(round(osz * ospc / out_spacing)) for osz, ospc in zip(original_size, original_spacing)]

```

or

```auto
out_size = [512,512,476]
out_spacing = [((osz - 1) * ospc) / (nsz - 1) for ospc, osz, nsz in zip(original_spacing, original_size, out_size)]

```

Treating the image as a physical object is a fundamental principal of ITK/SimpleITK. For further details see [this SimpleITK read-the-docs page](https://simpleitk.readthedocs.io/en/master/fundamentalConcepts.html).

---

<div class="post-metadata">

### Author: ![tutti](https://discourse.itk.org/letter_avatar_proxy/v4/letter/t/f4b2a3/32.png) [@tutti](https://discourse.itk.org/u/tutti)
#### Post date: [February 9, 2022, 1:15am UTC](https://discourse.itk.org/t/resampling-image/4793/3 "2022-02-09T01:15:04Z")

</div>

Hi @zivy,

That is what I am looking for, I understand now.  
Thank you for your help! :))

Best regards,

---

<div class="post-metadata">

### Author: ![YinCH](https://discourse.itk.org/letter_avatar_proxy/v4/letter/y/5f8ce5/32.png) [@YinCH](https://discourse.itk.org/u/YinCH)
#### Post date: [March 14, 2022, 9:30am UTC](https://discourse.itk.org/t/resampling-image/4793/4 "2022-03-14T09:30:30Z")

</div>

Hello @zivy ,  
I try to resample image 1 size[512,512,334], spacing [0.73046875, 0.73046875, 2.0] to image2 spacing2 [0.73046875, 0.73046875, 0.8], by calculating the proportion, the image size should be [512,512,835]. But I got the top layer of the 3D up-sampled image:

 ![屏幕截图 2022-03-14 171942](https://discourse.itk.org/uploads/default/original/2X/1/179c3de540983d4a0870578efda750b018a4bd11.jpeg)  
 ![屏幕截图 2022-03-14 171957](https://discourse.itk.org/uploads/default/original/2X/6/6e75510353c1ed82082cf10d9a96dc60db36edb3.jpeg)  
def resize\_image\_itk(itkimage, newSpacing, originSpcaing, resamplemethod=sitk.sitkNearestNeighbor):  
newSpacing = np.array(newSpacing, float)  
resampler = sitk.ResampleImageFilter()  
originSize = itkimage.GetSize()  
factor = newSpacing / originSpcaing  
newSize = originSize / factor  
newSize = newSize.astype(np.int)  
resampler.SetReferenceImage(itkimage)  
resampler.SetOutputSpacing(newSpacing.tolist())  
resampler.SetSize(newSize.tolist())  
resampler.SetTransform(sitk.Transform(3, sitk.sitkIdentity))  
resampler.SetInterpolator(resamplemethod)  
itkimgResampled = resampler.Execute(itkimage)  
return itkimgResampled

def DownsamplingDicomFixedResolution():  
src\_path = “inputpath”  
result\_path = “outputpath”  
path\_list = os.listdir(src\_path)  
newthickspacing = 0.8  
for subsetindex in path\_list:  
img = sitk.ReadImage(src\_path + path\_list[0])  
Spacing = img.GetSpacing()  
print(Spacing)  
thickspacing, widthspacing, heightspacing = Spacing[2], Spacing[0], Spacing[1]  
srcitk = resize\_image\_itk(img, newSpacing=(widthspacing, heightspacing, newthickspacing),  
originSpcaing=(widthspacing, heightspacing, thickspacing),  
resamplemethod=sitk.sitkLinear)  
print(src\_path + str(subsetindex) + “.nii.gz”)  
sitk.WriteImage(srcitk, result\_path + str(subsetindex) + “.nii.gz”)

DownsamplingDicomFixedResolution()

I can get the right result this way:  
out\_size = [512,512,835]  
out\_spacing = [((osz - 1) \* ospc) / (nsz - 1) for ospc, osz, nsz in zip(original\_spacing, original\_size, out\_size)]  
But can’t get it right the other way:  
out\_spacing = [0.73046875, 0.73046875, 0.8]  
out\_size = [int(round(osz \* ospc / out\_spacing)) for osz, ospc in zip(original\_size, original\_spacing)]

Did I miss something? 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: [March 14, 2022, 2:47pm UTC](https://discourse.itk.org/t/resampling-image/4793/5 "2022-03-14T14:47:00Z")

</div>

Hello @YinCH,

You didn’t miss anything, the second option specified a grid that is larger than the original image. When you have a larger image the voxels outside of the original image’s physical extent are assigned a fixed value (default is zero which is what you see, for CT you can use -1000 HU value for air).

When specifying a new spacing for resampling, the image’s physical size will most often change because the derived number of pixels is a  
discrete value (integer). If you want to be closer to the original size, try the following:

```auto
original_size = [512,512,334]
original_spacing = [0.73046875, 0.73046875, 2.0]
out_spacing = [0.73046875, 0.73046875, 0.8]
out_size = [int(round(1+(osz-1) * ospc / out_spc)) for out_spc, osz, ospc in zip(out_spacing, original_size, original_spacing)]

```

---

<div class="post-metadata">

### Author: ![YinCH](https://discourse.itk.org/letter_avatar_proxy/v4/letter/y/5f8ce5/32.png) [@YinCH](https://discourse.itk.org/u/YinCH)
#### Post date: [March 15, 2022, 1:43am UTC](https://discourse.itk.org/t/resampling-image/4793/6 "2022-03-15T01:43:17Z")

</div>

Hello,@zivy,  
Thank you very much for your answer. According to the formula logic you gave, the experiment is correct. Thank you for your help.  
Meanwhile, I would like to ask you which file of SITK is the source code for implementing this algorithm? What is the function name? So I can try to learn the logic of implementing this algorithm in the source code.  
Thank you again for your answer and help, which is very useful to me.

---

<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 15, 2022, 12:58pm UTC](https://discourse.itk.org/t/resampling-image/4793/7 "2022-03-15T12:58:48Z")

</div>

Hello @YinCH,

The original [source code](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Modules/Filtering/ImageGrid/include/itkResampleImageFilter.hxx) is in ITK, and SimpleITK is wrapping that with a simplified interface. The SimpleITK code is automatically generated from the [JSON description](https://github.com/SimpleITK/SimpleITK/blob/aff11ddc90bde4cc1669bb54a60dec52d50c0507/Code/BasicFilters/json/ResampleImageFilter.json).

You can go over the ITK code to understand how the resampling is done, this isn’t an easy read as the code is written to perform resampling in n-Dimensions and uses other classes to do the interpolation.

---

<div class="post-metadata">

### Author: ![YinCH](https://discourse.itk.org/letter_avatar_proxy/v4/letter/y/5f8ce5/32.png) [@YinCH](https://discourse.itk.org/u/YinCH)
#### Post date: [March 23, 2022, 3:30am UTC](https://discourse.itk.org/t/resampling-image/4793/8 "2022-03-23T03:30:33Z")

</div>

Hello,@zivy,  
Thank you very much for your answer. I have solved the problem well.  
If you have any new questions, please kindly comment.
