Filling Contours In A Volume

Hello,

I am currently working on a module that segments based on Edge Detection using SimpleITK filters. The results I’m getting are satisfying, however, I’m only segmenting the contours themselves and not the whole surface delimited by those contours. I’ve tried several SimpleITK filters to be able to fill the contours but with no luck. Does anyone have any tips on how to proceed or what methods to use?

Here is my current segmentation:
image

And here is how I would like it to look:
image

Here is the code that I use to create that first segmentation:

def draw_contours(self):
    # Get the output of the Canny filter
    inputImage = sitkUtils.PullVolumeFromSlicer(self.outputVolumeNode3)
    
    caster = sitk.CastImageFilter()
    # caster.SetOutputPixelType(sitk.sitkInt8) # Pour le Fillhole Filter
    caster.SetOutputPixelType(sitk.sitkFloat64) # Pour le Threshold Filter
    inputImage = caster.Execute(inputImage)

    # Convert the image to a binary mask
    binaryFilter = sitk.BinaryThresholdImageFilter()
    binaryFilter.SetLowerThreshold(1)  # adjust this value based on your needs
    binaryFilter.SetUpperThreshold(255)  # adjust this value based on your needs
    # binaryFilter = sitk.BinaryFillholeImageFilter()
    # print(binaryFilter.GetForegroundValue())
    # binaryFilter.SetFullyConnected(False)
    print("Lower: ", binaryFilter.GetLowerThreshold())
    print("Upper: ", binaryFilter.GetUpperThreshold())

    binaryImage = binaryFilter.Execute(inputImage)
    
    # Push the binary image to a new Slicer node
    binaryVolumeNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLLabelMapVolumeNode", "BinaryMask")
    sitkUtils.PushVolumeToSlicer(binaryImage, binaryVolumeNode)

    # Create a new segmentation node
    segmentationNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentationNode")

    print(type(binaryVolumeNode))
    print(type(segmentationNode))

    # Import the binary mask to the segmentation node
    slicer.modules.segmentations.logic().ImportLabelmapToSegmentationNode(binaryVolumeNode, segmentationNode)

    # Now you can visualize the segmentation in 3D Slicer
    slicer.util.setSliceViewerLayers(background=self.inputVolumeNode, label=segmentationNode)

    print(binaryVolumeNode.GetImageData().GetDimensions())

Thank you in advance