I am trying to erode a binary image using the Pythonic interface (using ITK 5.0 rc2).
From BinaryErodeImageFilter
’s documentation:
The eroded pixels will receive the BackgroundValue (defaults to 0).
This doesn’t seem to be true in the following example:
import numpy as np
import itk
test_array = np.zeros((10, 10, 10), dtype=np.int16)
test_array[4:7, 4:7, 4:7] = 1
img = itk.image_from_array(test_array)
StructuringElementType = itk.FlatStructuringElement[3]
structuringElement = StructuringElementType.Ball(1)
img1 = itk.binary_erode_image_filter(img, kernel=structuringElement, erode_value=1)
img1_np = itk.array_view_from_image(img1)
print(img1_np.min(), img1_np.max())
Result: -32768 1
. I was expecting 0 1
. This can be fixed by passing background_value=0
as an additional argument to binary_erode_image_filter
. Is this a typo in the documentation, which should say “defaults to the minimum of the image type” (in this case SHORT), or is this behaviour not intended?
Bonus question: how can I define the structuring element in a more Pythonic way? itk.BinaryBallStructuringElement
doesn’t seem to be accesible from Python. Something like kernel=itk.BinaryBallStructuringElement(radius)
would be welcome (dimension would be inferred from the input image).