# Adding extra padding to a nifti image

**URL:** https://discourse.vtk.org/t/adding-extra-padding-to-a-nifti-image/4038
**Category:** Support
**Created:** [August 21, 2020, 10:10am UTC](https://discourse.vtk.org/t/adding-extra-padding-to-a-nifti-image/4038 "2020-08-21T10:10:53Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![isuruwi](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/i/c0e974/32.png) [@isuruwi](https://discourse.vtk.org/u/isuruwi)
#### Post date: [August 21, 2020, 10:10am UTC](https://discourse.vtk.org/t/adding-extra-padding-to-a-nifti-image/4038/1 "2020-08-21T10:10:53Z")

</div>

I have a nifiti image where the dimension is (200, 200, 320) and what I want to do is that increase the height and width of the image by adding additional padding around it so that finally it contains (512, 512, 320). Is there have a way to pad the nifti image? Any example of that, please ?

---

<div class="post-metadata">

### Author: ![Charles\_Gueunet](https://discourse.vtk.org/user_avatar/discourse.vtk.org/charles_gueunet/32/239_2.png) [@Charles\_Gueunet](https://discourse.vtk.org/u/Charles_Gueunet)
#### Post date: [August 21, 2020, 1:17pm UTC](https://discourse.vtk.org/t/adding-extra-padding-to-a-nifti-image/4038/2 "2020-08-21T13:17:02Z")

</div>

You may be able to do that using the [vtkImageResample](https://vtk.org/doc/nightly/html/classvtkImageResample.html#details) class. You need to set the origin and spacing to be the same as your original nifti image.

---

<div class="post-metadata">

### Author: ![isuruwi](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/i/c0e974/32.png) [@isuruwi](https://discourse.vtk.org/u/isuruwi)
#### Post date: [August 21, 2020, 2:52pm UTC](https://discourse.vtk.org/t/adding-extra-padding-to-a-nifti-image/4038/3 "2020-08-21T14:52:35Z")

</div>

I have applied vtkImageConstantPad to add extra black pixels to the nifiti image as below and saved it. Now it gives (512, 512, 320) dimension.

vtkSmartPointer constantPadFilter =  
vtkSmartPointer::New();  
constantPadFilter-\>SetConstant(0.);  
constantPadFilter-\>SetInputConnection(niftiReader-\>GetOutputPort());  
constantPadFilter-\>SetOutputWholeExtent(-156, 355, -156, 355, 0, 319);  
constantPadFilter-\>Update();

**Should I have to update the origin of the new image after adding extra boundary?**  
**I’m asking this because I apply transformations to this updated image using vtKImageReslice filter as below and I have to add the OutputOrigin there.**

vtkSmartPointer reslice =  
vtkSmartPointer::New();  
reslice-\>SetInputConnection(niftiReader-\>GetOutputPort());  
reslice-\>SetResliceTransform(transform-\>GetInverse());  
reslice-\>SetInterpolationModeToCubic();  
reslice-\>SetOutputSpacing(  
niftiReader-\>GetOutput()-\>GetSpacing()[0],  
niftiReader-\>GetOutput()-\>GetSpacing()[1],  
niftiReader-\>GetOutput()-\>GetSpacing()[2]);  
reslice-\>SetOutputOrigin(  
niftiReader-\>GetOutput()-\>GetOrigin()[0],  
niftiReader-\>GetOutput()-\>GetOrigin()[1],  
niftiReader-\>GetOutput()-\>GetOrigin()[2]);  
reslice-\>SetOutputExtent(niftiReader-\>GetOutput()-\>GetExtent());  
reslice-\>Update();

---

<div class="post-metadata">

### Author: ![fepegar](https://discourse.vtk.org/user_avatar/discourse.vtk.org/fepegar/32/2244_2.png) [@fepegar](https://discourse.vtk.org/u/fepegar)
#### Post date: [August 27, 2020, 8:53am UTC](https://discourse.vtk.org/t/adding-extra-padding-to-a-nifti-image/4038/4 "2020-08-27T08:53:48Z")

</div>

Yes, you need to change the origin. You can check [here](https://github.com/fepegar/torchio/blob/be26d9beccc0194e4d95138a915e5521bbb4ad7e/torchio/data/image.py#L442-L458) how I do it after cropping an image in [TorchIO](https://torchio.readthedocs.io/). The logic for padding is similar. In TorchIO, the [`Pad` transform](https://torchio.readthedocs.io/transforms/preprocessing.html#pad) uses SimpleITK to perform the padding, which takes care of modifying the metadata accordingly.
