I have a volumetric image stored in a vtkImageData
object for which I would like to change the dimensions (e.g. from [100,100,100] to [120,120,120]). I would like to add zero values in each positive and negative direction separately, such that adding 5 slices in "negative X" and 15 slices in "positive X" still increases the X resolution by 20 in total.
Is there a vtk filter or maybe a numpy reshape function that allows me to do this?
Why am I doing this? I am using python to process a volumetric image. There is a morphological operator in VTK (5.10) that allows me to dilate and erode this image (called vtkImageDilateErode3D
). Unfortunately, it causes artefacts when I dilate/erode voxels close to the image boundary. I therefore want to "extrude" the volume before applying this operator.
Here is some python code that can somewhat reshape an image, but the data is just spread all over the image. Maybe I could somehow copy the previous image into the new array in a smarter way? (For performance reasons I wanted to avoid copying every voxel by hand, though)
def change_vol_resolution(volume, res_adjust=[0,0, 0,0, 0,0]):
changed_volume = vtk.vtkImageData()
original_dimensions = volume.GetDimensions()
changed_dimensions = [original_dimensions[0] + res_adjust[0] + res_adjust[1], original_dimensions[1] + res_adjust[2] + res_adjust[3], original_dimensions[2] + res_adjust[4] + res_adjust[5]]
changed_volume.SetDimensions(changed_dimensions)
changed_volume.SetScalarTypeToShort()
changed_volume.SetNumberOfScalarComponents(volume.GetNumberOfScalarComponents())
changed_volume.AllocateScalars()
np_array = numpy_support.vtk_to_numpy(volume.GetPointData().GetScalars())
np_array_copy = np.copy(np_array)
np_array_copy.resize(changed_dimensions[0]*changed_dimensions[1]*changed_dimensions[2])
changed_volume.GetPointData().SetScalars(numpy_support.numpy_to_vtk(np_array_copy,deep=1))
return changed_volume
UPDATE: I found a numpy function called numpy.pad()
, which allows adding empty "padding" regions in any dimensional direction of a matrix, which is pretty much what I am looking for.