# One viewer per image + segmentation

**URL:** <https://discourse.slicer.org/t/one-viewer-per-image-segmentation/35059>\
**Category:** Support\
**Tags:** segmentation, python, viewer\
**Created:** [March 25, 2024, 3:49pm UTC](https://discourse.slicer.org/t/one-viewer-per-image-segmentation/35059 "2024-03-25T15:49:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rkraaijveld](https://avatars.discourse-cdn.com/v4/letter/r/b9bd4f/32.png) [@rkraaijveld](https://discourse.slicer.org/u/rkraaijveld)\
**Post date:** [March 25, 2024, 3:49pm UTC](https://discourse.slicer.org/t/one-viewer-per-image-segmentation/35059/1 "2024-03-25T15:49:15Z")

</div>

Hi!

I have three separate images with corresponding segmentations and colour tables. I want to make three viewers, viewing the image in an axial direction.

Until now, I’m able to get one image in each viewer, but the image+segmentation in each viewer has been unsuccessful, also in axial orientation. Can anyone help?

Thanks!

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [March 25, 2024, 3:51pm UTC](https://discourse.slicer.org/t/one-viewer-per-image-segmentation/35059/2 "2024-03-25T15:51:45Z")

</div>

You can look at the [CompareVolume](https://slicer.readthedocs.io/en/5.6/user_guide/modules/comparevolumes.html) logic for ideas. It does some of what you describe but doesn’t handle segmentations explicitly.

---

<div class="post-metadata">

**Author:** ![rkraaijveld](https://avatars.discourse-cdn.com/v4/letter/r/b9bd4f/32.png) [@rkraaijveld](https://discourse.slicer.org/u/rkraaijveld)\
**Post date:** [March 25, 2024, 4:15pm UTC](https://discourse.slicer.org/t/one-viewer-per-image-segmentation/35059/3 "2024-03-25T16:15:25Z")

</div>

Hi! Thanks for such a quick response!

I looked into that but for some reason all my segmentations are still visibly in each viewer. I tried to do something like this but it didnt work:

```
#put one of the volumes into each view, or none if it should be blank
sliceNodesByViewName = {}
layoutManager = slicer.app.layoutManager()

for index in range(len(actualViewNames)):
  viewName = actualViewNames[index]
  try:
    volumeNodeID = volumeNodes[index].GetID()
    segNodeID = label[index].GetID()
  except IndexError:
    volumeNodeID = ""

  sliceWidget = layoutManager.sliceWidget(viewName)
  compositeNode = sliceWidget.mrmlSliceCompositeNode()
  

  if label:
    compositeNode.SetBackgroundVolumeID(volumeNodeID)
    compositeNode.SetLabelVolumeID(segNodeID)
    
  else:
    compositeNode.SetBackgroundVolumeID(volumeNodeID)

  sliceNode = sliceWidget.mrmlSliceNode()
  sliceNode.SetOrientation(orientation)
  sliceNodesByViewName[viewName] = sliceNode

```

---

<div class="post-metadata">

**Author:** ![rkraaijveld](https://avatars.discourse-cdn.com/v4/letter/r/b9bd4f/32.png) [@rkraaijveld](https://discourse.slicer.org/u/rkraaijveld)\
**Post date:** [March 25, 2024, 4:27pm UTC](https://discourse.slicer.org/t/one-viewer-per-image-segmentation/35059/4 "2024-03-25T16:27:09Z")

</div>

I wanted to start over, so this is what I have at the moment:

slicer.mrmlScene.Clear(0)  
# Reset the slice views to clear any remaining segmentations  
file\_path = self.nifti\_files[self.current\_index]

```
    self.volume0_node = slicer.util.loadVolume(file_path[0]["image"])
    self.volume1_node = slicer.util.loadVolume(file_path[1]["image"])
    self.volume2_node = slicer.util.loadVolume(file_path[2]["image"])

    self.label0_node = slicer.util.loadColorTable(file_path[0]["label"])
    self.label1_node = slicer.util.loadColorTable(file_path[1]["label"])
    self.label2_node = slicer.util.loadColorTable(file_path[2]["label"])

    self.segmentation0_node = slicer.util.loadSegmentation(file_path[0]["segmentation"], {'colorNodeID': self.label0_node.GetID()})
    self.segmentation1_node = slicer.util.loadSegmentation(file_path[1]["segmentation"], {'colorNodeID': self.label1_node.GetID()})
    self.segmentation2_node = slicer.util.loadSegmentation(file_path[2]["segmentation"], {'colorNodeID': self.label2_node.GetID()})

    sliceNodes = slicer.util.getNodes('vtkMRMLSliceNode*')

    layoutManager = slicer.app.layoutManager()
    sliceWidget = layoutManager.sliceWidget('Red')
    sliceNode = sliceWidget.mrmlSliceNode()
    sliceNode.SetOrientation('Axial')
    compositeNode = sliceWidget.mrmlSliceCompositeNode()
    compositeNode.SetBackgroundVolumeID(self.volume0_node.GetID())
    compositeNode.SetLabelVolumeID(self.segmentation0_node.GetID())

    layoutManager = slicer.app.layoutManager()
    sliceWidget = layoutManager.sliceWidget('Green')
    sliceNode = sliceWidget.mrmlSliceNode()
    sliceNode.SetOrientation('Axial')
    compositeNode = sliceWidget.mrmlSliceCompositeNode()
    compositeNode.SetBackgroundVolumeID(self.volume1_node)
    compositeNode.SetLabelVolumeID(self.segmentation1_node)

    layoutManager = slicer.app.layoutManager()
    sliceWidget = layoutManager.sliceWidget('Yellow')
    sliceNode = sliceWidget.mrmlSliceNode()
    sliceNode.SetOrientation('Axial')
    compositeNode = sliceWidget.mrmlSliceCompositeNode()
    compositeNode.SetBackgroundVolumeID(self.volume2_node)
    compositeNode.SetLabelVolumeID(self.segmentation2_node)

    slicer.util.resetSliceViews()

```

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [March 25, 2024, 7:03pm UTC](https://discourse.slicer.org/t/one-viewer-per-image-segmentation/35059/5 "2024-03-25T19:03:27Z")

</div>

Segmentations aren’t managed by the `vtkMRMLSliceCompositeNode` so you need to manage their visibility using the list of view node ids in their display nodes, [something like this](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#change-markup-point-list-display-properties).
