# Looping Over Segmentation Algorithm Targets Wrong Segment

**URL:** <https://discourse.slicer.org/t/looping-over-segmentation-algorithm-targets-wrong-segment/12459>\
**Category:** Development\
**Created:** [July 9, 2020, 1:38pm UTC](https://discourse.slicer.org/t/looping-over-segmentation-algorithm-targets-wrong-segment/12459 "2020-07-09T13:38:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vertebra](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/vertebra/32/7475_2.png) [@vertebra](https://discourse.slicer.org/u/vertebra)\
**Post date:** [July 9, 2020, 1:38pm UTC](https://discourse.slicer.org/t/looping-over-segmentation-algorithm-targets-wrong-segment/12459/1 "2020-07-09T13:38:46Z")

</div>

We have got our segmentation algorithm working! Now we are looping over it to allow doctors to segment multiple vertebrae. Whenever we run our code in a for loop, the seed around the second point, F-2(purple in the diagram on the yellow slice), works. However, when we run the effect function, the algorithm works perfectly fine but adds the segmented piece to the first segmentation. Does anyone have any ideas why?

We think the issue is in these lines of code when looping:

segmentationNode.AddSegmentFromClosedSurfaceRepresentation(lumbarSeed.GetOutput(), “Lumbar-” + str(x+1), [random(), random(), random()])  
segmentEditorWidget = slicer.qMRMLSegmentEditorWidget()  
segmentEditorWidget.setMRMLScene(slicer.mrmlScene)  
segmentEditorNode = slicer.vtkMRMLSegmentEditorNode()  
slicer.mrmlScene.AddNode(segmentEditorNode)  
segmentEditorWidget.setMRMLSegmentEditorNode(segmentEditorNode)  
segmentEditorWidget.setSegmentationNode(segmentationNode)  
segmentEditorWidget.setMasterVolumeNode(masterVolumeNode)

 ![Screen Shot 2020-07-09 at 9.35.02 AM](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/4/3/43bfe0104b2e9cf3ba854585c9b3267e3383b204.jpeg)

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [July 9, 2020, 1:48pm UTC](https://discourse.slicer.org/t/looping-over-segmentation-algorithm-targets-wrong-segment/12459/2 "2020-07-09T13:48:59Z")

</div>

> [@vertebra](#):
>
> However, when we run the effect function, the algorithm works perfectly fine but adds the segmented piece to the first segmentation.

This is exactly how the effect is supposed to work: incrementally add more parts to the current segment with each click.

If you want to create a separate segment for each click then create and select a new segment before each click.

---

<div class="post-metadata">

**Author:** ![vertebrae](https://avatars.discourse-cdn.com/v4/letter/v/dec6dc/32.png) [@vertebrae](https://discourse.slicer.org/u/vertebrae)\
**Post date:** [July 9, 2020, 3:20pm UTC](https://discourse.slicer.org/t/looping-over-segmentation-algorithm-targets-wrong-segment/12459/4 "2020-07-09T15:20:43Z")

</div>

> [@vertebra](#):
>
> We thought the new segmentationNode would automatically do that.

We also looked in the class reference linked below but we did not find anything.

[https://apidocs.slicer.org/master/classqMRMLSegmentEditorWidget.html#a07f1984ccaea1b8e49245d4b79be4382](https://apidocs.slicer.org/master/classqMRMLSegmentEditorWidget.html#a07f1984ccaea1b8e49245d4b79be4382)
