ApproximateSignedDistanceMapImageFilter does not support any pixel type

Hi,
I am trying to compute a distance map on a binary 3d shape, using ApproximateSignedDistanceMapImageFilter.
I tried the following code

def get_radius_map(mask):
    dist_filter = sitk.ApproximateSignedDistanceMapImageFilter()
    #dist_filter.SetDebug(True)
    dist_filter.SetInsideValue(1.0)
    dist_filter.SetOutsideValue(0.0)
    img =  sitk.Cast(GetImageFromArray(mask), sitk.sitkFloat64)
    dist_map_shape = dist_filter.Execute(img)
    return GetArrayFromImage(dist_map_shape)

The mask object I pass is a float 64 numpy array of dimension 3, with only 0s and 1s.
I tried to cast into various types, but regardless it will throw the following error (with a different failing pixel type):

RuntimeError: Exception thrown in SimpleITK ApproximateSignedDistanceMapImageFilter_Execute: /tmp/SimpleITK/Code/Common/include/sitkMemberFunctionFactory.hxx:155:
sitk::ERROR: Pixel type: 64-bit float is not supported in 3D by N3itk6simple39ApproximateSignedDistanceMapImageFilterE

I apologize if there is an obvious misuse of the filter, I struggle to find example in python code for this :sweat_smile:
Thank you in advance !

Hello @M-Puig,

The only thing you are missing is that the image type needs to be sitk.sitkUInt8, which is the type for binary images for all SimpleITK code that expects a mask/binary image (see common conventions).

See code below:

import SimpleITK as sitk
import numpy as np

np_mask = np.zeros([128]*3)
np_mask[32:64, 32:64, 32:64] = 1
# convert numpy data from float64 to SimpleITK binary image and cast to uint8 which is the type for
# SimpleITK masks (can be done safely because we know the numpy mask values are 0,1).
sitk_mask = sitk.Cast(sitk.GetImageFromArray(np_mask), sitk.sitkUInt8)

np_radius_map = sitk.GetArrayFromImage(sitk.ApproximateSignedDistanceMap(sitk_mask, insideValue=1, outsideValue=0))
1 Like