2

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.

Chris
  • 3,245
  • 4
  • 29
  • 53

1 Answers1

2

Since i dont have enough rep to write this as a comment, i'll have to write this as an answer. Although you figured out how to reshape with numpy, there are filters in vtk that can perform that task (maybe it helps because you do not have to copy the image into a numpy array). At the moment ther are three filters and they all subclass from the abstract vtkImagePadFiter. See vtkImagePadFilter Documentation VTK 5.10.

First there is the vtkImageConstantPad, which fills the additional values with a constant. I think this is what you wanted. See vtkImageConstantPad Documentation VTK 5.10.

Second there is the vtkImageMirroPad, which makes an image larger by filling extra pixels with a mirror image of the original image.

Third there is the vtkImageWrapPad, which wraps the existing image via modulo to fill the new pixels/voxels.

P.S My low rep does not allow me to add links to the documentation of the other two filters, bt they are easy to find anyway.

Taron
  • 1,205
  • 2
  • 13
  • 29