# Showing Segmentation on Top of A Volume

**URL:** https://discourse.slicer.org/t/showing-segmentation-on-top-of-a-volume/18715
**Category:** Development
**Tags:** segmentation, python
**Created:** [July 14, 2021, 2:12am UTC](https://discourse.slicer.org/t/showing-segmentation-on-top-of-a-volume/18715 "2021-07-14T02:12:22Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![ColdFour](https://avatars.discourse-cdn.com/v4/letter/c/858c86/32.png) [@ColdFour](https://discourse.slicer.org/u/ColdFour)
#### Post date: [July 14, 2021, 2:12am UTC](https://discourse.slicer.org/t/showing-segmentation-on-top-of-a-volume/18715/1 "2021-07-14T02:12:22Z")

</div>

Hello all,

I’m currently making an extension that has some heart-related features, I give the currently displayed VolumeNode array to a Deep Learning model to get an array of the heart’s segmentation, then load it into a LabelMapVolume to create a segmentation node from it.

I want it to show the segmentation on top of the volume containing the heart, however, the only way that I get the segmentation to show is by using the GUI to hide the volume, then toggling the Label or Segmentation, unhiding the Volume hides the segmentation/label.

```auto
# Create a new LabelMapVolume
LabelMapVolumeNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLLabelMapVolumeNode', f'{Name}-Label')

# Update the LabelMapVolume from the given Segmentation array
slicer.util.updateVolumeFromArray(LabelMapVolumeNode, Segmentation)

# Create a SegmentationNode
SegNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentationNode", f'{Name}-Segmentation')

# Load the LabelMapVolume into the SegmentationNode 
slicer.modules.segmentations.logic().ImportLabelmapToSegmentationNode(LabelMapVolumeNode, SegNode)

# Create Closed Surface Representation
if HeartSegVis:
    SegNode.CreateClosedSurfaceRepresentation()

```

Any help will be much appreciated, sorry for the long explanation

Thanks,

---

<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 14, 2021, 2:42am UTC](https://discourse.slicer.org/t/showing-segmentation-on-top-of-a-volume/18715/2 "2021-07-14T02:42:58Z")

</div>

You have only copied the voxel data. You must also need to copy the IJKToRAS matrix from the original volume into the labelmap volume (it specifies the image origin, spacing, and axis directions).

---

<div class="post-metadata">

### Author: ![ColdFour](https://avatars.discourse-cdn.com/v4/letter/c/858c86/32.png) [@ColdFour](https://discourse.slicer.org/u/ColdFour)
#### Post date: [July 14, 2021, 10:54pm UTC](https://discourse.slicer.org/t/showing-segmentation-on-top-of-a-volume/18715/3 "2021-07-14T22:54:19Z")

</div>

This solved the problem, thanks!  
This is the updated code for refrence.

```auto
# Get IJKToRAS Matrix
VolumeIJKToRAS = vtk.vtkMatrix4x4()
InputVolumeNode.GetIJKToRASMatrix(VolumeIJKToRAS)

# Create a new LabelMapVolume
LabelMapVolumeNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLLabelMapVolumeNode', f'{Name}-Label')

# Set IJKToRAS Matrix To That of The Original Volume
LabelMapVolumeNode.SetIJKToRASMatrix(VolumeIJKToRAS)

# Update the LabelMapVolume from the given Segmentation array
slicer.util.updateVolumeFromArray(LabelMapVolumeNode, Segmentation)

# Create a SegmentationNode
SegNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentationNode", f'{Name}-Segmentation')

# Load the LabelMapVolume into the SegmentationNode 
slicer.modules.segmentations.logic().ImportLabelmapToSegmentationNode(LabelMapVolumeNode, SegNode)

# Create Closed Surface Representation
if HeartSegVis:
    SegNode.CreateClosedSurfaceRepresentation()

```
