Combining multiple segments by name (python)

Hi!

Whenever I have my segmentations for several organs, such as Liver, Lungs, Kidney, I tend to split them into the connected components. I want to make an additional button in the Segment Editor where after splitting them into connected components, that I also am able to combine them.

For example, Lungs and Lungs_2 will become Lungs, Kidney and Kidney_2 will become Kidney etc.

This is what I’ve tried so far and it hasnt worked, can someone help out?

Below, organ_segments is simply:
{Lungs: [“Lungs”, “Lungs_2”], Kidney: [“Kidney”, “Kidney_2”], Liver: [“Liver”]}

segmentation_node = self.editor.segmentationNode()

 self.editor.setActiveEffectByName("Logical operators")
 effect = self.editor.activeEffect()


 for organ in segmented_organs:
            main_segment = organ
            for segment in organ_segments[organ]:
                if segment != organ:
                    self.editor.setCurrentSegmentID(main_segment)
                    effect.setParameter("Operation", "UNION")
                    effect.setParameter("ModifierSegmentID", segment)
                    effect.self().onApply()
                    
                    print("combined:", segment, organ)
                    segmentation_node.RemoveSegment(segment)




You may be running into the fact that the segmentID is not always the same as the segment name, depending on how it was created. This can be a bit confusing because the segmentID is just another string, and it often does coincide with the segment name. However, I think segmentIDs must always be unique within a segmentation, whereas segment names are allowed to be nonunique. Try modifying your code to explicitly get and use the segment IDs rather than relying on the assumption that the segment ID is the same as the segment name. You can get the segment ID from the name using segmentation_node.GetSegmentation().GetSegmentIdBySegmentName(segment_name)
For example, you could change the line getting the main segment id to read main_segment_id = segmentation_node.GetSegmentation().GetSegmentIdBySegmentName(organ)

Okay, thanks for the tips!

Maybe I’m missing something but when I add in your suggestion I seem to be getting a different error. This is the code:

for organ in segmented_organs:
            main_segment = segmentation_node.GetSegmentation().GetSegmentIdBySegmentName(organ)
            
            for segment in organ_segments[organ]:
                if segment != organ:
                    segment_id = segmentation_node.GetSegmentation().GetSegmentIdBySegmentName(segment)
                    
                    self.editor.setCurrentSegmentID(main_segment)
                    self.editor.setActiveEffectByName("Logical operators")
                    effect = self.editor.activeEffect()
                    effect.setParameter("ModifierSegmentID", segment_id)
                    effect.setParameter("Operation", "UNION")
                    effect.setParameter("BypassMasking", 1)
                    effect.self().onApply()

I still get the error: [Python] Operation UNION requires a selected modifier segment

Do you have any suggestions? Thanks in advance!

Give this a try:

for organ in segmented_organs:
            main_segment_id = segmentation_node.GetSegmentation().GetSegmentIdBySegmentName(organ)
            
            for segment in organ_segments[organ]:
                segment_id = segmentation_node.GetSegmentation().GetSegmentIdBySegmentName(segment)
                if segment_id != main_segment_id:
                    self.editor.setCurrentSegmentID(main_segment_id)
                    self.editor.setActiveEffectByName("Logical operators")
                    effect = self.editor.activeEffect()
                    effect.setParameter("ModifierSegmentID", segment_id)
                    effect.setParameter("Operation", "UNION")
                    effect.setParameter("BypassMasking", 1)
                    effect.self().onApply()

It just finishes the process of using segment IDs instead of names. Possibly, you were running into the error when the current segment and the modifier segment were the same. If this doesn’t work, then the error is due to something else.

1 Like

Perfect!! This works, thanks!!

1 Like