Nearest neighbour interpolation
I seem to be getting a linear interpolation output even when trying to use Nearest neighbour.
Code:
#!/usr/bin/env python3
import vtk
from vtk.util.numpy_support import vtk_to_numpy, numpy_to_vtk
import numpy as np
def vtkToNumpy(data):
np_arr = vtk_to_numpy(data.GetPointData().GetScalars())
dims = data.GetDimensions()
numpy_data = np_arr.reshape(dims[2], dims[1], dims[0])
numpy_data = numpy_data.transpose(2, 1,0)
return numpy_data
def numpyToVTK(data):
flat_data_array = data.transpose(2,1,0).flatten()
vtk_data_array = numpy_to_vtk(flat_data_array)
vtk_data = numpy_to_vtk(num_array=vtk_data_array, deep=True, array_type=vtk.VTK_UNSIGNED_CHAR)
img = vtk.vtkImageData()
img.GetPointData().SetScalars(vtk_data)
img.SetDimensions(data.shape)
img.SetOrigin([0, 0, 0])
img.SetSpacing([1, 1, 1])
return img
def main():
img = np.zeros((5, 5, 5), dtype=np.uint16)
img[1:4, 1:4, 1:3] = 5
img[2:4, 2:4, 4:9] = 11
vtkImg = numpyToVTK(img)
resample = vtk.vtkImageResample()
resample.SetInputData(vtkImg)
resample.SetInterpolationModeToNearestNeighbor()
resample.InterpolateOff()
resample.SetBackgroundColor([0, 0, 0, 0])
resample.SetBackgroundLevel(0)
resample.SetOutputSpacing([0.5, 0.5, 0.5])
resample.Update()
resampleNpImg = vtkToNumpy(resample.GetOutput())
print("\nOriginal size", vtkImg.GetDimensions())
print("Original center", vtkImg.GetOrigin())
print("Original spacing", vtkImg.GetSpacing())
print("\nResampled size", resample.GetOutput().GetDimensions())
print("Resampled center", resample.GetOutput().GetCenter())
print("Resampled spacing", resample.GetOutput().GetSpacing())
print("\noriginalImg", np.unique(img), "\n\n", img)
print("\nresampleNpImg", np.unique(resampleNpImg), "\n\n", resampleNpImg)
if __name__ == "__main__":
main()
Output:
Original size (5, 5, 5)
Original center (0.0, 0.0, 0.0)
Original spacing (1.0, 1.0, 1.0)
Resampled size (9, 9, 9)
Resampled center (2.0, 2.0, 2.0)
Resampled spacing (0.5, 0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