vtkImageDilateErode3D
uses an elliptical footprint. But, for a 3x3 kernel, it seems that the ellipse is approximated as a square (or cube for 3D). Is this a bug? I would have expected more of a cross (i.e. rounded corners).
Output from the code below:
Input image:
[[ 0 0 0 0 0]
[ 0 0 0 0 0]
[ 0 0 255 0 0]
[ 0 0 0 0 0]
[ 0 0 0 0 0]]
Dilated image:
[[ 0 0 0 0 0]
[ 0 255 255 255 0]
[ 0 255 255 255 0]
[ 0 255 255 255 0]
[ 0 0 0 0 0]]
Expected output:
Dilated image:
[[ 0 0 0 0 0]
[ 0 0 255 0 0]
[ 0 255 255 255 0]
[ 0 0 255 0 0]
[ 0 0 0 0 0]]
import vtk
import numpy as np
# Create a 5x5 image with one bright pixel in the center
size = 5
image = vtk.vtkImageData()
image.SetDimensions(size, size, 1)
image.AllocateScalars(vtk.VTK_UNSIGNED_CHAR, 1)
# Fill background with 0
for y in range(size):
for x in range(size):
image.SetScalarComponentFromFloat(x, y, 0, 0, 0)
# Set center pixel to 255
image.SetScalarComponentFromFloat(size // 2, size // 2, 0, 0, 255)
# Dilate image
dilate = vtk.vtkImageDilateErode3D()
dilate.SetInputData(image)
dilate.SetKernelSize(3, 3, 1)
dilate.SetDilateValue(255)
dilate.SetErodeValue(0)
dilate.Update()
# Show input and output arrays
def vtk_to_array(img):
dims = img.GetDimensions()
arr = np.zeros((dims[1], dims[0]), dtype=np.uint8)
for y in range(dims[1]):
for x in range(dims[0]):
arr[y, x] = int(img.GetScalarComponentAsFloat(x, y, 0, 0))
return arr
input_arr = vtk_to_array(image)
output_arr = vtk_to_array(dilate.GetOutput())
print("Input image:")
print(input_arr)
print("\nDilated image:")
print(output_arr)