Skip to content

Instantly share code, notes, and snippets.

@somada141
Last active August 29, 2015 14:08
Show Gist options
  • Select an option

  • Save somada141/1ac03678975fc6c3b715 to your computer and use it in GitHub Desktop.

Select an option

Save somada141/1ac03678975fc6c3b715 to your computer and use it in GitHub Desktop.
Resample a vtkImageData object using the vtkImageReslice class #python #vtk #imagedata #dicom #visualization

Assuming we have a vtkImageData object under the name of image this is how we'd go about resampling that image data with a factor of 2. Here we assume that image has a spacing of 1.0 mm across all axes

resliceFilter = vtk.vtkImageReslice()
resliceFilter.SetInput(image)
resliceFilter.SetOutputSpacing(0.5, 0.5, 0.5)
resliceFilter.SetInterpolationModeToCubic()
resliceFilter.Update()
imageResampled = resliceFilter.GetOutput()

The resampled vtkImageData object is stored under imageResampled. Note that resampling is achieved by setting the output spacing to 0.5 mm through the SetOutputSpacing method.

While the above snippet uses cubic interpolation, vtkImageReslice offers nearest-neighbor and linear interpolation as well. All it takes is replacing SetInterpolationModeToCubic with SetInterpolationModeToNearestNeighbor or SetInterpolationModeToLinear

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment