Binary segmentation dilation in mm

I am looking to perform dilation of a binary segmentation by a certain mm radius. E.g., dilate the segmentation of the lung by 5-10mm, so I can use it as a registration mask to focus image registration. Is the kernel radius that is set using SetKernelRadius parameter in BinaryDilateImageFilter equivalent to expanding the segmentation by that radius? I.e., for the above case, would I set the kernel radius to 5mm? Thank you!

Hello @tanres,

The kernel radius is specified in pixels, so you would have to compute the conversion from mm to pixels and round it. Note, this assumes the image spacing is isotropic.

import SimpleITK as sitk

foreground_label = 200
segmentation = sitk.Image([256,256], sitk.sitkUInt8)
segmentation[64:128,64:128] = foreground_label
sitk.Show(segmentation, "original")

dilation_in_mm = 10
binary_dilate_filter = sitk.BinaryDilateImageFilter()
binary_dilate_filter.SetForegroundValue(foreground_label)

dilation_in_pixels = round(dilation_in_mm / segmentation.GetSpacing()[0])
binary_dilate_filter.SetKernelRadius(dilation_in_pixels)
sitk.Show(binary_dilate_filter.Execute(segmentation), f"dilated with kernel radius {dilation_in_pixels} spacing, {segmentation.GetSpacing()}")

# a different spacing results in a different dilation_in_pixels value
segmentation.SetSpacing([0.25,0.25])
dilation_in_pixels = round(dilation_in_mm / segmentation.GetSpacing()[0])
binary_dilate_filter.SetKernelRadius(dilation_in_pixels)
sitk.Show(binary_dilate_filter.Execute(segmentation), f"dilated with kernel radius {dilation_in_pixels}, spacing {segmentation.GetSpacing()}")

Another option is to use a distance map and dilation is then in mm and there is no assumption with respect to spacing being isotropic:

import SimpleITK as sitk

foreground_label = 200
segmentation = sitk.Image([256,256], sitk.sitkUInt8)
segmentation[64:128,64:128] = foreground_label
sitk.Show(segmentation, "original")

dilation_in_mm = 10

signed_distance_map = sitk.SignedMaurerDistanceMap(segmentation, squaredDistance=False, useImageSpacing=True)
dilated_binary_image = (signed_distance_map<dilation_in_mm)
sitk.Show(dilated_binary_image* foreground_label)

segmentation.SetSpacing([0.25,0.25])
signed_distance_map = sitk.SignedMaurerDistanceMap(segmentation, squaredDistance=False, useImageSpacing=True)
dilated_binary_image = (signed_distance_map<dilation_in_mm)
sitk.Show(dilated_binary_image* foreground_label)
1 Like