# How to programmatically split the RGB channels and store them separately as a segmentation node

**URL:** <https://discourse.slicer.org/t/how-to-programmatically-split-the-rgb-channels-and-store-them-separately-as-a-segmentation-node/22122>\
**Category:** Support\
**Tags:** segmentation, python\
**Created:** [February 22, 2022, 11:13pm UTC](https://discourse.slicer.org/t/how-to-programmatically-split-the-rgb-channels-and-store-them-separately-as-a-segmentation-node/22122 "2022-02-22T23:13:29Z")\
**Posts on this page:** 1\
**Showing post:** 4

<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:** [February 23, 2022, 6:05am UTC](https://discourse.slicer.org/t/how-to-programmatically-split-the-rgb-channels-and-store-them-separately-as-a-segmentation-node/22122/4 "2022-02-23T06:05:37Z")

</div>

This script creates 3 segments by thresholding each channel, displays the segmentation in 3D, and displays the 3 channels as an RGB image:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/7/9/79c9c5095ba8e4de2943d7e7d9f714d2167075f6.png)

```python
# Inputs
filename = r"c:\Users\andra\OneDrive\Projects\SlicerTesting2022\20220222-Confocal\RGB STACK2.tif"
channelThresholds = [20, 30, 40]

# Install scikit-image
try:
    import skimage
except:
    pip_install('scikit-image')

from skimage import io
import numpy as np

# Read the image into numpy array
im = io.imread(filename)

# Create segments by thresholding each channel

# Create temporary labelmap volume node (it will store the thresholded image that will be imported into the segmentation)
tempLabelmapVolumeNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLLabelMapVolumeNode")
tempLabelmapVolumeNode.CreateDefaultDisplayNodes()
# Do not set a color node ID. This will make segment names set based on the labelmap volume's name (instead of looking up the segment name in the color node)
tempLabelmapVolumeNode.GetDisplayNode().SetAndObserveColorNodeID("")

segmentationNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentationNode")
for component in range(im.shape[3]):
    componentImage = im[:,:,:,component]
    componentThreshold = channelThresholds[component]
    thresholdedComponentImage = np.zeros(componentImage.shape)
    thresholdedComponentImage[componentImage > componentThreshold] = 1
    tempLabelmapVolumeNode.SetName(f"Component-{component}") # the volume node's name will be used as segment name
    slicer.util.updateVolumeFromArray(tempLabelmapVolumeNode, thresholdedComponentImage)
    slicer.modules.segmentations.logic().ImportLabelmapToSegmentationNode(tempLabelmapVolumeNode, segmentationNode)

slicer.mrmlScene.RemoveNode(tempLabelmapVolumeNode)

# Show segmentation in 3D
segmentationNode.CreateClosedSurfaceRepresentation()
segmentationNode.GetDisplayNode().SetOpacity(0.6)

# Show the image as an RGB volume in slice views
volumeNodeRGB = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLVectorVolumeNode", "image-rgb")
slicer.util.updateVolumeFromArray(volumeNodeRGB, im)
slicer.util.setSliceViewerLayers(background=volumeNodeRGB, fit=True)

```

---

_[View the full topic](https://discourse.slicer.org/t/how-to-programmatically-split-the-rgb-channels-and-store-them-separately-as-a-segmentation-node/22122)._
