# How to overlay more than two volumes

**URL:** <https://discourse.slicer.org/t/how-to-overlay-more-than-two-volumes/8860>\
**Category:** Support\
**Created:** [October 22, 2019, 3:33pm UTC](https://discourse.slicer.org/t/how-to-overlay-more-than-two-volumes/8860 "2019-10-22T15:33:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sthirumal](https://avatars.discourse-cdn.com/v4/letter/s/a5b964/32.png) [@sthirumal](https://discourse.slicer.org/u/sthirumal)\
**Post date:** [October 22, 2019, 3:33pm UTC](https://discourse.slicer.org/t/how-to-overlay-more-than-two-volumes/8860/1 "2019-10-22T15:33:32Z")

</div>

Operating system: Windows 10  
Slicer version: 4.10.1

I am wondering if there is a way to overlay more than just two volumes. I know we can overlay two by setting them to foreground and background, but is there a way to add more layers? Reason being, I am using Slicer to look at 2D cell images, where each image highlights different proteins in the cell.

Expected behavior:

 ![expected%20output%20-%20slicer%20forum](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/5/9/593ae7e8ca99af9e1d3f9757df2b5a75be84bf33.png)

The above is 3 images overlaid together, which is what I would like to be able to do. (This image was created in a different program)

Actual behavior:

 ![actual%20output%20-%20slicer%20forum](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/b/d/bda6a3f1ff24bd39fc61596e912b54765e76b953.png)

The above image is from Slicer with only 2 images overlaid.

---

<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:** [October 22, 2019, 3:49pm UTC](https://discourse.slicer.org/t/how-to-overlay-more-than-two-volumes/8860/2 "2019-10-22T15:49:28Z")

</div>

This is not currently supported (the two-layer limitation is pretty hard coded right now). But it is something we would like to support for multi-channel use cases like you describe.

---

<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:** [October 22, 2019, 4:05pm UTC](https://discourse.slicer.org/t/how-to-overlay-more-than-two-volumes/8860/3 "2019-10-22T16:05:55Z")

</div>

@pieper do you envision changing the slice view pipeline or just adding a module that can create an RGB volume from a vector volume?

@sthirumal You can create a colored image by combining any number of volumes into a single volume using custom rules by writing a couple of lines of Python code. See this example for combining two volumes into a third volume: [Creating Volume from Numpy](https://discourse.slicer.org/t/creating-volume-from-numpy/658/18). You can modify it to create a colored volume by creating a new vector volume instead of cloning the input scalar volume.

---

<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:** [October 22, 2019, 4:29pm UTC](https://discourse.slicer.org/t/how-to-overlay-more-than-two-volumes/8860/4 "2019-10-22T16:29:45Z")

</div>

I don’t have a specific implementation in mind - I hope to learn more about the specific needs before forming a plan.

@blowekamp walked me through some use cases at Project Week and my current general thought is that we could have a version/variant of the Vector To Scalar module that allows you to assign primary colors to different channels of an input volume and then display it as a RGB image. Or maybe a scalar volume with the channels mapped to different numerical ranges with a custom color lookup. Another possibility would be custom node and display node types.

I’m not sure which approach would be best for segmentation, quantification, etc.

Of course a lot of this microscopy data is also large, so we need to also think about handling that aspect of things.

---

<div class="post-metadata">

**Author:** ![blowekamp](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/blowekamp/32/1386_2.png) [@blowekamp](https://discourse.slicer.org/u/blowekamp)\
**Post date:** [October 22, 2019, 5:14pm UTC](https://discourse.slicer.org/t/how-to-overlay-more-than-two-volumes/8860/5 "2019-10-22T17:14:08Z")

</div>

For the labeled microscopy case, you will generally have an image with many channels, or multiple images with channels. It is good to be able to assign each channel or molecular “marker” a string label and a color, then select which channels to visualize.

This is a rough block of SimpleITK code to do some of the work in a command line utility:

```auto
        # matplotlib named colors: https://matplotlib.org/3.1.0/gallery/color/named_colors.html
        color_list = ["red", "green", "blue", "cyan", "magenta", "yellow"]
        if len(image_list) == 3:
            color_list = ["cyan", "magenta", "yellow"]

        rescale = False
        out = None
        for img, marker, color_name in zip(image_list, marker_list, cycle(color_list)):

            color = mcolors.get_named_colors_mapping()[color_name]
            rgb_color = mcolors.to_rgb(color)

            print("Colorizing {0} with {1} [{2}]...".format(marker, color_name, rgb_color))
            img = sitk.Cast(img, sitk.sitkFloat32)
            if rescale:
                img = sitk.RescaleIntensity(img, outputMaximum=255)

            img = sitk.Compose([c * img for c in rgb_color])

            if out is None:
                out = img
            else:
                out += img

        del image_list
        out = sitk.Clamp(out, sitk.sitkVectorUInt8, 0, 255)

```
