How to get a smoother region combining all the islands of my segmentationmask?


As shown here, I have multiple components of small regions. I don’t want to remove any islands. But I want to combine all of them into a single region.
I have been using SimpleITK for a while and tried various hole filling operations but failed to do this task. Is there any way to do this in simpleitk or any other python libraries?

Hello @unicorn,

Possibly use a distance map and binary search to yield tightest bound on object:

con_comp_filter = sitk.ConnectedComponentImageFilter()
distance_map = sitk.SignedMaurerDistanceMap(image==label_value, squaredDistance=False, useImageSpacing=True)
# largest_reasonable_threshold should yield a single connected component, the other bound for the 
# binary search is zero as we know it yields multiple components
binary_label_image = (distance_map<largest_reasonable_threshold)
con_comp_filter.Execute(binary_label_image)
number_of_comp = con_comp_filter.GetObjectCount()
# binary search for the lowest threshold value in [0,largest_reasonable_threshold] which produces a single component 

Having given the pseudo-code above, I am not 100% sure the situation is as you describe it. While it looks like there are multiple components, they visually appear to have the same label which raises my suspicion that we are looking at a 2D slice of a 3D dataset. Actually all these “small components” are really part of a single large component in 3D. In this case the binary search approach won’t work as we are already starting with a single component. If this is the case, then you can still use the distance map approach with a threshold, but then selection of the threshold may not be straightforward. If this is the actual situation, I would advise as a first step to see if a semi-automated, slider based user selection of threshold does what you want.

To get a tightly wrapped bounding surface over a set of islands, you can use the “shrinkwrap” algorithm.

I’m not aware of any ITK implementation, but I know there is an excellent 3D implementation in 3D Slicer’s SurfaceWrapSolidify extension. It is fully implemented in Python and VTK. You can run it via GUI or Python (using 3D Slicer’s Python environment, along with all your other Python code), or copy-paste relevant parts of the source code into your processing script.