Hi, I am trying to use SimpleITK in Python to dilate all non-zero regions in a 3D label image while keeping the image’s original label values.
I tried following this Multi-label Morphology example but sitk.MorphologicalWatershedFromMarkers() function kept assigning new label values to each region even if I change all label values to fall into the range 0-255 (by using sitk.ChangeLabel) beforehand.
The labels of the output image are the label of the marker image.
So I am not too sure why the function’s assigning new label values to each region instead of keeping market image’s label values. Does anyone know how to fix this? And please correct me if my understanding of the function is wrong.
Also, is there a faster way to achieve my purpose (multi-label dilation that keeps original labels) without going through BinaryDilate, MorphologicalWatershedFromMarkers, etc?
Not sure why you are looking into the MorphologicalWatershedFromMarkers if what you want is dilation. For dilation the relevant filter is BinaryDilateImageFilter. Please see code below:
import SimpleITK as sitk
# Did some sort of segmentation and obtained an image
# with two arbitrary labels
seg_image = sitk.Image([128]*3, sitk.sitkUInt8)
seg_image[20:30, 30:50, 40:70] = 2
seg_image[40:70, 30:50, 20:30] = 8
sitk.WriteImage(seg_image,'seg_image.nrrd')
# Get all label values, assume all non zero values are label
np_arr_view = sitk.GetArrayViewFromImage(seg_image)
unique_values = set(np_arr_view[np_arr_view!=0])
dilated_image = seg_image
dilate_filter = sitk.BinaryDilateImageFilter()
dilate_filter.SetKernelRadius(3)
dilate_filter.SetKernelType(sitk.sitkBall)
for label in unique_values:
dilate_filter.SetForegroundValue(int(label))
dilated_image = dilate_filter.Execute(dilated_image)
sitk.WriteImage(dilated_image,'dilated_seg_image.nrrd')
@zivy thank you for the reply. I was looking into MorphologicalWatershedFromMarkers since I was hoping to do a simultaneous dilation of all labeled regions. I think I figured out how to keep label values from the market image. It wasn’t working before since I was passing a label map that had values outside of the range 0-255 to the SimpleITK.ChangeLabels.
Thank you @blowekamp. I will also try using LabelSetDilateImageFilter and compare the runtime performance with the other approach (Multi-label Morphology).