Adding extra padding to a nifti image

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 ?

You may be able to do that using the vtkImageResample class. You need to set the origin and spacing to be the same as your original nifti image.

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();

Yes, you need to change the origin. You can check here how I do it after cropping an image in TorchIO. The logic for padding is similar. In TorchIO, the Pad transform uses SimpleITK to perform the padding, which takes care of modifying the metadata accordingly.