Default BackgroundValue in erode/dilate filters

#1

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).

(Matt McCormick) #2

Yes! Good catch! :eagle: It “defaults to the minimum of the output image type” . This is defined here. This is defined in the comments for the class. Would you like to contribute a patch?

Your code looks good :+1:. Note that itk.FlatStructuringElement is the preferred way to create a structuring element.

We have a number of convenience Python functions defined in itkExtras.py . We may want to consider a function there that makes it easier to create structuring elements in a more Pythonic way.

#3

Thanks for your answer!

Why not, I’ll read the guidlines and try to submit a PR. However, “defaults to the minimum of the image type” seems to be in conflict with SimpleITK’s procedural interface, sitk.BinaryErode, where double backgroundValue = 0.0. Are the defaults for ITK and SimpleITK not supposed to be the same where possible?

1 Like
(Matt McCormick) #4

SimpleITK deviates from ITK in some places.